Professional Documents
Culture Documents
Practical: 1
Practical: 1
Practical: 1
(1) Implement a program for stack that performs following operations
using array. (a)PUSH,(b)POP,(c)PEEP,(d)DISPLAY
Program:
#include<stdio.h>
main()
{
int TOP=-1,MAX,choice,i;
int stack[MAX];
while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: if(TOP==MAX-1)
{
printf("Stack is Full!\n");
break;
}
else
{
printf("Enter the value:");
scanf("%d",&stack[++TOP]);
break;
}
case 2:
if(TOP==-1)
{
Sem Page|1
Data Structure 190170107114
printf("Stack is Empty!\n");
break;
}
else
{
printf("Value %d has been removed from Stack.\n",stack[TOP]);
stack[TOP--]=0;
break;
}
case 3:
if(TOP==-1)
{
printf("Stack is Empty!\n");
break;
}
else
{
printf("Peek value is %d\n",stack[TOP]);
break;
}
case 4:
if(TOP==-1)
{
printf("Stack is Empty!\n");
break;
}
else
{
printf("Your Stack :- ");
for(i=TOP;i>=0;i--)
{
printf("%d\n",stack[i]);
}
break;
}
case 5:
exit(0);
default:printf("Invalid Input!\n");
}
}
getch();
}
Sem Page|2
Data Structure 190170107114
Output:
Menu - Press
1. PUSH
2. POP
3. PEEK
4. Displ
ay
5.Exit
Enter your choice :
1 Enter the
value:68 Enter
your choice : 1
Enter the value:56
Enter your choice :
2
56 has been removed from
Stack. Enter your choice : 3
Peek value is 68.
Enter your choice :
4 Your Stack :-
68
Sem Page|3
Data Structure 190170107114
Program:
#include<stdio.h>
//Global declaration
int top=-1;
char stack[100];
char pop(void);
void push(char);
int precedence(char);
//Main Function
main()
{
char input[100];
char output[100];
int outvar=0,i;
else if(input[i]=='(')
push(input[i]);
else if(input[i]==')')
{
while(stack[top]!='(')
{
output[outvar++]=pop();
Sem Page|4
Data Structure 190170107114
}
top--;
}
else
{
while((precedence(input[i])<=precedence(stack[top])) && (top!=-1))
output[outvar++]=pop();
push(input[i]);
}
}
}
while(1)
{
if(top>=0) output[outvar+
+]=pop();
else
break;
}
output[outvar]='\0';
printf("Post Expression: %s",output);
}
// Extra functions
char pop(void)
{
char val;
val=stack[top--];
return(val);
}
void push(char val)
{
stack[++top]=val;
Sem Page|5
Data Structure 190170107114
else if(val=='(')
return(0);
}
Output:
Sem Page|6
Data Structure 190170107114
Program:
#include<stdio.h>
int top=-1;
float stack[100];
main()
{
int i;
float ans,n1,n2;
char input[100];
for(i=0;input[i]!='\0';i++)
{
if(input[i]>='0' && input[i]<='9')
stack[++top]= input[i]-'0';
else
{
n1=stack[top--];
n2=stack[top--];
switch(input[i])
{
case '+':
ans=n2+n1;
break;
case '-':
ans=n2-n1;
break;
case '*':
ans=n2*n1;
break;
case '/':
ans=n2/n1;
break;
case '%': ans=(int)n2%
(int)n1; break;
default:
printf("\nInvalid Operator!");
}
Sem Page|7
Data Structure 190170107114
stack[++top]=ans;
}
}
printf("Your answer is %f",stack[top]);
getch();
}
Output:
Sem Page|8
Data Structure 190170107114
Program:
#include<stdio.h>
void TH(int,char,char,char);
main()
{
int n;
printf("Enter number of Disks:");
scanf("%d",&n);
printf("\nFollow these steps to solve Tower of Hanoi\n");
TH(n,'A','B','C');
getch();
}
Output:
Sem Page|9
Data Structure 190170107114
Practical: 2
(1) Write a program to implement QUEUE using arrays that
performs following operations (a)INSERT (b)DELETE (c)DISPLAY
Program:
#include<stdio.h>
main()
{
int front=-1,rear=-1;
int n,choice,val,i;
int queue[n];
while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(rear==n-1)
printf("Queue is Full!\n");
else
{
if(front==-1 && rear==-1)
front=rear=0;
else
rear++;
printf("Enter value : ");
scanf("%d",&val);
queue[rear]=val;
Sem Page|
Data Structure 190170107114
}
break;
case 2:
if(front==-1 || front>rear)
printf("Queue is Empty!\n");
else
printf("%d has been deleted from Queue.\n",queue[front++]);
break;
case 3:
if(rear==-1 || front>rear)
printf("Queue is Empty!\n");
else
printf("Peek value is %d\n",queue[front]);
break;
case 4:
if(rear==-1 || front>rear)
printf("Queue is Empty!\n");
else
{
printf("Your Queue :-\n");
for(i=front;i<=rear;i++)
printf("%d\n",queue[i]);
}
break;
case 5:
exit(0);
default:
printf("Invalid Input!");
}
}
}
Sem Page|
Data Structure 190170107114
Output:
Menu
1. Insert an Element
2. Remove an Element
3. Peek
4. Display
5. Exit
Enter your choice :
1 Enter value : 68
Enter your choice :
1 Enter value : 64
Enter your choice :
2
68 has been deleted from
Queue. Enter your choice : 3
Peek value is 64
Enter your choice :
4 Your Queue :-
64
Sem Page|
Data Structure 190170107114
Program:
#include<stdio.h>
main()
{
int front=-1,rear=-1;
int n,choice,val,i;
int queue[n];
while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if((front==0 && rear==n-1) || front==rear+1)
printf("Queue is Full!\n");
else
{
if(front==-1 && rear==-1)
front=rear=0;
else if(front!=0 && rear==n-1)
rear=0;
else
rear++;
printf("Enter value : ");
scanf("%d",&val);
queue[rear]=val;
}
Sem Page|
Data Structure 190170107114
break;
case 2:
if(front==-1)
printf("Queue is Empty!\n");
else if(front==n-1)
{
printf("%d has been removed from Queue.\n",queue[front]);
front=0;
}
else if(front==rear)
{
printf("%d has been removed from Queue.\n",queue[front]);
front=rear=-1;
}
else
printf("%d has been removed from Queue.\n",queue[front++]);
break;
case 3:
if(front==-1)
printf("Queue is Empty!\n");
else
printf("Peek value is %d.\n",queue[front]);\
break;
case 4:
if(front==-1)
printf("Queue is Empty!\n");
else
{
printf("Your Queue :- \n");
if(front<rear)
{
for(i=front; i<=rear; i++)
printf("%d\n",queue[i]);
}
else
{
for(i=front;i<n;i++) printf("%d\
n",queue[i]);
for(i=0;i<=rear;i++) printf("%d\
n",queue[i]);
}
}
break;
Sem Page|
Data Structure 190170107114
case 5:
exit(0);
default :
printf("Invalid Input!\n");
}
}
}
Output:
Menu
1. Insert an Element
2. Remove an Element
3. Peek
4. Display
5. Exit
Enter your choice :
1 Enter value : 45
Enter your choice :
1 Enter value : 67
Enter your choice :
1 Enter value : 78
Enter your choice :
1 Queue is Full!
Enter your choice : 2
45 has been removed from
Queue. Enter your choice : 1
Enter value : 89
Enter your choice :
3 Peek value is 67.
Enter your choice :
4 Your Queue :-
67
78
89
Enter your choice :
1 Queue is Full!
Enter your choice : 5
Sem Page|
Data Structure 190170107114
Practical: 3
(1) Write a menu driven program to implement following operations on the
singly linked list. (a) Insert a node at the front of the linked list. (b) Insert a
node at the end of the linked list. (c) Insert a node such that linked list is in
ascending order. (according to info. Field)(d) Delete a first node of the linked
list. (e) Delete a node before specified position. (f)Delete a node after
specified position.
Program:
#include<stdio.h>
#include<stdlib.h>
struct linked_list
{
int data;
struct linked_list *next;
};
void main()
{
int choice;
node *head,*start;
node *new_node;
head=(node*)malloc(sizeof(node));
start=head;
Sem Page|
Data Structure 190170107114
while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("(Enter -1 to stop Insertion)\n");
create_linked_list(start);
break;
case 2:
display_linked_list(start);
printf("\n");
break;
case 3:
printf("Total Elements : %d\n",count_elements(start));
break;
case 4:
start=insert_at_beginning(start);
break;
case 5:
head=insert_at_end(start);
break;
case 6:
head=insert_after_key(start);
break;
case 7:
if(insert_before_key(start)==NULL)
{
start=insert_at_beginning(start);
}
Sem Page|
Data Structure 190170107114
break;
case 8:
start=insert_ascending(start);
break;
case 9:
start=delete_at_beginning(start);
break;
case 10:
head=delete_at_end(start);
break;
case 11:
head=delete_after_key(start);
break;
case 12:
if(delete_before_key(start)==NULL)
{
start=delete_at_beginning(start);
}
break;
case 13:
exit(0);
default:
printf("Invalid Input!\n");
}
}
}
if(list->data==-1)
{
list->next=NULL;
}
else
{
list->next=(node*)malloc(sizeof(node));
create_linked_list(list->next);
}
}
Sem Page|
Data Structure 190170107114
{
if(list->next!=NULL && list->next->next!=NULL)
{
printf("%d-->",list->data);
display_linked_list(list->next);
}
else if(list->next!=NULL && list->next->next==NULL)
{
printf("%d",list->data);
}
return;
}
new_node->next=list;
list=new_node;
return list;
}
while(list->next!=NULL)
{
if(list->next->next==NULL)
{
new_node->next=list->next;
list->next=new_node;
break;
}
list=list->next;
Sem Page|
Data Structure 190170107114
}
return list;
}
if(list->data==key)
{
new_node->next=list->next;
list->next=new_node;
}
else
{
while(list->next!=NULL)
{
if(list->next->data==key)
{
new_node->next=list->next->next;
list->next->next=new_node;
break;
}
list=list->next;
}
}
return list;
}
if(list->data==key)
{
return NULL;
}
else
{
Sem Page|
Data Structure 190170107114
new_node=(node*)malloc(sizeof(node));
printf("Enter value : ");
scanf("%d",&new_node->data);
while(list->next!=NULL)
{
if(list->next->data==key)
{
new_node->next=list->next;
list->next=new_node;
break;
}
list=list->next;
}
return list;
}
}
Sem Page|
Data Structure 190170107114
while(list->next!=NULL)
{
if(list->data==key)
{
list->next=list->next->next;
return(list);
}
list=list->next;
}
}
if(list->data==key)
{
printf("There is no node before the Key Value's Node!\n");
return list;
}
else if(list->next->data==key)
{
return NULL;
}
else
{
while(list->next!=NULL)
{
if(list->next->next->data==key)
{
list->next=list->next->next;
return(list->next);
}
list=list->next;
}
}
}
Sem Page|
Data Structure 190170107114
node *new_node;
new_node=(node*)malloc(sizeof(node));
if(list->data>=new_node->data)
{
new_node->next=list;
list=new_node;
return list;
}
else if(list->next->data>=new_node->data)
{
new_node->next=list->next;
list->next=new_node;
return (list);
}
else
{
while(list->next!=NULL)
{
if(list->next->next->data>=new_node->data)
{
new_node->next=list->next->next;
list->next->next=new_node;
return(list->next->next);
}
list=list->next;
}
}
Sem Page|
Data Structure 190170107114
Output:
Menu
1. Create Linked List
2. Display Linked List
3. Count Total Elements
4. Insert At Beginning
5. Insert At End
6. Insert After The Key Value
7. Insert Before The Key Value
8. Insert Value at Ascending Order
9. Delete At Beginning
10. Delete At End
11. Delete After The Key Value
12. Delete Before The Key Value
13. Exit
Enter your choice : 1
(Enter -1 to stop Insertion)
Enter value : 10
Enter value : 11
Enter value : 12
Enter value : 13
Enter value : 14
Enter value : 15
Enter value : -1
Enter your choice : 2
10-->11-->12-->13-->14-->15
Enter your choice : 3
Total Elements : 6
Enter your choice : 4
Enter value : 9
Enter your choice : 2
9-->10-->11-->12-->13-->14-->15
Enter your choice : 5
Enter value : 16
Enter your choice : 2
9-->10-->11-->12-->13-->14-->15-->16
Enter your choice : 6
Enter Key Value : 12
Enter value : 17
Enter your choice : 2
9-->10-->11-->12-->17-->13-->14-->15-->16
Enter your choice : 7
Enter Key Value : 12
Enter value : 18
Enter your choice : 2
9-->10-->11-->18-->12-->17-->13-->14-->15-->16
Sem Page|
Data Structure 190170107114
Sem Page|
Data Structure 190170107114
Program:
#include<conio.h>
#include<stdio.h>
struct linked_list
{
int data;
struct linked_list *next;
};
node *start=NULL;
void push(void);
void pop(void);
void display(void);
void main()
{
int choice;
while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
printf("\n");
break;
Sem Page|
Data Structure 190170107114
case 4:
exit(0);
default :
printf("Invalid Input!\n");
}
}
}
void push(void)
{
node *new_node;
if(start == NULL)
{
new_node=(node*)malloc(sizeof(node));
new_node->next = NULL;
start = new_node;
}
else
{
new_node=(node*)malloc(sizeof(node));
new_node->next = start;
start=new_node;
}
printf("%d has been stored into Stack.\n",new_node->data);
}
void pop(void)
{
node *ptr;
ptr=start;
if(ptr==NULL)
printf("Stack is Empty!\n");
else
{
start = ptr ->next;
printf("%d has been removed from Stack.\n",ptr->data);
free(ptr);
}
}
Sem Page|
Data Structure 190170107114
void display()
{
node *ptr;
ptr=start;
if(ptr==NULL)
printf("Stack is Empty!");
else
{
while(ptr!=NULL)
{
if(ptr->next==NULL)
printf("%d",ptr-
>data);
else
{
printf("%d-->",ptr->data);
if(ptr->next->next==NULL)
{
printf("%d",ptr->next->data);
break;
}
}
ptr=ptr->next;
}
Sem Page|
Data Structure 190170107114
Output:
Menu
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice :
1 Enter value : 12
12 has been stored into
Stack. Enter your choice : 1
Enter value : 13
13 has been stored into
Stack. Enter your choice : 1
Enter value : 14
14 has been stored into
Stack. Enter your choice : 3
14-->13-->12
Enter your choice : 2
14 has been removed from
Stack. Enter your choice : 3
13-->12
Enter your choice : 2
13 has been removed from
Stack. Enter your choice : 2
12 has been removed from
Stack. Enter your choice : 3
Stack is Empty!
Enter your
choice : 2 Stack is
Empty!
Sem Page|
Data Structure 190170107114
Program:
#include<conio.h>
#include<stdio.h>
struct linked_list
{
int data;
struct linked_list *next;
};
node *front=NULL;
node *rear=NULL;
void insert_element(void);
void delete_element(void);
void display(void);
void main()
{
int choice;
while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete_element();
break;
case 3:
display();
printf("\n");
Sem Page|
Data Structure 190170107114
break;
case 4:
exit(0);
default :
printf("Invalid Input\n");
}
}
}
void insert(void)
{
node *new_node;
new_node=(node *)malloc(sizeof(node));
if(front==NULL && rear==NULL)
{
printf("Enter value : ");
scanf("%d",&new_node->data);
new_node->next = NULL;
front = new_node;
rear = new_node;
}
else
{
printf("Enter value : ");
scanf("%d",&new_node->data);
new_node->next = NULL;
rear->next = new_node;
rear=new_node;
}
}
void delete_element(void)
{
if(front==NULL && rear==NULL)
{
printf("Queue is Empty!\n");
}
else
{
node *ptr;
if(front==rear)
{
ptr=front;
front=front->next;
Sem Page|
Data Structure 190170107114
ptr->next=NULL;
printf("%d has been deleted from Queue.\n",ptr->data);
free(ptr);
front = NULL;
rear = NULL;
}
else
{
ptr=front;
front = front->next;
ptr->next=NULL;
printf("%d has been deleted from Queue.\n",ptr->data);
free(ptr);
}
}
}
void display(void)
{
node *ptr;
ptr=front;
if(front== NULL && rear == NULL)
{
printf("Queue is Empty!");
}
else
{
while(ptr!=NULL)
{
if(ptr->next==NULL)
printf("%d",ptr-
>data);
else
{
printf("%d-->",ptr->data);
if(ptr->next->next==NULL)
{
printf("%d",ptr->next->data);
break;
}
}
ptr=ptr->next;
}
}
}
Sem Page|
Data Structure 190170107114
Output:
Menu
1. Insert an Element
2. Delete an Element
3. Display
4. Exit
Enter your choice :
1 Enter value : 1
Enter your choice :
1 Enter value : 2
Enter your choice :
1 Enter value : 3
Enter your
choice : 3 1-->2--
>3
Enter your choice : 2
1 has been deleted from
Queue. Enter your choice : 3
2-->3
Enter your choice : 2
2 has been deleted from
Queue. Enter your choice : 2
3 has been deleted from
Queue. Enter your choice : 2
Queue is Empty!
Enter your choice :
3 Queue is Empty!
Sem Page|
Data Structure 190170107114
Practical: 4
(1) Write a program to implement following operations on the doubly linked list.
(a) Insert a node at the front of the linked list. (b) Insert a node at the end of
the linked list. (c) Delete a last node of the linked list. (d) Delete a node before
specified position.
Program:
#include <stdio.h>
#include <stdlib.h>
struct linked_list
{
struct linked_list *prev;
int data;
struct linked_list *next;
};
void create_linked_list(void);
void display_linked_list(void);
void count_elements(void);
void insert_at_beginning(void);
void insert_at_end(void);
void insert_after_key(void);
void insert_before_key(void);
void
delete_at_beginning(void);
void delete_at_end(void);
void delete_after_key(void);
void delete_before_key(void);
void main()
{
int choice;
printf("----------Menu---------\n");
printf("1. Input Data in Linked List\n");
printf("2. Display List\n");
printf("3. Count Elements of Linked List\n");
printf("4. Insert Element at Beginning\n");
printf("5. Insert Element at End\n");
printf("6. Insert Element After The Key Value\n");
Sem Page|
Data Structure 190170107114
switch (choice)
{
case 1:
create_linked_list();
break;
case 2:
display_linked_list();
break;
case 3:
count_elements();
break;
case 4:
insert_at_beginning();
break;
case 5:
insert_at_end();
break;
case 6:
insert_after_key();
break;
case 7:
insert_before_key();
break;
case 8:
delete_at_beginning();
break;
case 9:
delete_at_end();
break;
case 10:
delete_after_key();
Sem Page|
Data Structure 190170107114
break;
case 11:
delete_before_key();
break;
case 12:
printf("See you soon!\n");
exit(0);
default:
printf("Invalid Input!\n");
}
}
}
void create_linked_list(void)
{
int n;
printf("Enter value : ");
scanf("%d", &n);
if (n == -1)
return;
else
{
node *new_node;
new_node = (node *)malloc(sizeof(node));
if (start == NULL)
{
start = new_node;
head = new_node;
new_node->prev = start;
new_node->data = n;
new_node->next = NULL;
}
else
{
new_node->prev = head->next;
head->next = new_node;
new_node->next = NULL;
new_node->data = n;
head = new_node;
}
create_linked_list();
}
}
void display_linked_list(void)
{
Sem Page|
Data Structure 190170107114
if (start == NULL)
{
printf("List is Empty!\n");
}
else if (start->next == NULL)
{
printf("|%d|\n", start->data);
}
else
{
while (temp != NULL)
{
printf("|%d|<-->", temp->data);
if (temp->next->next == NULL)
{
printf("|%d|", temp->next->data);
break;
}
temp = temp->next;
}
printf("\n");
}
}
void count_elements(void)
{
node *temp = start;
int count = 0;
if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
while (temp != NULL)
{
count++;
temp = temp->next;
}
printf("Total number of Elements in List : %d\n", count);
}
}
void insert_at_beginning(void)
{
node *new_node;
new_node = (node *)malloc(sizeof(node));
Sem Page|
Data Structure 190170107114
new_node->next = start;
start = new_node;
new_node->prev = start;
}
void insert_at_end(void)
{
node *new_node;
new_node = (node *)malloc(sizeof(node));
head->next = new_node;
new_node->prev = head;
new_node->next = NULL;
head = new_node;
}
void insert_after_key(void)
{
node *new_node, *temp = start;
new_node = (node *)malloc(sizeof(node));
if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
int key;
printf("Enter Key Value : ");
scanf("%d", &key);
new_node->next = temp->next;
temp->next = new_node;
new_node->prev = temp;
return;
}
else if (temp->data == key && temp->next == NULL)
{
Sem Page|
Data Structure 190170107114
new_node->next = NULL;
new_node->prev = head;
head->next = new_node;
head = new_node;
return;
}
temp = temp->next;
}
printf("Invalid key!\n");
}
free(new_node);
}
void insert_before_key(void)
{
node *new_node, *temp = start;
new_node = (node *)malloc(sizeof(node));
if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
int key;
printf("Enter Key value : ");
scanf("%d", &key);
new_node->next = start;
start = new_node;
new_node->prev = start;
return;
}
else if (temp->next->data == key)
{
printf("Enter value : ");
scanf("%d", &new_node->data);
new_node->next = temp->next;
new_node->prev = temp;
(temp->next)->prev = new_node;
Sem Page|
Data Structure 190170107114
temp->next = new_node;
return;
}
else if (temp->next->data != key && temp->next->next ==
NULL) break;
temp = temp->next;
}
printf("Invalid Key!\n");
}
free(new_node);
}
void delete_at_beginning(void)
{
node *store;
if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
if (start->next == NULL)
{
store = start;
start = NULL;
head = NULL;
}
else
{
store = start;
start = start->next;
start->prev = start;
}
printf("%d has Deleted from List!\n", store->data);
free(store);
}
}
void delete_at_end(void)
{
node *store, *temp = start;
if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
if (start->next == NULL)
{
Sem Page|
Data Structure 190170107114
store = start;
start = NULL;
head = NULL;
}
else
{
while (temp != NULL)
{
if (temp->next->next == NULL)
{
store = temp->next;
temp->next = NULL;
head = temp;
break;
}
temp = temp->next;
}
}
printf("%d has Deleted from List!\n", store->data);
free(store);
}
}
void delete_after_key(void)
{
node *temp = start, *store;
if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
int key;
printf("Enter Key Value : ");
scanf("%d", &key);
Sem Page|
Data Structure 190170107114
break;
}
else if (temp->data == key && temp->next == NULL)
{
printf("Node doesn't exist after key value!\n");
return;
}
else if (temp->data != key && temp->next == NULL)
{
printf("Invalid Key!\n");
return;
}
temp = temp->next;
}
printf("%d has Deleted from List!\n", store->data);
free(store);
}
}
void delete_before_key(void)
{
node *temp = start, *store;
if (start == NULL)
{
printf("List is Empty!\n");
}
else
{
int key;
printf("Enter Key Value : ");
scanf("%d", &key);
Sem Page|
Data Structure 190170107114
start->prev = start;
break;
}
Sem Page|
Data Structure 190170107114
Output:
Menu
1. Input Data in Linked List
2. Display List
3. Count Elements of Linked List
4. Insert Element at Beginning
5. Insert Element at End
6. Insert Element After The Key Value
7. Insert Element Before The Key Value
8. Delete Element at Beginning
9. Delete Element at End
10. Delete Element After The Key Value
11. Delete Element Before The Key Value
12. Exit
Enter your choice :
1 Enter value : 1
Enter value :
2 Enter value
: 3 Enter
value : 4
Enter value :
5 Enter value
: -1
Enter your choice : 2
|1|<-->|2|<-->|3|<-->|4|<-->|
5| Enter your choice : 4
Enter value : 0
Enter your choice : 2
|0|<-->|1|<-->|2|<-->|3|<-->|4|<-->|5|
Enter your choice : 5
Enter value : 6
Enter your choice : 2
|0|<-->|1|<-->|2|<-->|3|<-->|4|<-->|5|<-->|6|
Enter your choice : 9
6 has Deleted from
List! Enter your
choice : 2
|0|<-->|1|<-->|2|<-->|3|<-->|4|<-->|
5| Enter your choice : 11
Enter Key Value : 1
0 has Deleted from
List! Enter your
Sem Page|
Data Structure 190170107114
Practical: 5
(1) Write a program to implement following operations on the circular linked list.
(a) Insert a node at the end of the linked list. (b) Insert a node before
specified position. (c) Delete a first node of the linked list. (d) Delete a
node after specified position.
Program:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct linked_list
{
int data;
struct linked_list *next;
};
node *start=NULL;
void insert_at_front(int);
void insert_before_key(int,int);
void delete_after_key(int);
void delete_at_front(void);
void display(void);
void main()
{
int choice;
int value,prev_value,next_value;
printf("\n------------Menu-----------\n");
printf("1. Insert Element at Front\n");
printf("2. Insert Element Before Key Value\
n"); printf("3. Delete Element at Front\n");
printf("4. Delete Element After Key Value\n");
printf("5. Display\n");
printf("6. Exit\n");
while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);
Sem Page|
Data Structure 190170107114
switch(choice)
{
case 1:
printf("Enter value : ");
scanf("%d",&value);
insert_at_front(value);
break;
case 2:
printf("Enter Key Value : ");
scanf("%d",&next_value);
printf("Enter value : ");
scanf("%d",&value);
insert_before_key(next_value,value);
break;
case 3:
delete_at_front();
break;
case 4:
printf("Enter Key Value : ");
scanf("%d",&value);
delete_after_key(value);
break;
case 5:
display();
printf("\n");
break;
case 6:
exit(0);
default :
printf("Invalid Input!\n");
}
}
}
Sem Page|
Data Structure 190170107114
else
{
new_node=(node*)malloc(sizeof(node));
new_node->data = data;
ptr=start;
while(ptr->next!=start)
{
ptr=ptr->next;
}
ptr->next=new_node;
new_node->next=start;
start=new_node;
}
}
/*void display()
{
node *ptr;
if(start == NULL)
{
printf("Queue is Empty!\n");
}
else
{
ptr=start;
while(ptr->next!= start )
{
printf("\t %d",ptr->data);
ptr = ptr->next;
}
printf("\t %d \n",ptr->data);
}
}*/
void display(void)
{
node *ptr;
ptr=start;
if(start==NULL)
{
printf("Queue is Empty!");
}
else
{
do
{
if(ptr->next==start)
printf("%d",ptr-
>data);
else
Sem Page|
Data Structure 190170107114
{
printf("%d-->",ptr->data);
if(ptr->next->next==NULL)
{
printf("%d",ptr->next->data);
break;
}
}
ptr=ptr->next;
}while(ptr!=start);
}
}
new_node=(node*)malloc(sizeof(node));
new_node->data = data;
temp = start ;
while(temp->next->data!=next_data)
{
temp = temp->next;
}
if(temp->next==start)
{
insert_at_front(data);
}
else
{
new_node->next = temp->next;
temp->next = new_node;
}
void delete_at_front()
{
node *ptr;
if(start==NULL)
{
printf("Queue is Empty!\n");
}
else if(start->next == start)
{
start = NULL;
free(start);
Sem Page|
Data Structure 190170107114
}
else
{
ptr=start;
while(ptr->next != start)
{
ptr=ptr->next;
}
ptr->next = start->next;
free(start);
start=ptr->next;
}
}
ptr=start;
while(ptr->next!=start)
{
if(ptr->data!=key)
ptr=ptr->next;
else
break;
count++;
}
if(ptr->next==NULL && count!=0 && ptr->data!=key || ptr->next==NULL && count==0 && ptr-
>data!=key)
{
printf("Key doesn't exist!\n");
return;
}
else if(ptr->next==start)
{
if(ptr==start)
{
printf("No node after Key!\n");
return;
}
after = ptr->next;
new_start = ptr->next->next;
ptr->next = after->next;
after->next = NULL;
start = new_start;
free(after);
Sem Page|
Data Structure 190170107114
}
else
{
after = ptr->next;
ptr->next = after-
>next; after->next =
NULL; free(after);
}
}
Output:
Menu
1. Insert Element at Front
2. Insert Element Before Key Value
3. Delete Element at Front
4. Delete Element After Key Value
5. Display
6. Exit
Enter your choice :
1 Enter value : 1
Enter your choice :
1 Enter value : 2
Enter your choice :
1 Enter value : 3
Enter your
choice : 5 3-->2--
>1
Enter your choice :
2 Enter Key Value :
1 Enter value : 4
Enter your
choice : 5 3-->2--
>4-->1
Enter your
choice : 3 Enter
your choice : 5 2--
>4-->1
Enter your
choice : 4 Enter
Sem Page|
Data Structure 190170107114
Practical: 6
(1) Write a program which create binary search tree.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct bst
{
int data;
struct bst *left;
struct bst *right;
};
void getnode(void);
void insert(node *);
void create(void);
void inorder(node *);
void delete (void);
void search1(node *, int);
void delete1(node *);
int smallest(node *);
int largest(node *);
void main()
{
int choice;
printf(" Menu \n");
printf("1. Insert an element into tree\n");
printf("2. Delete an element from the tree\n");
printf("3. Inorder Traversal\n");
printf("4. Exit\n");
while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &choice);
Sem Page|
Data Structure 190170107114
switch (choice)
{
case 1:
create();
break;
case 2:
delete ();
break;
case 3:
inorder(root);
break;
case 4:
printf("See you soon!\n");
exit(0);
default:
printf("Invalid Input!\n");
}
}
}
void getnode(void)
{
int data;
Sem Page|
Data Structure 190170107114
}
}
if (temp->data > root->data)
{
if (root->right == NULL)
{
root->right = temp;
}
else
{
insert(root->right);
}
}
}
void create(void)
{
getnode();
if (root == NULL)
{
root = temp;
}
else
{
insert(root);
}
}
if (temp->right != NULL)
inorder(temp->right);
}
Sem Page|
Data Structure 190170107114
if (root == NULL)
{
printf("Tree is Empty!\n");
return;
}
printf("Enter Value : ");
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
}
/* To delete a node */
void delete1(node *temp)
{
int k;
Sem Page|
Data Structure 190170107114
return;
}
Sem Page|
Data Structure 190170107114
}
else
{
k = largest(temp->left);
flag = 2;
}
search1(root, k);
temp->data = k;
}
}
Sem Page|
Data Structure 190170107114
Output:
Menu
1. Insert an element into tree
2. Delete an element from the tree
3. Inorder Traversal
4. Exit
Sem Page|
Data Structure 190170107114
Program:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
};
int main()
{
int choice;
char ans = 'n';
node *newnode;
root = NULL;
printf("\n------------Menu-----------\n");
printf("1. Create\n");
printf("2. Inorder\n");
printf("3. Preorder\n");
printf("4. Postorder\n");
printf("5. Exit\n");
while(1)
{
printf("\nEnter Your Choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
root = NULL;
Sem Page|
Data Structure 190170107114
do
{
newnode = getnode(); printf("\
nEnter Value : "); scanf("%d",
&newnode->data);
if (root == NULL)
root = newnode;
else
insert(root, newnode);
case 2:
inorder(root);
break;
case 3:
preorder(root);
break;
case 4:
postorder(root);
break;
case 5:
printf(“See you Soon!\n”);
exit(0);
default:
printf("Invalid Input!\n");
}
}
getch();
}
// Inorder Traversal
void inorder(node *root)
{
if (root == NULL)
return;
inorder(root->left);
printf("%d ->", root->data);
inorder(root->right);
}
// preorderTraversal traversal
Sem Page|
Data Structure 190170107114
// postorderTraversal traversal
void postorder(node *root)
{
if (root == NULL)
return;
postorder(root->left);
postorder(root->right);
printf("%d ->", root->data);
}
node *getnode(void)
{
temp = (node *)malloc(sizeof(node));
temp->left = NULL;
temp->right = NULL;
return temp;
}
Sem Page|
Data Structure 190170107114
{
insert(root->left, new);
}
}
}
Output:
Menu
1. Create
2. Inorder
3. Preorder
4. Postorder
5. Exit
1 Enter Value : 1
Want to add more elements?(y/n) y
Enter Value : 2
Insert in left/right of 1 ?(l/r) l
Want to add more elements?(y/n) y
Enter Value : 3
Insert in left/right of 1 ?(l/r) r
Want to add more elements?(y/n) n
Sem Page|
Data Structure 190170107114
Practical: 8
(1) Write a program to implement Linear Search.
Program:
#include<stdio.h>
void main()
{
int n,search,position=0;
int a[n];
for(int i=0;i<n;i++)
{
printf("Enter Value : ");
scanf("%d",&a[i]);
}
printf("\n-----------Search Result----------\n");
for(int i=0;i<n;i++)
{
if(a[i]==search)
{
position++;
printf("Position : %d\n",i+1);
}
}
if(position==0)
{
printf("Value not Found!\n");
}
else
{
printf("Total Result : %d\n",position);
}
}
Sem Page|
Data Structure 190170107114
Output:
Enter Size of
Array : 5 Enter
Value : 12 Enter
Value : 20 Enter
Value : 18 Enter
Value : 56 Enter
Value : 29 Search
Value : 12
Sem Page|
Data Structure 190170107114
Program:
#include <stdio.h>
void main()
{
int n, temp, position = 0;
printf("Enter Size of Array : ");
scanf("%d", &n);
int a[n];
// Insertion
for (int i = 0; i < n; i++)
{
printf("Enter value : ");
scanf("%d", &a[i]);
}
// Sorting
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] >= a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("\n---------------Sorted Array--------------\n");
printf("Position\tValue\n");
for (int i = 0; i < n; i++)
{
printf("%d\t\t%d\n", i + 1, a[i]);
}
//Searching
int search;
printf("Search Value : ");
scanf("%d", &search);
Sem Page|
Data Structure 190170107114
printf("\n---------------Search Result--------------\n");
Sem Page|
Data Structure 190170107114
Output:
Sorted Array
Position Value
1 10
2 12
3 26
4 28
5 36
Search Value : 26
Sem Page|
Data Structure 190170107114
Practical: 9
(1) Write a program to implement Quick Sort.
Program:
#include <stdio.h>
#include <conio.h>
#define size 100
void main()
{
int a[size], i, n;
printf("Enter size of Array : ");
scanf("%d", &n);
quick_sort(a, 0, n - 1);
printf("------------Sorted Array-----------\n");
Sem Page|
Data Structure 190170107114
if (loc == right)
flag = 1;
if (flag != 1)
{
while ((array[loc] >= array[left]) && (loc != left))
left++;
if (loc == left)
flag = 1;
Sem Page|
Data Structure 190170107114
Output:
Enter size of
Array : 5 Enter
Value : 5
Enter
Value : 4
Enter
Value : 3
Enter
Value : 2
Enter
Value : 1
Sorted Array 1
Sem Page|
Data Structure 190170107114
Program:
#include <stdio.h>
void main()
{
int n, i;
printf("Enter size of Array : ");
scanf("%d", &n);
int a[n];
//sorting
merge_sort(a, 0, n - 1);
printf("\n--------------Sorted Array-------------\n");
for (i = 0; i < n; i++)
printf("%d\n", a[i]);
}
Sem Page|
Data Structure 190170107114
Sem Page|
Data Structure 190170107114
Output:
Enter size of
Array : 5 Enter
value : 5
Enter
value : 4
Enter
value : 3
Enter
value : 2
Enter
value : 1
Sorted Array 1
Sem Page|
Data Structure 190170107114
Program:
#include <stdio.h>
void main()
{
int n, flag = 0;
printf("Enter size of Array : ");
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++)
{
printf("Enter value : ");
scanf("%d", &a[i]);
}
for (int i = n; i >= 0; i--)
{
for (int j = 0; j < i; j++)
{
if (a[j] > a[j + 1])
{
flag = 1;
int temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
if (flag == 0)
break;
}
if (flag == 0)
{
printf("Array is already Sorted!\n");
}
else
{
printf("Sorted Array : \n");
for (int i = 0; i < n; i++)
{
printf("%d\n", a[i]);
}
}
}
Sem Page|
Data Structure 190170107114
Output:
Enter size of
Array : 5 Enter
value : 132 Enter
value : 34 Enter
value : 34 Enter
value : 23 Enter
value : 1
Sorted Array :
1
23
34
34
132
Second Output
Sem Page|