/*==========================================================================================
**
** File Name : stack_linkedlist.c
** Creation Date : Fri 13 Oct 2017 11:07:44 PM IST
** Last Modified : Fri 13 Oct 2017 11:22:39 PM IST
** Compiler : gcc
** Author : Manoj Kumar Patra, Asst. Professor
** Organization : Vignan University, Guntur, India.
**
**=========================================================================================*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
}*top=NULL;
void push(int item);
int pop();
void display();
int peek();
int isEmpty();
int main()
{
printf("\n= = = = = = = = = = Stack Using Linked List in C = = = = = = = = = =\n");
int choice,item;
while(1)
{
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display all items of the stack\n");
printf("4. Display item at the top\n");
printf("5. Quit\n");
printf("Enter your choice : ") ;
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
printf("Enter the item to be pushed : ");
scanf("%d",&item);
push(item);
printf("The item %d pushed into the stack\n",item);
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
break;
case 2:
item=pop();
printf("Item %d has been Popped out\n",item);
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
break;
case 3:
display();
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
break;
case 4:
printf("Item at the top is %d\n",peek());
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
break;
case 5:
printf("Program Terminated\n");
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
return;
default :
printf("Oops.. Wrong choice! Select Again\n");
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}
}
}
void push(int item)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("Stack Overflow\n");
return;
}
temp->info=item;
temp->next=top;
top=temp;
}
int pop()
{
struct node *temp;
int item;
if(isEmpty())
{
printf("Stack Underflow\n");
exit(1);
}
temp=top;
item=temp->info;
top=top->next;
free(temp);
return item;
}
void display()
{
struct node *ptr;
ptr=top;
if(isEmpty())
{
printf("Stack is empty\n");
return;
}
printf("Stack elements :\n");
while(ptr!=NULL)
{
printf(" %d\n",ptr->info);
ptr=ptr->next;
}
}
int peek()
{
if(isEmpty())
{
printf("Stack Underflow\n");
exit(1);
}
return top->info;
}
int isEmpty()
{
if(top == NULL)
return 1;
else
return 0;
}
OutPut:
manoj@ubuntu:~/ds/stack$ gcc stack_linkedlist.c -o stack_linkedlist
manoj@ubuntu:~/ds/stack$ ./stack_linkedlist
= = = = = = = = = = Stack Using Linked List in C = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 1
The item 1 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 2
The item 2 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 3
The item 3 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 4
The item 4 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 3
Stack elements :
4
3
2
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 2
Item 4 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 4
Item at the top is 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 3
Stack elements :
3
2
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/stack$
**
** File Name : stack_linkedlist.c
** Creation Date : Fri 13 Oct 2017 11:07:44 PM IST
** Last Modified : Fri 13 Oct 2017 11:22:39 PM IST
** Compiler : gcc
** Author : Manoj Kumar Patra, Asst. Professor
** Organization : Vignan University, Guntur, India.
**
**=========================================================================================*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
}*top=NULL;
void push(int item);
int pop();
void display();
int peek();
int isEmpty();
int main()
{
printf("\n= = = = = = = = = = Stack Using Linked List in C = = = = = = = = = =\n");
int choice,item;
while(1)
{
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display all items of the stack\n");
printf("4. Display item at the top\n");
printf("5. Quit\n");
printf("Enter your choice : ") ;
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
printf("Enter the item to be pushed : ");
scanf("%d",&item);
push(item);
printf("The item %d pushed into the stack\n",item);
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
break;
case 2:
item=pop();
printf("Item %d has been Popped out\n",item);
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
break;
case 3:
display();
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
break;
case 4:
printf("Item at the top is %d\n",peek());
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
break;
case 5:
printf("Program Terminated\n");
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
return;
default :
printf("Oops.. Wrong choice! Select Again\n");
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}
}
}
void push(int item)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("Stack Overflow\n");
return;
}
temp->info=item;
temp->next=top;
top=temp;
}
int pop()
{
struct node *temp;
int item;
if(isEmpty())
{
printf("Stack Underflow\n");
exit(1);
}
temp=top;
item=temp->info;
top=top->next;
free(temp);
return item;
}
void display()
{
struct node *ptr;
ptr=top;
if(isEmpty())
{
printf("Stack is empty\n");
return;
}
printf("Stack elements :\n");
while(ptr!=NULL)
{
printf(" %d\n",ptr->info);
ptr=ptr->next;
}
}
int peek()
{
if(isEmpty())
{
printf("Stack Underflow\n");
exit(1);
}
return top->info;
}
int isEmpty()
{
if(top == NULL)
return 1;
else
return 0;
}
OutPut:
manoj@ubuntu:~/ds/stack$ gcc stack_linkedlist.c -o stack_linkedlist
manoj@ubuntu:~/ds/stack$ ./stack_linkedlist
= = = = = = = = = = Stack Using Linked List in C = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 1
The item 1 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 2
The item 2 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 3
The item 3 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter the item to be pushed : 4
The item 4 pushed into the stack
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 3
Stack elements :
4
3
2
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 2
Item 4 has been Popped out
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 4
Item at the top is 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 3
Stack elements :
3
2
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Push
2. Pop
3. Display all items of the stack
4. Display item at the top
5. Quit
Enter your choice : 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/stack$
No comments:
Post a Comment