PROGRAM CODING:-
#include<stdio.h>
#include<stdlib.h>
struct stack
{
int data;
struct stack *next;
};
int isempty(struct stack *t)
{
return(t==NULL);
}
void push(struct stack **t,int num)
{
struct stack *temp;
temp=(struct stack *)malloc(sizeof(struct stack));
temp->data=num;
temp->next=*t;
*t=temp;
}
void pop(struct stack **t)
{
struct stack *temp=*t;
int item;
if(isempty(*t))
{
printf("Fatal error:Stack is empty!**!");
return;
}
else
{
item=temp->data;
*t=(*t)->next;
free(temp);
printf("Item popped: %d",item);
}
}
void peek(struct stack *t)
{
if(isempty(t))
{
printf("Fatal error:Stack is empty!**!");
return;
}
printf("Top element is %d",t->data);
}
void display(struct stack *t)
{
struct stack *q=t;
if(isempty(t))
printf("Fatal Error:Stack is empty!**!");
else
{
printf("\nThe contents of the stack are:\n");
while(q!=NULL)
{
printf("%d\t",q->data);
q=q->next;
}
}
}
void delstack(struct stack **t)
{
struct stack *temp;
if(isempty(*t))
return;
while(*t!=NULL)
{
temp=*t;
*t=(*t)->next;
free(temp);
}
}
void count(struct stack *t)
{
struct stack *q=t;
int c=0;
while(q!=NULL)
{
++c;
q=q->next;
}
printf("Count: %d",c);
}
int main()
{
struct stack *top;
int ch,num;
top=NULL;
while(1)
{
printf("\nMENU:-\n\t1)Push\n\t2)Pop\n\t3)Peek\n\t4)Peed and Pop\n\t5)Display\n\t6)Make Empty\n\t7)Count\n\t8)Exit\nChoice: ");
scanf("%d",&ch);
if(ch==8)
break;
switch(ch)
{
case 1:
printf("Enter the element to pushed: ");
scanf("%d",&num);
push(&top,num);
display(top);
break;
case 2:pop(&top);break;
case 3:peek(top);break;
case 4:peek(top);printf("\n");pop(&top);break;
case 5:display(top);break;
case 6:delstack(&top);break;
case 7:count(top);break;
default:printf("Enter correct menu option!!!");
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct stack
{
int data;
struct stack *next;
};
int isempty(struct stack *t)
{
return(t==NULL);
}
void push(struct stack **t,int num)
{
struct stack *temp;
temp=(struct stack *)malloc(sizeof(struct stack));
temp->data=num;
temp->next=*t;
*t=temp;
}
void pop(struct stack **t)
{
struct stack *temp=*t;
int item;
if(isempty(*t))
{
printf("Fatal error:Stack is empty!**!");
return;
}
else
{
item=temp->data;
*t=(*t)->next;
free(temp);
printf("Item popped: %d",item);
}
}
void peek(struct stack *t)
{
if(isempty(t))
{
printf("Fatal error:Stack is empty!**!");
return;
}
printf("Top element is %d",t->data);
}
void display(struct stack *t)
{
struct stack *q=t;
if(isempty(t))
printf("Fatal Error:Stack is empty!**!");
else
{
printf("\nThe contents of the stack are:\n");
while(q!=NULL)
{
printf("%d\t",q->data);
q=q->next;
}
}
}
void delstack(struct stack **t)
{
struct stack *temp;
if(isempty(*t))
return;
while(*t!=NULL)
{
temp=*t;
*t=(*t)->next;
free(temp);
}
}
void count(struct stack *t)
{
struct stack *q=t;
int c=0;
while(q!=NULL)
{
++c;
q=q->next;
}
printf("Count: %d",c);
}
int main()
{
struct stack *top;
int ch,num;
top=NULL;
while(1)
{
printf("\nMENU:-\n\t1)Push\n\t2)Pop\n\t3)Peek\n\t4)Peed and Pop\n\t5)Display\n\t6)Make Empty\n\t7)Count\n\t8)Exit\nChoice: ");
scanf("%d",&ch);
if(ch==8)
break;
switch(ch)
{
case 1:
printf("Enter the element to pushed: ");
scanf("%d",&num);
push(&top,num);
display(top);
break;
case 2:pop(&top);break;
case 3:peek(top);break;
case 4:peek(top);printf("\n");pop(&top);break;
case 5:display(top);break;
case 6:delstack(&top);break;
case 7:count(top);break;
default:printf("Enter correct menu option!!!");
}
}
return 0;
}
No comments:
Post a Comment