stack implementation using linked list in c

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

struct node
{
    int data;
    struct node *next;
};
typedef struct node node;

node *top;

void initialize()
{
    top = NULL;
}

void push(int value)
{
    node *tmp;
    tmp = malloc(sizeof(node));
    tmp -> data = value;
    tmp -> next = top;
    top = tmp;
}

int pop()
{
    node *tmp;
    int n;
    tmp = top;
    n = tmp->data;
    top = top->next;
    free(tmp);
    return n;
}

int Top()
{
    return top->data;
}

int isempty()
{
    return top==NULL;
}

void display(node *head)
{
    if(head == NULL)
    {
        printf("NULL\n");
    }
    else
    {
        printf("%d\n", head -> data);
        display(head->next);
    }
}

int main()
{
    initialize();
    push(10);
    push(20);
    push(30);
    printf("The top is %d\n",Top());
    pop();
    printf("The top after pop is %d\n",Top());
    display(top);
    return 0;
}

4.25
8
Wizongod 100 points

                                    /*
 * C Program to Implement a Stack using Linked List
 */
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
&nbsp;
struct node
{
    int info;
    struct node *ptr;
}*top,*top1,*temp;
&nbsp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
&nbsp;
int count = 0;
&nbsp;
void main()
{
    int no, ch, e;
&nbsp;
    printf(&quot;\n 1 - Push&quot;);
    printf(&quot;\n 2 - Pop&quot;);
    printf(&quot;\n 3 - Top&quot;);
    printf(&quot;\n 4 - Empty&quot;);
    printf(&quot;\n 5 - Exit&quot;);
    printf(&quot;\n 6 - Dipslay&quot;);
    printf(&quot;\n 7 - Stack Count&quot;);
    printf(&quot;\n 8 - Destroy stack&quot;);
&nbsp;
    create();
&nbsp;
    while (1)
    {
        printf(&quot;\n Enter choice : &quot;);
        scanf(&quot;%d&quot;, &amp;ch);
&nbsp;
        switch (ch)
        {
        case 1:
            printf(&quot;Enter data : &quot;);
            scanf(&quot;%d&quot;, &amp;no);
            push(no);
            break;
        case 2:
            pop();
            break;
        case 3:
            if (top == NULL)
                printf(&quot;No elements in stack&quot;);
            else
            {
                e = topelement();
                printf(&quot;\n Top element : %d&quot;, e);
            }
            break;
        case 4:
            empty();
            break;
        case 5:
            exit(0);
        case 6:
            display();
            break;
        case 7:
            stack_count();
            break;
        case 8:
            destroy();
            break;
        default :
            printf(&quot; Wrong choice, Please enter correct choice  &quot;);
            break;
        }
    }
}
&nbsp;
/* Create empty stack */
void create()
{
    top = NULL;
}
&nbsp;
/* Count stack elements */
void stack_count()
{
    printf(&quot;\n No. of elements in stack : %d&quot;, count);
}
&nbsp;
/* Push data into stack */
void push(int data)
{
    if (top == NULL)
    {
        top =(struct node *)malloc(1*sizeof(struct node));
        top-&gt;ptr = NULL;
        top-&gt;info = data;
    }
    else
    {
        temp =(struct node *)malloc(1*sizeof(struct node));
        temp-&gt;ptr = top;
        temp-&gt;info = data;
        top = temp;
    }
    count++;
}
&nbsp;
/* Display stack elements */
void display()
{
    top1 = top;
&nbsp;
    if (top1 == NULL)
    {
        printf(&quot;Stack is empty&quot;);
        return;
    }
&nbsp;
    while (top1 != NULL)
    {
        printf(&quot;%d &quot;, top1-&gt;info);
        top1 = top1-&gt;ptr;
    }
 }
&nbsp;
/* Pop Operation on stack */
void pop()
{
    top1 = top;
&nbsp;
    if (top1 == NULL)
    {
        printf(&quot;\n Error : Trying to pop from empty stack&quot;);
        return;
    }
    else
        top1 = top1-&gt;ptr;
    printf(&quot;\n Popped value : %d&quot;, top-&gt;info);
    free(top);
    top = top1;
    count--;
}
&nbsp;
/* Return top element */
int topelement()
{
    return(top-&gt;info);
}
&nbsp;
/* Check if stack is empty or not */
void empty()
{
    if (top == NULL)
        printf(&quot;\n Stack is empty&quot;);
    else
        printf(&quot;\n Stack is not empty with %d elements&quot;, count);
}
&nbsp;
/* Destroy entire stack */
void destroy()
{
    top1 = top;
&nbsp;
    while (top1 != NULL)
    {
        top1 = top-&gt;ptr;
        free(top);
        top = top1;
        top1 = top1-&gt;ptr;
    }
    free(top1);
    top = NULL;
&nbsp;
    printf(&quot;\n All stack elements destroyed&quot;);
    count = 0;
}

4.25 (8 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
c code to implement stack using linked list how to implement stack using linked list in c++ stack display function in c in lined list program to implement a stack using Linked List in c stacks using linked list in c programming9 stacks using linked list in c Stack linked list implementation c create a stack using linked list in c implementation of stack using linked list in c++ programs stack implementation using linked list c++ stack implementation in c++ using linked list stack implementation using linked list and structure c++ stack implementation using linked list and structure stack linked list implementation in c stack implementation linked list in c stack using linked list program in c stack implementation using linked list in c tutorialspoint stack c linked list implementing stack in c++ using linked list stack with linked list program in c++ stack with linked list program in c stack using linked list c++ program stack implementation in c using linked list stack using linked list in c codescope Write a C program to implement linked Stack. stack in c++ using linked list stack with linked list code in c Stack Implementation Using Single Linked List using c c programming stack linked lists operations to implement in stack using linked list in c how to implement a linked list stack in c++ stack using soubly linked list + c stack with linked list in c the linked stack implementation in c linked list example in C stack overflow Program to implement Stack using Linked List and perform all the operations of the stack. in c simple stack operations c with linked list stack with linked list c linked stacks in c c stack implementation linked list linked stack in c implementation of stack using array and linked list in c stack linked list in c stack operations using linkedlist in c stack using linked list implementation in c creating a stack in c with linked list stack c program using linked list implementing stack using linked list in c implementation of stack using linked list in c linked list implementation of stack in c stack in singly linklist stack with linked list which of the following statement is true about linkedlist i) An empty stack will have top = NULL ii) The last node is the bottom of stack and its link field is set to NULL iii)The first node is considered to be at the topp of the stack How would you implement the push method for a Stack implemented as a Linked List? stack implementation using linkedlist java C++ Program to Implement Stack using linked list sample code for stack implementation using linked list using java sample code for stack implementation using linked list program to implement stack operations using linked list. stack implementation using linked list in c++ stack with linked list java program code to implement a stack as a linked list. linked list stack implementation is empty linked list implementation of stack using c Simulate the operations of a stack using a singly linked list c program to implement stack using linked list stack implementation using linked list in cpp implement a stack using linked list in c linked list stack implementation c++ stack implementation using linked list stzck by linked list declaraction wap for linked list implement stack pop and push in c wap for linked list implement stack pop and push sStack using LL implement stack using linkedlist pushing an element in linked list w.a.p for implementation of stack using arrays and link list showingh push,pop and display operation implementation array stack of node stacks using singly linked list code to implement stack using linked list stack implementation using linked list and with out switch case statement in c stack creation in c code using linked list imprelement a stack using linked list creation of stack using linked list in c creation of stack using linked list in the linked list implementation of the stack Implement stack using linked representation PUSH operation of a stack implemented with Singly Linked List (SLL) is nothing bu hoe to make stack value zero in linklist add stack linked list c++ linked list implementation of stack java linked based stack applicaitons Code to PUSH item in Stack using Linked List. stack linear list algorithm stack adt using linked list in c 1. Write a C program for implementing stack using single link list stack linked list java linked list in stack java linked list stack Discuss the stack implemelltation using lists with example. Which operation is permissible for Linked Stack? point Insert_at_any_position Deletion_from_any_position Deletion_from_beginning Insert_at_middle_position implementation of linked listwith stack In which position a new value is inserted into the stack when implemented using linked list Write a program to implement ation of list as stack stack using linked list algorithm pop function for stack with linked list stack push and pop using linked list program for stack using linked list stack using linked list and array storing linked list in stack using structure in c stack as linked list linked list implementation in c c program to implement stack using circular linked list For the linked list implementation of the stack, where are the pushes and pops performed? linked list stack in c c++ stack using linked list overflow implement a stack from ll stack que using link list It is possible to initialize a stack implemented as a linked list in O(1) time stack using linked list in c program imllement stack using linked list push and pop in linked list It is more efficient to implement Stack using LinkedList. stack as linked lsit java how stack is implemented using linked list necessary C functions to implement stack using linked list why a list is not used for implementation of stack stack using linked lisrt stack using singly linked list impementing stack using linked list stack implementtion using linked list stack using sll c program for stack using linked list stack using sll which will take more time Implement Stack using a linked list immplementation of stakc using linked list stacks in linked list stacks using linked list c stacks using linked list If a singly linked list is used for a stack, push(data) will be: linked list stack c++ implement stack using singly linked list Psuedocode for stack using linked list implement stack using the linked list stack implement using linked list stack using linked list in c++ Stack using Linked List. Implement STACK using link list. Stack implemetation using linked list linked stack menu driven program for stack in c using linked list stack in c using linked list java how add linkedlist elemtn to stack stack using linked list string in c linked stack implementation stack linked list implementation stack ked list implementation linked stack implemetation explain stack node in c linked list stack without top c++ stack and link list stack pop linked list implementation of stack implement stack using linked list Insertion and deletion in stack is similar to that of linked list linked list in c pop() push() implementing stack using linked list stack implementation using link in c stack implementation using link stack from linked list which functin of linked list is used in push and pop Write a program that takes the details of a using structures Create a linked stack to store the details stauck eith linked list stack using linked list in c including capacity in struct The only way to implement a stack is using a linked list. stack using linked list without pointer in c staqck using linked list using single pointer Stack can be implemented using link list in which nodes are added and removed from same end using single pointer. node stack implementation create stack using linked list StackAsLinedList java add 2 integers stacks and pop in new empty stack linked list Linked List implementation of Stack and Queue. in c stack using linked list C Write a C program to implement stack using Linked list representation. When we implement stack by using linked list then when we implement stack by linked list stack linked list implementation c++ Write a program to implement stack using linked list. implement a stack using linked list stack as linked list c Linked List implementation of stacks java Linked List implementation of stacks Write a program to implement STACK using Linked List and perform PUSH and POP operations. implementation of stack using linked list when we implement the stack is using linked list when we implement stack using linked list when we implement stack using linked list then insertion of node is done when we implememt stck by using liked list insertiuoin of node is when we implememt stck by using liked list linked list as a stack how to make a linked list function as a stack Start assignment implement stack using linklis in c Start assignment implement stack using linklis c++ Implement a link-based dynamic stack using pointers Implement a link-based dynamic stack using pointers c++ stack program in c using linked list linked list stack program in c print stack using linked list python Implement a link-based dynamic stack using pointers Write a program to create a singly linked list in LIFO fashion in c++ how to implement stack with linked list make a ciruclar linked list and implement a stack java implenting a stack based on ciruclar linked list link-based dynamic stack application of stack using linked list stack and queue using linked list program in c linked list in c peek and pop pop from stack c++ linked list c program to implement stack using singly linked list How to add all elements of a linked list to a stack C# stack in a linked liat stack implementation c++ linked list 2.Write A program to implement Stack using Linked List in c 2.Writea program to implement Stack using Linked List in c 2.Writea program to implement Stack using Linked List. linked list stack implement stack with linked list linked list representaion of stack and queue stack using singly linked list c stack using ll in c linked list using stack c program to implement stack adt using a singly linked list write a c program implement stack adt using singly linked list write a c program/ algorithm to implement stack adt using singly linked list write a c program/ algorithm to implement stack adt using singly linked list. perform following operations: write a c program/ algorithm to implement stack adt using singly linked list. perform following operations: (i) push (ii) pop (iii) peek (iii) display the stack contents Write a C program/ algorithm to implement stack ADT using singly linked list. Perform following operations: (i) Push (ii) Pop (iii) Peek (iii) Display the stack contents stack implementation using linked list java implement stack using linked list n java singly linked list as stack stack implementation using linked list write a pseudocode to implement STACK using linked list with all the basic operations peek in stack uing linked list stack using linkedlist in c stack using linked list isfull method isfull in stack by linedlist stack using linked list insertion at end and deletion at end stack implementation sing lnked list stack using ll implementation of 2 stack in a single linked list linked are implemented using linked list retrun top element in linked list c display method for stack by linkedlist in c++ implementation a stack using singly linked list complexity popping from a stack implementation as a singly linked list,where 'head' is maintain as 'top' complexity popping from a stack implementation as a singly linked list,where 'head' is maintain as 'top' popping from a stack implementation as a singly linked list implementing all the Stack Operations using Linked List stack linked list implementation java stack using linked list in c implementing a stack using a linked list in c implementation of linked list with stack implementation of linkedlist with stack stack with singly linked list stack using linkedlist Write a code for stack using linked list implementing a linked list using stack stack using linked list stack using single linked list in c implement stack using ll output for stack using linked list implementation of stack adt using linked list in c++ implementing stack as a adt using linked list linked implementation of stack cpp linked list stack capacity Write a function to pop an element and push an element into a stack using SLL implementing stack using linked list in java implement stack using link list in c stack using linked list java stack strings using linked lists in c pseudo code for Menu driven C program to implement stack using linked list. pseduo code Menu driven C program to implement stack using linked list. . Write a C program using dynamic variables and pointers to construct a stack of integers using singly linked list to perform the following operations: a. Push b. Pop c stack implementation using linked list in c
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source