0% found this document useful (0 votes)
58 views

Data Structures Lab

The document describes the implementation of a stack data structure using both arrays and linked lists in C. It provides algorithms for push, pop, peek, display and search operations for each implementation. The array implementation includes a C program example showing how to create a stack and call each function. The linked list implementation similarly includes a C program to demonstrate creating a linked list stack and calling push, pop and display functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Data Structures Lab

The document describes the implementation of a stack data structure using both arrays and linked lists in C. It provides algorithms for push, pop, peek, display and search operations for each implementation. The array implementation includes a C program example showing how to create a stack and call each function. The linked list implementation similarly includes a C program to demonstrate creating a linked list stack and calling push, pop and display functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

EX NO: 3(a)

IMPLEMENTATION OF STACK ADT

(a) USING ARRAY


(b) USING LINKED LIST

DATE: 28/09/2021

AIM:
To write a C program to implement Stack ADT using,
(a) Array.
(b) Linked List.

OPERATIONS:
1. PUSH()
2. POP()
3. PEEK()
4. SEARCH()
5. DISPLAY()

ALGORITHM:
(a) ARRAY:
PUSH():
1. Start.
2. Create an array a[max].
3. Create a variable max and assign a value to it.
4. Create a variable top an set it to -1.
5. Now create a function push().
6. Inside push function,create a if block and check few conditions.
7. If the top value equal to max-1 then, print overflow.
8. Else increment top value by 1.
9. Stop.

POP():
1. Start.
2. Create a pop() function.
3. Create a if block to check few conditions.
4. If the top value equal to -1, then print underflow.
5. Else decrement top value by 1.
6. Stop.

PEEK():
1. Start.
2. Create a peek() function.
3. Create a if block and check whether top equal to -1.
4. If its -1, then print no elements.
5. Else print a[top].
6. Stop.

DISPLAY():
1. Start.
2. Create a display() function.
3. Create a for loop from i=top to i=0 and increment i value.
4. Inside the loop print a[i].
5. Stop.

SEARCH():
1. Start.
2. Create a variable flag and set it to 0.
3. Create a search() function and pass the x value (the element to search from the user) as
parameter and create a for loop.
4. Create a for loop from i=0 to i<=top and increment i.
5. If a[i]==x then, set flag is 1.
6. Create a if block and check whether flag is equal to 1.
7. If its 1 then print element found, else print not found.
8. Stop.

PROGRAM:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
flag=0;
void search(int x){
for(i=top;i>=0;i++){
if(a[i]==x){
flag=1;
}
}
if(flag==1){
printf(“Element found”);
}else{
printf(“Element not found”);
}
}
}

OUTPUT:

ALGORITHM:
(b) LINKED LIST:
PUSH():
1. Start.
2. Create a node with one data and one next pointer variable.
3. Create a top pointer variable and set its value as 0.
4. Now create a pop() function.
5. Create a new node inside it and create a while loop and check a condition.
6. While top==null then top=newnode and newnode’s next value is 0.
7. Else set new node’s next as top and set top = new node.
8. Stop.

POP():
1. Start.
2. Check whether top is 0.
3. If yes then print underflow.
4. Else assign temp=top and top=top’s next.
5. Then delete the node by free(temp).
6. Stop.
PEEK():
1. Start.
2. Check if top==0 then print no elements.
3. Else print top’s data.
4. Stop.

DISPLAY():
1. Start.
2. Assign temp=top.
3. Check if top==0 then print no elements.
4. Create a while loop and check temp’s next value is not equal to null.
5. If yes move temp by temp=temp’s next.
6. Meanwhile print all the temp’s data.
7. Stop.

SEARCH():
1. Start.
2. Create a search() function.
3. Assign temp = top.
4. Create a while loop with condition,temp’s data not equal to x.
5. If temp’s data is equal to x then print element found.
6. Else print element not found.
7. Stop.

PROGRAM:
#include<stdio.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*top = NULL;
void push(int);
void pop();
void display();

void main()
{
int choice, value;
clrscr();
printf("\n:: Stack using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
void search(int x){
temp=top;
while(temp->data!==x&&temp->next!=null){
temp=temp->next;
if(temp->data==x){
printf(“Element found”);
}else
Printf(“Element not found”);
}
}
}

OUTPUT:

RESULT:
Array Implementation and Liked List Implementation of Stack are done and the expected
output is obtained.

You might also like