Data Structure Lab Manual R20 CSE (1)
Data Structure Lab Manual R20 CSE (1)
List of Experiments:
Exercise -1 (Searching)
a)Write C program that use both recursive and non-recursive functions to perform Linear search
for a Key value in a given list.
b) Write C program that use both recursive and non-recursive functions to perform Binary search
for a Key value in a given list.
Exercise -2 (Sorting-I)
a) Write C program that implement Bubble sort, to sort a given list of integers in ascending order
b) Write C program that implement Quick sort, to sort a given list of integers in ascending order
c) Write C program that implement Insertion sort, to sort a given list of integers in ascending
order
Exercise -3(Sorting-II)
a) Write C program that implement radix sort, to sort a given list of integers in ascending order
b) Write C program that implement merge sort, to sort a given list of integers in ascending order
Exercise -5(Queue)
a) Write C program that implement Queue (its operations) using arrays.
b) Write C program that implement Queue (its operations) using linked lists
Exercise -6(Stack)
a) Write C program that implement stack (its operations) using arrays
b) Write C program that implement stack (its operations) using Linked list
c) Write a C program that uses Stack operations to evaluate postfix expression
Exercise -1 (Searching)
a)Write C program that use both recursive and non-recursive functions to perform Linear
search for a Key value in a given list.
Program:
#include <stdio.h>
#define MAX_LEN 10
int main() {
int l[MAX_LEN], num, ele;
int ch;
printf("======================================================");
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Linary Search using Recursion method");
printf("\n[2] Linary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);
switch(ch) {
case 1:
printf("\n**Recursion method**\n");
l_search_recursive(l,num,ele);
break;
case 2:
printf("\n**Non-Recursion method**\n");
l_search_nonrecursive(l,num,ele);
break;
}
}
return 0;
}
/*end main*/
/* Non-Recursive method*/
void l_search_nonrecursive(int l[],int num,int ele) {
int j, f=0;
for(j=0; j<num; j++) {
if( l[j] == ele) {
printf("\nThe element %d is present at position %d in list\n",ele,j);
f=1;
break;
}
if(f==0)
printf("\nThe element is %d is not present in the list\n",ele);
}
/* Recursive method*/
void l_search_recursive(int l[],int num,int ele) {
int f = 0;
if( l[num] == ele) {
printf("\nThe element %d is present at position %d in list\n",ele,num);
f=1;
} else {
if((num==0) && (f==0)) {
printf("The element %d is not found.",ele);
} else {
l_search_recursive(l,num-1,ele);
}
}
}
Output:
======================================================
MENU
=====================================================
[1] Linary Search using Recursion method
[2] Linary Search using Non-Recursion method
**Recursion method**
--------------------------------
Process exited after 20.71 seconds with return value 0
Press any key to continue . . .
Output 2:
======================================================
MENU
=====================================================
[1] Linary Search using Recursion method
[2] Linary Search using Non-Recursion method
**Non-Recursion method**
Exercise -1 (Searching)
b) Write C program that use both recursive and non-recursive functions to perform
Binary search for a Key value in a given list.
Program:
#include <stdio.h>
#define MAX_LEN 10
/* Non-Recursive function*/
void b_search_nonrecursive(int l[],int num,int ele) {
int l1,i,j, flag = 0;
l1 = 0;
i = num-1;
while(l1 <= i) {
j = (l1+i)/2;
if( l[j] == ele) {
printf("\nThe element %d is present at position %d in list\n",ele,j);
flag =1;
break;
} else if(l[j] < ele)
l1 = j+1;
else
i = j-1;
}
if( flag == 0)
printf("\nThe element %d is not present in the list\n",ele);
}
/* Recursive function*/
int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a) {
int m,pos;
if (arrayStart<=arrayEnd) {
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a<l[m])
return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
}
return -1;
}
/*main function*/
int main() {
int l[MAX_LEN], num, ele,f,l1,a;
int ch,pos;
//clrscr();
printf("=====================================================
=");
printf("\n\t\t\tMENU");
printf("\n====================================================
=");
printf("\n[1] Binary Search using Recursion method");
printf("\n[2] Binary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);
switch(ch) {
case 1:
printf("\nRecursive method:\n");
pos=b_search_recursive(l,0,num,ele);
if(pos==-1) {
printf("Element is not found");
} else {
printf("Element is found at %d position",pos);
}
//getch();
break;
case 2:
printf("\nNon-Recursive method:\n");
b_search_nonrecursive(l,num,ele);
//getch();
break;
}
}
//getch();
}
Output:
======================================================
MENU
=====================================================
Output 2:
======================================================
MENU
=====================================================
[1] Binary Search using Recursion method
Non-Recursive method:
The element 49 is present at position 3 in list
--------------------------------
Process exited after 26.74 seconds with return value
Exercise -2 (Sorting-I)
a) Write C program that implement Bubble sort, to sort a given list of integers in
ascending order
Program:
4
978
62
15
Sorted list in ascending order:
4
15
18
32
62
65
95
978
--------------------------------
Process exited after 8.559 seconds with return value
Exercise -2 (Sorting-I)
b) Write C program that implement Quick sort, to sort a given list of integers in ascending
order
Program:
#include<stdio.h>
#define MAX_SIZE 1000
void display(int b[],int n) {
printf("\n");
for(int i=0; i<n; i++) {
printf("%d ",b[i]);
}
printf("\n");
}
int partition(int a[],int beg,int end) {
int pivot;
pivot = a[beg];
int i = beg;
int j = end;
while(i<j) {
while(a[i]<=pivot) {
i++;
}
while(a[j]>pivot) {
j--;
}
if(i<j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
a[beg] = a[j];
a[j] = pivot;
return j;
}
void quicksort(int a[],int begg,int endd) {
if(begg<endd) {
int loc = partition(a,begg,endd);
quicksort(a,begg,loc-1);
quicksort(a,loc+1,endd);
}
}
int main() {
int a[MAX_SIZE];
int n;
printf("Enter the size of Array:");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (int i= 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nElements Before Sorting:\n");
display(a,n);
int beg=0;
int end= n-1;
quicksort(a,beg,end);
printf("\nAscending order Elements: \n");
display(a,n);
return 0;
}
Output:
Enter the size of Array:8
Enter 8 integers
62
-959
62
31
48
87
625
-16
Elements Before Sorting:
62 -959 62 31 48 87 625 -16
Ascending order Elements:
-959 -16 31 48 62 62 87 625
--------------------------------
Process exited after 13.03 seconds with return value 0
Press any key to continue . . .
Exercise -2 (Sorting-I)
c) Write C program that implement Insertion sort, to sort a given list of integers in
ascending order
Program:
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 1000
void display(int b[],int n) {
printf("\n");
for(int i=0; i<n; i++) {
printf("%d ",b[i]);
}
printf("\n");
}
void insertion(int a[],int n) {
for(int i=1; i<n; i++) {
int temp = a[i];
int j=i-1;
while(temp<=a[j]&& j>=0) {
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
int main() {
int a[MAX_SIZE];
int n;
printf("Enter the size of Array:");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (int i= 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nElements Before Sorting:\n");
display(a,n);
//Logic for insertion sort
insertion(a,n);
//Display of array elements
printf("\nAscending order Elements: \n");
display(a,n);
return 0;
}
Output:
Enter the size of Array:10
Enter 10 integers
65
32
18
94
65
-12
65
48
-84
32
65 32 18 94 65 -12 65 48 -84 32
-84 -12 18 32 32 48 65 65 65 94
--------------------------------
Process exited after 13.44 seconds with return value 0
Press any key to continue . . .
Exercise -3(Sorting-II)
a) Write C program that implement radix sort, to sort a given list of integers in ascending
order
Program:
#include<stdio.h>
#define MAX_SIZE 1000
void display(int b[],int size) {
printf("\n");
for(int i=0; i<size; i++) {
printf("%d\t",b[i]);
}
printf("\n");
}
int bigno(int d[],int size) {
int large = d[0];
for(int i=1; i<size; i++) {
if(large<d[i]) {
large = d[i];
}
}
return large;
}
void countsort(int d[],int size1,int pos1) {
int count[10];
int aux[8];
for(int i=0; i<10; i++) {
count[i] = 0;
}
for(int i=0; i<size1; i++) {
count[(d[i]/pos1)%10]++;
}
for(int i=1; i<10; i++) {
count[i] = count[i]+count[i-1];
}
for(int i=size1-1; i>=0; i--) {
aux[--count[(d[i]/pos1)%10]]=d[i];
}
for(int i=0; i<size1; i++) {
d[i]=aux[i];
}
}
void radixsort(int c[],int size) {
int digit= bigno(c,size);
//printf("%d",digit);
for(int pos =1; digit/pos>0; pos*=10) {
countsort(c,size,pos);
}
}
int main() {
int a[MAX_SIZE],n;
printf("Enter the size of Array:");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (int i= 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nElements Before Sorting:\n");
display(a,n);
radixsort(a,n);
printf("\nAscending order Elements: \n");
display(a,n);
return 0;
}
Output:
Enter the size of Array:8
Enter 8 integers
12
3
48
65
952
6478
23
48
Elements Before Sorting:
12 3 48 65 952 6478 23 48
Ascending order Elements:
3 12 23 48 48 65 952 6478
--------------------------------
Process exited after 13.43 seconds with return value 0
Press any key to continue . . .
Exercise -3(Sorting-II)
b) Write C program that implement merge sort, to sort a given list of integers in ascending
order
Program:
// merge sort
#include<stdio.h>
#define MAX_SIZE 1000
void display(int b[],int no) {
int i=0;
printf("\n");
while(i<no) {
printf("%d ",*(b+i));
i++;
}
printf("\n");
}
//Merging elements after dividing individual elements
void merge(int c[],int begi,int midd,int endi) {
int arr[10];
int i = begi;
int j = midd+1;
int k = begi;
while(i<=midd&&j<=endi){
if(c[i]<c[j]){
arr[k] = c[i];
i++;
}else{
arr[k] = c[j];
j++;
}
k++;
}
if(i>midd){
while(j<=endi){
arr[k] = c[j];
j++;
k++;
}
}else{
while(i<=midd){
arr[k] = c[i];
i++;
k++;
}
}
for(int i=begi;i<k;i++)
c[i] = arr[i];
}
//Merge sort algorithm
void mergesort(int b[],int beg,int end) {
if(beg<end) {
int mid = (beg+end)/2;
mergesort(b,beg,mid);
mergesort(b,mid+1,end);
merge(b,beg,mid,end);
}
}
int main() {
int a[MAX_SIZE],n;
printf("Enter the size of Array:");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (int i= 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nElements Before Sorting:\n");
display(a,n);
int begg=0;
int endd=n-1;
mergesort(a,begg,endd);
printf("\nAscending order Elements: \n");
display(a,n);
return 0;
}
Output:
Enter the size of Array:8
Enter 8 integers
95
62
31
84
75
-65
98
-23
95 62 31 84 75 -65 98 -23
-65 -23 31 62 75 84 95 98
--------------------------------
Process exited after 14.01 seconds with return value 0
Press any key to continue . . .
printf("\n------------MENU--------------------\n");
printf("\n 1.Create");
printf("\n 2.Display ");
printf("\n 3.Insert at the beginning");
printf("\n 4.Insert at the end ");
printf("\n 5.Insert at specified position");
printf("\n 6.Delete from beginning");
printf("\n 7.Delete from the end");
printf("\n 8.Delete from specified position");
printf("\n 9.Exit");
printf("\n--------------------------------------\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice) {
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_begin();
break;
case 4:
insert_end();
break;
case 5:
insert_pos();
break;
case 6:
delete_begin();
break;
case 7:
delete_end();
break;
case 8:
delete_pos();
break;
case 9:
exit(0);
break;
default:
printf("\n Wrong Choice:\n");
break;
}
}
return 0;
}
void create() {
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL) {
printf("\nOut of Memory Space:\n");
exit(0);
}
printf("\nEnter the data value for the node:\t");
scanf("%d",&temp->info);
temp->next=NULL;
if(start==NULL) {
start=temp;
} else {
ptr=start;
while(ptr->next!=NULL) {
ptr=ptr->next;
}
ptr->next=temp;
}
}
void display() {
struct node *ptr;
if(start==NULL) {
printf("\nList is empty:\n");
return;
} else {
ptr=start;
printf("\nThe List elements are:\n");
while(ptr!=NULL) {
printf("%d\t",ptr->info );
ptr=ptr->next ;
}
}
}
void insert_begin() {
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL) {
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL) {
start=temp;
} else {
temp->next=start;
start=temp;
}
}
void insert_end() {
struct node *temp,*ptr;
temp->next=NULL;
if(pos==0) {
temp->next=start;
start=temp;
} else {
for(i=0,ptr=start; i<pos-1; i++) {
ptr=ptr->next;
if(ptr==NULL) {
printf("\nPosition not found:[Handle with care]\n");
return;
}
}
temp->next =ptr->next ;
ptr->next=temp;
}
}
void delete_begin() {
struct node *ptr;
if(ptr==NULL) {
printf("\nList is Empty:\n");
return;
} else {
ptr=start;
start=start->next ;
printf("\nThe deleted element is :%d\t",ptr->info);
free(ptr);
}
}
void delete_end() {
struct node *temp,*ptr;
if(start==NULL) {
printf("\nList is Empty:");
exit(0);
} else if(start->next ==NULL) {
ptr=start;
start=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
} else {
ptr=start;
while(ptr->next!=NULL) {
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
}
void delete_pos() {
int i,pos;
struct node *temp,*ptr;
if(start==NULL) {
printf("\nThe List is Empty:\n");
exit(0);
} else {
printf("\nEnter the position of the node to be deleted:\t");
scanf("%d",&pos);
if(pos==0) {
ptr=start;
start=start->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
} else {
ptr=start;
for(i=0; i<pos; i++) {
temp=ptr;
ptr=ptr->next ;
if(ptr==NULL) {
printf("\nPosition not Found:\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
}
}
Output:
------------MENU--------------------
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:1
------------MENU--------------------
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:1
------------MENU--------------------
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:1
------------MENU--------------------
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
Wrong Choice:
------------MENU--------------------
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:1
------------MENU--------------------
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2
1.Create
2.Display
------------MENU--------------------
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:3
------------MENU--------------------
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:8
------------MENU--------------------
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:7
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2
1.Create
2.Display
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:9
--------------------------------
Process exited after 88.95 seconds with return value 0
Press any key to continue . . .
#include<stdio.h>
#include<stdlib.h>
void createList(int n);
void reverse_list();
void displayList();
void createList(int n) {
struct Node *newNode, *temp;
int data, i;
head = (struct Node *)malloc(sizeof(struct Node));
// When the list is empty
if(head == NULL) {
printf("Unable to allocate memory.");
} else {
printf("\nEnter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++) {
newNode = (struct Node *)malloc(sizeof(struct Node));
if(newNode == NULL) {
printf("Unable to allocate memory.");
break;
} else {
printf("\nEnter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
}
}
void displayList() {
struct Node *temp;
if(head == NULL) {
printf("List is empty.");
} else {
temp = head;
//Print the list
while(temp != NULL) {
printf("%d\t", temp->data);
temp = temp->next;
}
printf("\n");
}
}
int main() {
int n;
printf("\nEnter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nThe List is \n");
displayList();
printf("\nThe Reversed Linked List is\n");
reverse_list();
displayList();
return 0;
}
Output:
The List is
32 65 48 98 65 45
--------------------------------
Process exited after 10.66 seconds with return value 0
Press any key to continue . . .
Exercise -5(Queue)
a) Write C program that implement Queue (its operations) using arrays.
Program:
/*
* C Program to Implement a Queue using an Array
*/
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main() {
int choice,del;
while (1) {
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert() {
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else {
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delete() {
if (front == - 1 || front > rear) {
printf("Queue Underflow \n");
return;
} else {
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display() {
int i;
if (front == - 1)
printf("Queue is empty \n");
else {
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */
Output:
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
4.Quit
Enter your choice : 2
Element deleted from queue is : 95
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Queue Underflow
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Queue Underflow
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 4
--------------------------------
Process exited after 45.16 seconds with return value 0
Press any key to continue . . .
Exercise -5(Queue)
b) Write C program that implement Queue (its operations) using linked lists
Program:
/*
* C Program to Implement Queue Data Structure using Linked List
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main() {
int no, ch, e;
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1) {
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
rear->ptr = NULL;
rear->info = data;
front = rear;
} else {
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
if (front1 == NULL) {
printf("\n Error: Trying to display elements from empty queue");
return;
} else if (front1->ptr != NULL) {
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
} else {
Output:
1 - Enque
2 - Deque
3 - Front element
4 - Empty
5 - Exit
6 - Display
7 - Queue size
Enter choice : 3
Enter choice : 1
Enter data : 95
Enter choice : 1
Enter data : 64
Enter choice : 6
65 95 64
Enter choice : 3
Front element : 65
Enter choice : 7
Queue size : 3
Enter choice : 4
Queue not empty
Enter choice : 1
Enter data : 46
Enter choice : 1
Enter data : 27
Enter choice : 6
65 95 64 46 27
Enter choice : 2
Dequed value : 65
Enter choice : 2
Dequed value : 95
Enter choice : 4
Queue not empty
Enter choice : 5
--------------------------------
Process exited after 71.4 seconds with return value 0
Press any key to continue . . .
Exercise -6(Stack)
a) Write C program that implement stack (its operations) using arrays
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");
}
}
Output:
48
32
65
65
Press Next Choice
Enter the Choice:2
The popped elements is 48
Enter the Choice:1
Enter a value to be pushed:46
Enter the Choice:3
The elements in STACK
46
32
65
65
Press Next Choice
Enter the Choice:2
Exercise -6(Stack)
b) Write C program that implement stack (its operations) using Linked list
Program:
/*
* C Program to Implement a Stack using Linked List
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main() {
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1) {
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else {
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
if (top1 == NULL) {
printf("Stack is empty");
return;
}
if (top1 == NULL) {
printf("\n Error : Trying to pop from empty stack");
return;
} else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
8 - Destroy stack
Enter choice : 1
Enter data : 65
Enter choice : 1
Enter data : 32
Enter choice : 1
Enter data : 46
Enter choice : 3
Top element : 46
Enter choice : 6
46 32 65
Enter choice : 7
Enter choice : 1
Enter data : 7
Enter choice : 1
Enter data : 62
Enter choice : 3
Top element : 62
Enter choice : 2
Popped value : 62
Enter choice : 4
--------------------------------
Process exited after 48.53 seconds with return value 0
Press any key to continue . . .
Exercise -6(Stack)
c) Write a C program that uses Stack operations to evaluate postfix expression
Program:
#include<stdio.h>
int stack[20];
int top = -1;
void push(int x) {
stack[++top] = x;
}
int pop() {
return stack[top--];
}
int main() {
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0') {
if(isdigit(*e)) {
num = *e - 48;
push(num);
} else {
n1 = pop();
n2 = pop();
switch(*e) {
case '+': {
n3 = n1 + n2;
break;
}
case '-': {
n3 = n2 - n1;
break;
}
case '*': {
n3 = n1 * n2;
break;
}
case '/': {
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
Output:
Enter the expression :: 23+4/
--------------------------------
Process exited after 5.371 seconds with return value 0
Press any key to continue . . .
int main() {
struct tnode *root = NULL;
int choice, item, n, i;
do {
printf("\n\nBinary Search Tree Operations\n");
printf("\n1. Creation of BST");
printf("\n2. Traverse in Inorder");
printf("\n3. Traverse in Preorder");
printf("\n4. Traverse in Postorder");
printf("\n5. Exit\n");
printf("\nEnter Choice : ");
scanf("%d",&choice);
switch(choice) {
case 1:
root = NULL;
printf("\n\nBST for How Many Nodes ? ");
scanf("%d",&n);
for(i = 1; i <= n; i++) {
printf("\nEnter data for node %d : ", i);
scanf("%d",&item);
root = CreateBST(root,item);
}
printf("\nBST with %d nodes is ready to Use!!\n", n);
break;
case 2:
printf("\nBST Traversal in INORDER \n");
Inorder(root);
break;
case 3:
printf("\nBST Traversal in PREORDER \n");
Preorder(root);
break;
case 4:
printf("\nBST Traversal in POSTORDER \n");
Postorder(root);
break;
case 5:
printf("\n\n Terminating \n\n");
break;
default:
printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
} while(choice != 5);
return 0;
}
return(root);
}
}
1. Creation of BST
2. Traverse in Inorder
3. Traverse in Preorder
4. Traverse in Postorder
5. Exit
Enter Choice : 1
1. Creation of BST
2. Traverse in Inorder
3. Traverse in Preorder
4. Traverse in Postorder
5. Exit
Enter Choice : 2
1. Creation of BST
2. Traverse in Inorder
3. Traverse in Preorder
4. Traverse in Postorder
5. Exit
Enter Choice : 3
1. Creation of BST
2. Traverse in Inorder
3. Traverse in Preorder
4. Traverse in Postorder
5. Exit
Enter Choice : 4
1. Creation of BST
2. Traverse in Inorder
3. Traverse in Preorder
4. Traverse in Postorder
5. Exit
Enter Choice : 5
Terminating
--------------------------------
Process exited after 48.98 seconds with return value 0
Press any key to continue . . .
struct btnode {
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
int flag = 1;
void main() {
int ch;
printf("\nOPERATIONS ---");
printf("\n1 - Insert an element into tree\n");
printf("2 - Delete an element from the tree\n");
printf("3 - Inorder Traversal\n");
printf("4 - Preorder Traversal\n");
printf("5 - Postorder Traversal\n");
printf("6 - Exit\n");
while(1) {
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
/* To create a node */
void create() {
int data;
if (root == NULL) {
printf("No elements in a tree to delete");
return;
}
printf("Enter the data to be deleted : ");
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
/* To delete a node */
void delete1(struct btnode *t) {
int k;
} else {
t1->r = t->l;
}
t = NULL;
free(t);
return;
}
OPERATIONS ---
1 - Insert an element into tree
2 - Delete an element from the tree
3 - Inorder Traversal
4 - Preorder Traversal
5 - Postorder Traversal
6 - Exit
--------------------------------
Process exited after 83.36 seconds with return value 0
Press any key to continue . . .