0% found this document useful (1 vote)
17 views2 pages

Stack Implementation in C: Arrays & Links

Uploaded by

Ajay Kumar Reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
17 views2 pages

Stack Implementation in C: Arrays & Links

Uploaded by

Ajay Kumar Reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#Stach using arrays {

printf("top element in stack is


#include <stdio.h> %d",stack[top]);
#define SIZE 5 }
}
int stack[SIZE];
int top = -1, i, value, x; void display()
char ch; {
if(top == -1)
void push() printf("Stack is empty");
{ else
if(top == SIZE-1) {
printf("stach is full"); for(i=top;i>=0;i--)
else printf("%d
{ \n",stack[top]);
top++; }
printf("Enter a value to }
push\n");
scanf("%d",&value); void main()
stack[top] = value; {
} do
} {
printf("select operation to
void pop() perform on stack \n");
{ printf("1. push \t 2. pop \n 3.
if(top == -1) peep \t 4. display \n");
printf("Stack is empty"); scanf("%d",&x);
else
{ switch (x)
printf("deleted element is {
%d",stack[top]); case 1:
top--; push();
} break;
} case 2:
pop();
void peep() break;
{ case 3:
if(top == -1) peep();
printf("Stack is empty"); break;
else

case 4: scanf("%c", &ch);


display(); }while(ch=='y');
break; }
}
printf("Press \'y\' to add a
element or \'n\' to exit\n");
fflush(stdin);
# Stack using links }

struct node void peer()


{ {
int data; if(top==NULL)
struct node *next; printf("Stack is empty");
}*new, *top, *temp; else
{
void pop() printf("Top element of the
{ stack is %d", top->data);
do }
{ }
new=(struct node
*)malloc(sizeof(struct node)); void display()
printf("Enter a value : "); {
scanf("%d",&value); temp = top;
new->data = value; if(top==NULL)
new->next = NULL; printf("Stack is empty");
if(top==NULL) else
{ {
top = new; while(temp != NULL)
} {
else printf("%d -> ", top-
{ >data);
new->next = top; temp = temp->next;
top = new; }
} }
}
printf("Press \'y\' to add a
node and \'n\' to exit\n");
fflush(stdin);
scanf(" %c", &ch);
} while(ch=='y');
}

void push()
{
if(top == NULL)
printf("Stack is empty");
else
{
temp = top;
printf(" deleted item is %d",
top->data);
top = tos->next;
temp->next = NULL;
free(temp);
}

You might also like