Skip to content

Instantly share code, notes, and snippets.

@raju249
Created August 8, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raju249/679fc17a77840efe70ef to your computer and use it in GitHub Desktop.
Save raju249/679fc17a77840efe70ef to your computer and use it in GitHub Desktop.
/*
Own code implementated by Rajendra Kadam
Source code is available for download on my website
link: iamrajendra.pythonanywhere.com
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
// implementations of stack using linked list
struct stackNode
{
/* data */
int data;
struct stackNode* next;
};
struct stackNode* top = NULL;
// stack functions prototypes
void push();
void pop();
void display();
void main(){
int choice;
clrscr();
while(1){
// operations to be performed
printf("1: Push\n");
printf("2: Pop\n");
printf("3: Display\n");
printf("4: Exit\n");
printf("Enter Command: \n");
scanf("%d",&choice);
// making program menu driven
switch(choice){
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
break;
}
}
getch();
}
// functions implementation
// pushing the node into stack
void push(){
// make a new node to store data
int data;
struct stackNode* newNode;
newNode = (struct stackNode*)malloc(sizeof(struct stackNode));
// take the data to be stored
printf("Enter data: \n");
scanf("%d",&data);
newNode->data = data;
newNode->next = NULL;
// checking is top null?
if (top == NULL){
// make the top point the first node
top = newNode;
}
else{
// update the top if it is not first node
newNode->next = top;
top = newNode;
}
// display the stack
display();
}
// pop node from stack
void pop(){
// sanity check for is stack empty?
if (top == NULL){
printf("Stack Empty\n");
}
else{
top = top->next;
printf("Data popped.\n");
display();
}
}
void display(){
struct stackNode* temp;
temp = top;
printf("_____________\n");
printf("Stack is now\n");
while(temp != NULL){
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
printf("_____________");
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment