C Program to implement STACK operations using Linked Lists

Data structures using C, Stack is a data structure in which the objects are arranged in a non linear order. In stack, elements are aded or deleted from only one end, i.e. top of the stack. In this program, we implement the stack operations using linked list. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdlib.h>
typedef struct node {
int data;
struct node *link;
} NODE;

void Push(int);
int pop();
void Display();
NODE *top = NULL; /* Global Declarations */

main() {
/* Main Program */
int opn, elem;
do {
clrscr();
printf("n ### Linked List Implementation of STACK Operations ### nn");
printf("n Press 1-Push, 2-Pop, 3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d", &opn);
switch (opn) {
case 1:
printf("nnRead the Element tobe pushed ?");
scanf("%d", &elem);
Push(elem);
break;
case 2:
elem = Pop();
if (elem != -1)
printf(" Deleted Node(From Top)with the Data: %dn", elem);
break;
case 3:
printf("Linked List Implementation of Stack: Status:n");
Display();
break;
case 4:
printf("nn Terminating nn");
break;
default:
printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
} while (opn != 4);
}

void Push(int info) {
NODE *temp;
temp = (NODE *) malloc(sizeof(NODE));
if (temp == NULL)
printf(" Out of Memory !! Overflow !!!");
else {
temp->data = info;
temp->link = top;
top = temp;
printf(" Node has been inserted at Top(Front) Successfully !!");
}
}

int Pop() {
int info;
NODE *t;
if (top == NULL) {
printf(" Underflow!!!");
return -1;
} else {
t = top;
info = top->data;
top = top->link;
t->link = NULL;
free(t);
return (info);
}
}

void Display() {
NODE *t;
if (top == NULL)
printf("Empty Stackn");
else {
t = top;
printf("Top->");
while (t) {
printf("[%d]->", t->data);
t = t->link;
}
printf("Nulln");
}
}
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

Leave a Reply