Data Structure Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Array implementation of Stack ADT

Program:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
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");
}}
Output:
Array Implementation of Queue ADT
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("\n Queue using Array");
printf("\n\t1.Insertion \n\t2.Deletion \n\t3.Display \n\t4.Exit");
while(ch)
{
printf("\n Enter the Choice:");
scanf(" %d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d\n",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf(" %d\t",queue[i]);

}
printf("\n");
break;
case 4:
exit(0);
default:
printf(" Wrong Choice: please see the options");
}
}}
getch();
}
Output:
Linked List Implementation of Stack ADT
Program:
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <malloc.h>
#include<stdlib.h>
struct node
{
int label;
struct node *next;
};
void main()
{
int ch = 0;
int k;
struct node *h, *temp, *head;
clrscr();
/* Head node construction */
head = (struct node*) malloc(sizeof(struct node));
head->next = NULL;
while(1)
{
printf("\n Stack using Linked List \n");
printf(" 1->Push ");
printf(" 2->Pop ");
printf(" 3->View ");
printf(" 4->Exit \n");
printf(" Enter your choice : ");
scanf(" %d", &ch);
switch(ch)
{
case 1:
/* Create a new node */
temp=(struct node *)(malloc(sizeof(struct node)));
printf(" Enter the data to push : ");
scanf(" %d", &temp->label);
h = head;
temp->next = h->next;
h->next = temp;
break;
case 2:
/* Delink the first node */
h = head->next;
head->next = h->next;
printf(" Node %s deleted\n", h->label);
free(h);
break;
case 3:
printf("\n HEAD -> ");
h = head;
/* Loop till last node */
while(h->next != NULL)
{
h = h->next;
printf(" %d -> ",h->label);
}
printf(" NULL \n");
break;
case 4:
exit(0);
}
}
}
Output:
Linked list implementation of List ADT
Program:
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;

struct Node
{
int e;
Position next;
};

void Insert(int x, List l, Position p)


{
Position TmpCell;
TmpCell = (struct Node*) malloc(sizeof(struct Node));
if(TmpCell == NULL)
printf(" Memory out of space\n");
else
{
TmpCell->e = x;
TmpCell->next = p->next;
p->next = TmpCell;
}
}
int isLast(Position p)
{
return (p->next == NULL);
}

Position FindPrevious(int x, List l)


{
Position p = l;
while(p->next != NULL && p->next->e != x)
p = p->next;
return p;
}

void Delete(int x, List l)


{
Position p, TmpCell;
p = FindPrevious(x, l);
if(!isLast(p))
{
TmpCell = p->next;
p->next = TmpCell->next;
free(TmpCell);
}
else
printf(" Element does not exist!!!\n");
}

void Display(List l)
{
printf(" The list element are :: ");
Position p = l->next;
while(p != NULL)
{
printf(" ->%d -> ", p->e);
p = p->next;
}
}

void Merge(List l, List l1)


{
int i, n, x, j;
Position p;
printf(" Enter the number of elements to be merged :: ");
scanf("%d",&n);

for(i = 1; i <= n; i++)


{
p = l1;
scanf("%d", &x);
for(j = 1; j < i; j++)
p = p->next;
Insert(x, l1, p);
}
printf(" The new List :: ");
Display(l1);
printf(" The merged List ::");
p = l;
while(p->next != NULL)
{
p = p->next;
}
p->next = l1->next;
Display(l);
}

int main()
{
int x, pos, ch, i;
List l, l1;
l = (struct Node *) malloc(sizeof(struct Node));
l->next = NULL;
List p = l;
printf("\n LINKED LIST IMPLEMENTATION OF LIST ADT\n");
printf("\n\t 1. INSERT\n\t 2. DELETE\n\t 3. MERGE\n\t 4. PRINT\n\t 5. QUIT\n");
do
{
printf(" \n Enter the choice :: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
p = l;
printf(" Enter the element to be inserted :: ");
scanf("%d",&x);
for(i = 1,pos=0; i < pos+1; i++,pos++)
{
p = p->next;
}
Insert(x,l,p);
break;

case 2:
p = l;
printf(" Enter the element to be deleted :: ");
scanf("%d",&x);
Delete(x,p);
break;

case 3:
l1 = (struct Node *) malloc(sizeof(struct Node));
l1->next = NULL;
Merge(l, l1);
break;

case 4:
Display(l);
break;
}
}
while(ch<5);
return 0;
}
Output:
Linked list implementation of Queue ADT

Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*front = NULL, *rear = NULL;
void insert();
void delet();
void display();
int item;
void main()
{
int ch;
printf("\n QUEUE OPERATIONS\n");
printf("\n\t1.Enqueue\n\t2.Dequeue\n\t3.Display\n\t4.Exit\n");
do
{
printf("\n Enter your choice: ");
scanf(" %d", &ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\n Invalid choice. Please try again...\n");
}
} while(1);
getch();
}
void insert()
{
printf("\n Enter ITEM: ");
scanf("%d", &item);
if(rear == NULL)
{
rear = (struct node *)malloc(sizeof(struct node));
rear->info = item;
rear->link = NULL;
front = rear;
}
else{
rear->link = (struct node *)malloc(sizeof(struct node));
rear = rear->link;
rear->info = item;
rear->link = NULL;
}}
void delet(){
struct node *ptr;
if(front == NULL)
printf("\n Queue is empty.\n");
else{
ptr = front;
item = front->info;
front = front->link;
free(ptr);
printf("\n Item deleted: %d\n", item);
if(front == NULL)
rear = NULL;
}}
void display()
{
struct node *ptr = front;
if(rear == NULL)
printf("\n Queue is empty.\n");
else
{
printf("\n");
while(ptr != NULL)
{
printf(" %d\t",ptr->info);
ptr = ptr->link;
}
}
}
Output:
Application of Stack (Infix to Postfix)
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
char stack[20];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
}
void main()
{
char exp[20];
char *e, x;
printf("\n Enter the infix expression :");
scanf("%s",exp);
e = exp;
printf("\n The postfix expression is");
while(*e != '\0')
{
if(isalnum(*e))
printf(" %c",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c",pop());
printf("\n");
}
}
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy