stack using linklist

#include<stdio.h>
#include<conio.h>
struct node* push(struct node *head);
struct node* pop(struct node *head);
struct node* peep(struct node *head);
struct node* display(struct node *head);

struct node
{
 int data;
 struct node *next;
};
void main()
{
 int num=0, choice,con=0;
 struct node *head=NULL,*temp1;
 do
 {
 printf(" 1. push value in stack\n 2. pop value from stack \n 3.print top value\n 4.display stack ");
 printf("\n\nenter your choice :: ");
 scanf("%d",&choice);
 switch(choice)
 {
 case 1:
       temp1=push(head);
    break;
 case 2:
    temp1=pop(temp1);
    break;
 case 3:
    peep(temp1);
    break;
 case 4:
    display(temp1);
    break;
 default:
      printf("\ninvalid choice");
   break;
 }
 printf("\ncontinu so enter (1) :: ");
 scanf("%d",&con);
 }while(con==1);
 getch();
}

struct node* push(struct node *head)
{
 struct node *new_node,*current,*temp;
 int ch;
 do{

 new_node=(struct node *)malloc(sizeof(struct node));
   
 
 printf("\nEnter Value :");
 scanf("%d",&ch);
 if(ch!=-1)
 {
 new_node->data=ch;
 new_node->next=NULL;

 if(head==NULL)
   {
   head=new_node;
   temp=new_node;
   temp->next=NULL;   
   }
 else
   {
   head=new_node;
   head->next=temp;
   temp=new_node;
   
   }
 }
 }while(ch!=-1);

      return head;
}

struct node* pop(struct node *head)
{
 if(head!=NULL)
 {
  printf("\n delete value :: %d",head->data);
 head=head->next;
 return head;
 }
 else
 {
  head=NULL;
  printf("\n stack is empty");
 }
 return head;
}
struct node* peep(struct node *head)
{
 printf("\ntop value is :: %d",head->data);
}

struct node* display(struct node *head)
{
 struct node *temp1;
 temp1=head;
 if(head==NULL)
 {
  printf("\nstack is empty");
 }
 else
 {
 while(temp1!=NULL)
 {
  printf("\n%d",temp1->data);
  temp1=temp1->next;
 }
 }
 
}

***********************************************************************************************************************
------------------------------------------------------------------------

         output

------------------------------------------------------------------------

 1. push value in stack
 2. pop value from stack
 3.print top value
 4.display stack

enter your choice :: 1

Enter Value :10

Enter Value :20

Enter Value :30

Enter Value :40

Enter Value :50

Enter Value :-1

continu so enter (1) :: 1
 1. push value in stack
 2. pop value from stack
 3.print top value
 4.display stack

enter your choice :: 4

50
40
30
20
10
continu so enter (1) :: 1
 1. push value in stack
 2. pop value from stack
 3.print top value
 4.display stack

enter your choice :: 2

 delete value :: 50
continu so enter (1) :: 1
 1. push value in stack
 2. pop value from stack
 3.print top value
 4.display stack

enter your choice :: 3

top value is :: 40
continu so enter (1) :: 1
 1. push value in stack
 2. pop value from stack
 3.print top value
 4.display stack

enter your choice :: 4

40
30
20
10
continu so enter (1) ::

********************************************************************************************************************************
SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment