|
|
stacks using array & linked
list
--------------------------------------------------------------------------------
Description
: operations on stack-push,pop,display implemented using both array & linked
list
Code
:
*******************************************************************
IMPLEMENTATION
OF STACKS USING ARRAY & USING LINKED LIST
********************************************************************
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define
MAX 10
struct st_a{
int topmost;
int stack[MAX] ;
};
struct
st_a s;
struct stl{
int data;
struct stl*next
;
}*top,*p,*q;
void stack_array();
void
stack_llist();
void main()
{
int i,ch,ch1;
char
c;
clrscr();
printf(" PROGRAM TO IMPLEMENT STACKS USING ARRAY & USING
LINKED
LIST");
do{
printf("
Select method to implement
stacks...
1.Array
2.linked list
3.Exit
your choice:
");
scanf("%d",&ch);
switch(ch)
{
case
1:
stack_array();
break;
case 2:
stack_list();
break;
case
3:
exit();
default:
printf("Invalid
choice");
break;
}
printf("Return to main
menu?y/n:");
scanf("%s",&c);
}
while(c=='y');
getch();
}
void stack_array()
{
void
push_array();
void pop_array();
void display_array();
int i,ch2
;
char c1;
printf("IMPLEMENTAION OF STACKS USING
ARRAY<BR>);
do
{
printf("*******MENU*******");
printf("
1.Push
2.Pop
3.Display
4.Exit
your
choice:");
scanf("%d",&ch2);
switch(ch2)
{
case
1:
push_array();
break;
case 2:
pop_array();
break;
case
3:
display_array();
break;
case
4:
exit();
default:
printf("Invalid
choice");
break;
}
printf("Want to perform another
operation?y/n:");
scanf("%s",&c1);
}
while(c1=='y');
}
int
full()
{
if(s.topmost==MAX)
return 1;
else
return
0;
}
int empty()
{
if(s.topmost==0)
return
1;
else
return 0;
}
void push_array()
{
int
full();
char
ch3;
s.topmost=0;
do{
if(full()==1)
{
printf("The Stack is
full!!");
break;
}
else
{
s.topmost++;
printf("Enter
the Element to be pushed :
");
scanf("%d",&s.stack[s.topmost]);
printf("Push another
element?y/n?:");
//flushall();
scanf("%s",&ch3);
}
}
while(ch3=='y' || ch3=='Y');
}
void pop_array()
{
int
empty();
char ch;
do{
if(empty()==1)
{
printf("The Stack is
Empty!!");
break;
}
else
{
printf("%d has been
POPPED!",s.stack[s.topmost]);
s.stack[s.topmost--]=0;
printf("Pop
other element?y/n:");
//flushall();
scanf("%s",&ch);
}
}
while(ch=='y' || ch=='Y');
}
void display_array()
{
int
i;
if(empty()==1)
printf("
No
Records!!!!!!");
else
{
printf("
The contents of the stack
are....<BR>);
for(i=s.topmost ;i>=1;
i--)
printf("
%d",s.stack[i]);
}
}
/* Implementation of
stacks using linked list */
void stack_llist()
{
int*
push_llist(struct stl *,struct stl *);
int* pop_llist(struct stl *,struct stl
*);
void display_llist(struct stl *);
int i,ch5 ;
char
c5;
printf("IMPLEMENTAION OF STACKS USING LINKED
LIST<BR>);
do
{
printf("
*****MENU*******");
printf("
1.Push
2.Pop
3.Display
4.Exit
your
choice:");
scanf("%d",&ch5);
switch(ch5)
{
case
1:
top=push_llist(top,p);
break;
case
2:
top=pop_llist(top,q);
break;
case
3:
display_llist(top);
break;
case
4:
exit();
default:
printf("Invalid
choice");
break;
}
printf("Want to perform another
operation?y/n:");
scanf("%s",&c5);
}
while(c5=='y');
}
int*
push_llist(struct stl *top,struct stl *p)
{
void display_llist(struct stl
*);
int num;
char c7;
do{
printf("Enter the Element to
be pushed : ");
scanf("%d",&num);
p=malloc(sizeof(struct
stl));
p->data= num;
p->next=NULL;
if(top==NULL)
top=p;
else
{
p->next=top;
top=p;
}
printf("Push
another element?y/n?:");
scanf("%s",&c7);
} while(c7=='y' ||
c7=='Y');
return(top);
}
int* pop_llist(struct stl *top,struct
stl *q)
{
void display_llist(struct stl *);
char
c8;
do{
if(top==NULL)
{
printf("List is
empty!");
break;
}
else
{
q=top;
top=top->next;
q->next=NULL;
free(q);
}
printf("Pop
other element?y/n:");
scanf("%s",&c8);
} while(c8=='y' ||
c8=='Y');
return(top);
}
void display_llist(struct stl
*top)
{
if(top==NULL)
printf("stack is
empty!!");
else
{
q=top;
printf ( "contents of stack
are:<BR> ) ;
while ( q!= NULL ) /* traverse the entire linked list
*/
{
printf ( "%d ", q -> data ) ;
q = q -> next
;
}
}
} |
|
|
No comments:
Post a Comment