Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Last active April 6, 2019 19:47
Show Gist options
  • Select an option

  • Save jatinsharrma/47b0c3ab06856563b890e400338956da to your computer and use it in GitHub Desktop.

Select an option

Save jatinsharrma/47b0c3ab06856563b890e400338956da to your computer and use it in GitHub Desktop.
Stack implementation using linked list
#include<stdio.h>
#include<stdlib.h>
//prottyping
void push(int);
int pop(void);
void traverse(void);
int length(void);
void delete(void);
//initialize
struct node {
int data;
struct node* link;
};
struct node * top = NULL;
int capacity;
//-------------------------------------------------------------------------------------------
int main(){
printf("Enter capacity of stack : ");
scanf("%d",&capacity);
while(1){
printf("\nStack operations\n");
printf(" 1. Push \n 2. Pop \n 3. Traverse \n 4. Length \n 5. Empty the stack \n 6. Exit");
int option;
printf("\nEnter your choice : ");
scanf("%d",&option);
switch (option)
{
case 1 : {if (length() >= capacity){
printf("\n Stack overflow\n");
}
else{
printf("\nEnter push data : ");
int data;
scanf("%d", &data);
push(data);
}
break;}
case 2 : {int temp;
temp = pop();
if(temp){
printf("\nPopped successfully\n");
}
else{
printf("\nStack underflow\n");
}
break;}
case 3 : traverse();
break;
case 4 :{int len;
len = length();
if (len == 0){
printf("\nStack underflow\n");
}
else{
printf("\nTotal elements in Stack : %d\n", len);
}
break;}
case 5 : delete();
break;
case 6 : delete();
exit(0);
break;
default: printf("\nInvalid input\n");
break;
}
}
return 0;
}
//-------------------------------------------------------------------------------------------
void push(int data){
struct node * temp;
temp = (struct node *)malloc(sizeof(struct node));
temp -> data = data;
if(top == NULL){
top = temp;
temp -> link = NULL;
}
else{
temp -> link = top;
top = temp;
}
}
//-------------------------------------------------------------------------------------------
int pop(){
if (top == NULL){
return 0;
}
else{
struct node* temp;
temp = top;
if (temp -> link == NULL){
top = NULL;
free(temp);
}
else{
top = temp -> link;
temp -> link = NULL;
free(temp);
}
return 1;
}
}
//-------------------------------------------------------------------------------------------
void traverse(){
struct node* temp;
temp = top;
if (top == NULL){
printf("\nStack underflow\n");
}
else{
printf("\n");
while(temp != NULL){
printf("%d ", temp -> data);
temp = temp -> link;
}
printf("\n");
}
}
//-------------------------------------------------------------------------------------------
int length(){
struct node* temp;
int count = 0;
temp = top;
if (top == NULL){
return 0;
}
else{
while(temp != NULL){
temp = temp -> link;
count++;
}
return count;
}
}
void delete(){
if (top == NULL){
printf("\nStack underflow\n");
}
else{
struct node* temp;
temp = top;
if (temp -> link == NULL){
top = NULL;
free(temp);
}
else{
while (temp ->link == NULL){
top = temp -> link;
temp -> link = NULL;
free(temp);
}
free(temp);
top = NULL;
}
printf("\nStack cleaned successfully\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment