You are on page 1of 38

ADITYA AJIT PATIL

42 III

AI&DS

Data Structures

21 22

December 11,2021
DATA STRUCTURES
TEACHER’ S
SR. PAGE
EXPERIMENT NAME DATE SIGN.
NO. NO.
1. To study and implement Stack ADT
using array
2. To study and implement Convert an
Infix expression to Postfix expression
using stack ADT
3. To study and implement Evaluate
Postfix Expression using Stack ADT
4. To study and implement Applications
of Stack ADT
5. To study and implement Linear Queue
ADT using array
6. To study and implement Circular
Queue ADT using array
7. To study and implement Priority
Queue ADT using array.
8. To study and implement Singly
Linked List ADT.
9. To study and implement Circular
Linked List ADT
10. To study and implement Dobuly Linked
List .
Experiment no. 1

INPUT:

#include <stdio.h>
#include <stdlib.h>

int a[20],ch=0,top=-1,i,s,n,j,k;
void push();
void pop();
void display();

int main()
{
printf("Enter the size of stack\n");scanf("%d",&s);
while(ch!=7)
{
printf("Select your choice 1-->Push 2-->Pop 3-->Display 4-->Peek 5-->Top element 6-->Bottom element 7--
>Exit\n"); scanf("%d",&ch);switch(ch)
{
case 1:
{
push();break;
}
case 2:
{
pop();break;
}
case 3:
{
display();break;
}
case 4:
{
peek();break;;
}
case 5:
{
Top();break;
}
case 6:
{
Bottom();break;
}
case 7:
{
exit(0);break;
}
default:
{
printf("Invalid choice...Please select options between 1 to 7");break;
}
}
}
return 0;
}
void push()
{
if(top==s-1)
{
printf("Overflow has occured!!!.. ");
}
else
{
printf("Enter the number to be inserted into stack\n");scanf("%d",&n);
top=top+1;a[top]=n;
}
}
void pop()
{
if(top==-1)
{
printf("Underflow has occured!!!.. ");
}
else
{
top=top-1;
}
}
int b[20]; void display()
{
for(i=top;i>=0;i--)
{
printf("top[%d]=%d\n",i,a[i]);break;
}
for(i=top-1;i>=0;i--)
{
printf("%d\n",a[i]);
}
if(top==-1)
{
printf("Stack is empty");
}
}
int peek()
{
int j=a[top];
printf("The value at top is:%d\n",j);return j;
}
void Top()
{
if (top == -1)
{
printf("Stack is Empty!!\n");
}
else
{
printf("Top element is:\n%d\n",a[top]);
}
}
void Bottom()
{
if (top == -1)
{
printf("Stack is Empty!!\n");
}
else
{
printf("Bottom element is:\n%d\n",a[0]);
}
}
Output :-
Experiment 02 ;

Input :
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define SIZE 100
char stack[SIZE];
int top=-1;
void push(char item);
char pop();
int is_operator(char symbol);
int precedence(char symbol);
void infixtopostfix(char infix_exp[],char postfix_exp[]);
int main(){
char infix[SIZE],postfix[SIZE];
printf("Enter infix expression\n");
gets(infix);
infixtopostfix(infix,postfix);
printf("Postfix Expression is : ");
puts(postfix);
return 0;
}
void push(char item){
if(top>=SIZE-1){
printf("\nSTACK is over flow");
}
else{
top++;
stack[top]=item;
}
}
char pop(){
char item;
if(top<=-1){
printf("\nStack is under flow");
}
else{
item=stack[top];
top--;
return item;
}
}
int is_operator(char symbol){
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol == '-'){
return 1;
}
else{
return 0;
}
}
int precedence(char symbol){
if(symbol=='^'){
return 3;
}
else if(symbol=='*' || symbol=='/'){
return 2;
}
else if(symbol=='+' || symbol=='-'){
return 1;
}
else{
return 0;
}
}
void infixtopostfix(char infix_exp[],char postfix_exp[]){
int i,j;
char item;
char x;
push('(');
strcat(infix_exp,")");
i=0;
j=0;
item=infix_exp[i];
while(item!='\0'){
if(item=='('){
push(item);
}
else if(isdigit(item) || isalpha(item)){
postfix_exp[j]=item;
j++;
}
else if(is_operator(item)==1){
x=pop();
while(is_operator(x)==1 && precedence(x)>=precedence(item)){
postfix_exp[j]=x;
j++;
x=pop();
}
push(x);
push(item);
}
else if (item == ')')
{
x=pop();
while(x!='('){
postfix_exp[j]=x;
j++;
x=pop();
}
}
else{
printf("\nInvalid infix expression\n");
}
i++;
item=infix_exp[i];
}
if(top>0){
printf("\nInvalid infix expression\n");
}
postfix_exp[j]='\0';
}

Output :
Experiment 3

Input :
#include<stdio.h>
#include<stdlib.h>
#define MAX 80
int stack[MAX];int top=-1;

int gettype(char t)
{
switch(t)
{
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
case '$': return 2;default: return 1;
}
}
void push( int temp)
{
if(top==MAX-1)
{
printf("\n Stack full");
}
else stack[++top]=temp;
}
void eval(char op,int num1, int num2)
{
int result;switch(op)
{
case '+' :result=num1 + num2;break;
case '-' :result=num1 - num2;break;
case '*' :result=num1 * num2;break;
case '/' :result=num1 / num2;break;
case '%' :result=num1 % num2;break;
case '^':
case '$': result=pow(num1,num2) ;
}
push(result);
if(top==MAX-1)
{
printf("\n Stack full");
}
else stack[++top]=temp;
}
void eval(char op,int num1, int num2)
{
int result;switch(op)
{
case '+' :result=num1 + num2;break;
case '-' :result=num1 - num2;break;
case '*' :result=num1 * num2;break;
case '/' :result=num1 / num2;break;
case '%' :result=num1 % num2;break;
case '^':
case '$': result=pow(num1,num2) ;
}
push(result);
}
int pop()
{
int x;
if(top == -1)
printf("stack-Underflow"); else
x=stack[top--];
return x;
}
main()
{
int ans,i,n1,n2,res,ch;char exp[MAX];
printf("\n Enter the Postfix expression : \t");scanf("%s",exp);
for(i=0;exp[i]!='\0';i++)
{
ans=gettype(exp[i]); switch(ans)
{
case 1:
ch=exp[i]-'0'; //0 --48push(ch);
break;case 2:
n2=pop();
n1=pop(); eval(exp[i],n1,n2); break;
}
}
printf("\n Evaluation of Postfix expression - Answer is : %d",pop());getch();
}
Experiment 4

Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
struct Stack
{
unsigned capacity;
int top;
int *array;
};
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack -> capacity = capacity;
stack -> top = -1;
stack -> array = (int*) malloc(stack -> capacity * sizeof(int));
return stack;
}
int isFull(struct Stack* stack)
{
return (stack->top == stack->capacity - 1);
}
int isEmpty(struct Stack* stack)
{
return (stack->top == -1);
}
void push(struct Stack *stack, int item)
{
if (isFull(stack))
return;
stack -> array[++stack -> top] = item;
}
int pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack -> array[stack -> top--];
}
void moveDisk(char fromPeg, char toPeg, int disk)
{
printf("Move the disk %d from \'%c\' to \'%c\'\n",
disk, fromPeg, toPeg);
void moveDisksBetweenTwoPoles(struct Stack *src, struct Stack *dest, char s, char d)
{
int pole1TopDisk = pop(src);
int pole2TopDisk = pop(dest);

if (pole1TopDisk == INT_MIN)
{
push(src, pole2TopDisk);
moveDisk(d, s, pole2TopDisk);
}

else if (pole2TopDisk == INT_MIN)


{
push(dest, pole1TopDisk);
moveDisk(s, d, pole1TopDisk);
}

else if (pole1TopDisk > pole2TopDisk)


{
push(src, pole1TopDisk);
push(src, pole2TopDisk);
moveDisk(d, s, pole2TopDisk);
}

else
{
push(dest, pole2TopDisk);
push(dest, pole1TopDisk);
moveDisk(s, d, pole1TopDisk);
}
}

void tohIterative(int num_of_disks, struct Stack *src, struct Stack *aux, struct Stack *dest)
{
int i, total_num_of_moves;
char s = '1', d = '2', a = '3';

if (num_of_disks % 2 == 0)
{
char temp = d;
d = a;
a = temp;
}
total_num_of_moves = pow(2, num_of_disks) - 1;

for (i = num_of_disks; i >= 1; i--)


push(src, i);

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


{
if (i % 3 == 1)
moveDisksBetweenTwoPoles(src, dest, s, d);

else if (i % 3 == 2)
moveDisksBetweenTwoPoles(src, aux, s, a);

else if (i % 3 == 0)
moveDisksBetweenTwoPoles(aux, dest, a, d);
}
}

int main()
{
unsigned num_of_disks = 3;

struct Stack *src, *dest, *aux;

src = createStack(num_of_disks);
aux = createStack(num_of_disks);
dest = createStack(num_of_disks);
tohIterative(num_of_disks, src, aux, dest);
return 0;
}
Output:
Experiment 05 :

Input
#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;
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(1);
default:
printf("Wrong choice \n");
}
}
}

void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
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;
}
}
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");
}
}
Experiment 06
Code :
#include <stdio.h>
# define max 6
int queue[max];
int front=-1;
int rear=-1;
void enqueue(int element)
{
if(front==-1 && rear==-1)
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front)
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max;
queue[rear]=element;
}
}
int dequeue()
{
if((front==-1) && (rear==-1))
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is: %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is: %d", queue[front]);
front=(front+1)%max;
}
}
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are : ");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x;

while(choice<4 && choice!=0)


{
printf("\nPress 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{

case 1:

printf("Enter the element which is to be inserted: ");


scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}}
return 0;
}
Output:

SS
Experiment 07

Code:
#include <stdio.h>
int size = 0;
void swap(int *a, int *b) {
int temp = *b;
*b = *a;
*a = temp;
}
void heapify(int array[], int size, int i) {
if (size == 1) {
printf("Single element in the heap");
} else {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < size && array[l] > array[largest])
largest = l;
if (r < size && array[r] > array[largest])
largest = r;
if (largest != i) {
swap(&array[i], &array[largest]);
heapify(array, size, largest);
}
}
}
void insert(int array[], int newNum) {
if (size == 0) {
array[0] = newNum;
size += 1;
} else {
array[size] = newNum;
size += 1;
for (int i = size / 2 - 1; i >= 0; i--) {
heapify(array, size, i);
}
}
}
void deleteRoot(int array[], int num) {
int i;
for (i = 0; i < size; i++) {
if (num == array[i])
break;
}
swap(&array[i], &array[size - 1]);size -= 1;
for (int i = size / 2 - 1; i >= 0; i--) {
heapify(array, size, i);
}
}
void printArray(int array[], int size) {for (int i = 0;
i < size; ++i) printf("%d ", array[i]);
printf("\n");
}
int main() { int array[10];
insert(array, 3);
insert(array, 4);
insert(array, 9);
insert(array, 5);
insert(array, 2); printf("Max-Heap array:
");printArray(array, size);
deleteRoot(array, 4);
printf("After deleting an element: ");
printArray(array, size);
}

Output:
Experiment – 08

Input :

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
struct node {
int data;
struct node *next;
};
struct node *head;

void binsert(); void einsert(); void rinsert(); void bdelete();void edelete();void rdelete();void display(); void search();
void reverse();

void main() {
int choice = 0,data,index,ch=0,j=1;
printf("Enter the no of elements to insert in linked list:\n");scanf("%d",&ch);
printf("Enter the %d th element:\n",j);binsert();
for (j=2; j<=ch; j++) {
printf("Enter the %d th element:\n",j);einsert();
}
display();

do {printf("*****Main Menu******\nEnter you choice:\n1.Insert at beggining\t2.Insert at the end\t3.Insertat random


position\n4.Delete at beggining\t5.Delete at the end\t6.Delete at random
position\n7.Display\t8.Search\t9.Reverse\t10.Exit\n");
scanf("%d",&choice);switch(choice) {
case 1:
binsert();break;

case 2:
einsert();break;

case 3:
printf("Enter the position at which you want to insert:\n");scanf("%d",&index);
printf("Insert the value to insert:\n");scanf("%d",&data);
rinsert(head, data, index);break;

case 4: bdelete();break;

case 5: edelete();break;

case 6: rdelete();break;

case 7: display();break;

case 8: search();
break;
case 9: reverse();
break;

case 10: exit(0); break;


default:
printf("\nPlease enter a valid value!!\n");
}
}
while(choice != 10);
}

void binsert() { struct node *ptr;


ptr = (struct node*)malloc(sizeof(struct node*));

if (ptr == NULL) {
printf("you have reached the end of the Linked List.\n");
}
else {
int item;
printf("Enter the value you want to store:\n"); scanf("%d",&item);
ptr->data = item; ptr->next = head; head = ptr;
printf("Node Inserted.\n");
}
}
void einsert(){ int d;
struct node *ptr,*temp;
ptr = (struct node*)malloc(sizeof(struct node*)); temp = head;
if (temp->next == NULL) { temp->next = ptr;
ptr->next = NULL;
printf("Enter the element to insert:\n"); scanf("%d",&d);
ptr->data = d;
printf("Node Inserted.\n");
}
else {
while (temp->next != NULL) {temp = temp->next;
}
temp->next = ptr; ptr->next = NULL;
printf("Enter the element to insert:\n");scanf("%d",&d);
ptr->data = d; printf("Node Inserted.\n");

}
}
void rinsert(struct node *head, int data, int index){
struct node * ptr = (struct node *) malloc(sizeof(struct node));struct node * p;
p = head;int i = 1;
while (i!=index-1)
{
p = p->next;i++;
}
ptr->data = data;
ptr->next = p->next;p->next = ptr;
printf("Node Inserted.\n");
}
void bdelete() {
struct node *p = head;head = head->next; free(p);
printf("Node deleted.\n");
}

void edelete() {
struct node *ptr, *p;ptr = head;
while(ptr->next != NULL) {p=ptr;
ptr=ptr->next;
}
if(ptr==head)
{
head=NULL;
}
else
{
p->next = NULL;
}
free(ptr);
printf("Node deleted.\n");

void rdelete() {int i,n;


struct node *q,*p;
printf("Enter the position at which you want to delete the node:\n");scanf("%d",&n);
q = head;i = 1;
while (i < n-1) {q = q->next; i++;
}
p=q->next;
q- >next=p->next;free(p);
printf("Node deleted.\n");
}

void search() {int n, i;


printf("Enter the element to search:\n");scanf("%d",&n);
printf("After performing the search algorithm:\n");struct node *p;
p = head;i = 1;
while(p!=NULL) { if (p->data == n) {
printf("%d found in the Linked list at %d th position!!\n",p->data, i);break;
}
p = p->next;i++;
}
if (p == NULL) {
printf("Data not found!!\n");
}
}
void reverse(){
struct node *prevnode,*currentnode,*nextnode;prevnode=0;
currentnode=nextnode=head;while(nextnode!=0)
{
nextnode=nextnode->next; currentnode->next=prevnode;prevnode=currentnode; currentnode=nextnode;
}
head=prevnode;
printf("Linked list has reversed\n");

}
void display() {
printf("Elements of Linked list in sequencial order:\n");struct node *p;
p = head; while(p!=NULL) {
printf("%d\n",p->data);p = p->next;
}
}
OUTPUT:
OUTPUT:
Enter the no of elements to insert in linked list:
Enter
3 the no of elements to insert in linked list:
3Enter the 1 th element:
Enter
Enterthe
the1value
th element:
you want to store:
Enter
10 the value you want to store:
10
Node Inserted.
Node
EnterInserted.
the 2 th element:
Enter
Enterthe
the2element
th element:
to insert:
Enter
20 the element to insert:
20
Node Inserted.
Node
EnterInserted.
the 3 th element:
Enter
Enterthe
the3element
th element:
to insert:
Enter
30 the element to insert:
30
Node Inserted.
Node Inserted.
Elements of Linked list in sequencial order:
Elements
10 of Linked list in sequencial order:
10
20
20
30
30
*****Main Menu******Enter you choice:
*****Main Menu******Enter
1.Insert at beggining 2.Insert you choice:
at the end 3.Insert at random position 4.Delete at beggining
Insert atatbeggining
1.5.Delete 2.Insert at the
the end 6.Delete at random endposition7.Display
3.Insert at8.Search
random position9.Reverse
4.Delete at beggining
10.Exit 5.Delete at
the
1 end 6.Delete at random position7.Display 8.Search 9.Reverse 10.Exit
1Enter the value you want to store:
Enter
5 the value you want to store:
5Node Inserted.
Node Inserted.
*****Main Menu******Enter you choice:
*****Main Menu******Enter
1.Insert at beggining 2.Insert you choice:
at the end 3.Insert at random position 4.Delete at beggining
1.Insert at beggining 2.Insert at
5.Delete at the end 6.Delete at random the endposition7.Display
3.Insert at8.Search
random position9.Reverse
4.Delete at beggining
10.Exit 5.Delete at
the
2 end 6.Delete at random position7.Display 8.Search 9.Reverse 10.Exit
2Enter the element to insert:
Enter
25 the element to insert:
25
Node Inserted.
Node Inserted.
*****Main Menu******Enter you choice:
*****Main Menu******Enter
1.Insert at beggining 2.Insert you choice:
at the end 3.Insert at random position 4.Delete at beggining
1.Insert at beggining 2.Insert at
5.Delete at the end 6.Delete at random the endposition7.Display
3.Insert at8.Search
random position9.Reverse
4.Delete at beggining
10.Exit 5.Delete at
the
3 end 6.Delete at random position7.Display 8.Search 9.Reverse 10.Exit
3Enter the position at which you want to insert:3
Enter
Insertthe
theposition
value toatinsert:15
which you want to insert:3
Insert the value
Node Inserted. to insert:15
Node Inserted.
*****Main Menu******Enter you choice:
*****Main Menu******Enter
1.Insert at beggining 2.Insert you choice:
at the end 3.Insert at random position 4.Delete at beggining
1.Insert at beggining 2.Insert at
5.Delete at the end 6.Delete at random the endposition7.Display
3.Insert at8.Search
random position9.Reverse
4.Delete at beggining
10.Exit 5.Delete at
the
7 end 6.Delete at random position7.Display 8.Search 9.Reverse 10.Exit
7Elements of Linked list in sequencial order:
Elements
5 of Linked list in sequencial order:
510
10
15
15
20
20
30
30
25
25
*****Main Menu******Enter you choice:
*****Main Menu******Enter
1.Insert at beggining 2.Insert you choice:
at the end 3.Insert at random position 4.Delete at beggining
Insert atatbeggining
1.5.Delete 2.Insert at the
the end 6.Delete at random endposition7.Display
3.Insert at8.Search
random position9.Reverse
4.Delete at beggining
10.Exit 5.Delete at
4
Node deleted.
*****Main Menu******Enter you choice:
1. Insert at beggining 2.Insert at the end 3.Insert at random position 4.Delete at beggining
5.Delete at the end 6.Delete at random position7.Display 8.Search 9.Reverse 10.Exit
5
Node deleted.
*****Main Menu******Enter you choice:
1.Insert at beggining 2.Insert at the end 3.Insert at random position 4.Delete at beggining
5.Delete at the end 6.Delete at random position7.Display 8.Search 9.Reverse 10.Exit
6
Enter the position at which you want to delete the node:3
Node deleted.
*****Main Menu******Enter you choice:
1.Insert at beggining 2.Insert at the end 3.Insert at random position 4.Delete at beggining
5.Delete at the end 6.Delete at random position7.Display 8.Search 9.Reverse 10.Exit
7
Elements of Linked list in sequencial order:
10
15
30
*****Main Menu******Enter you choice:
1.Insert at beggining 2.Insert at the end 3.Insert at random position 4.Delete at beggining
5.Delete at the end 6.Delete at random position7.Display 8.Search 9.Reverse 10.Exit
8
Enter the element to search:
15
After performing the search algorithm:
15 found in the Linked list at 2 th position!!
*****Main Menu******
Enter you choice:
1.Insert at beggining 2.Insert at the end 3.Insert at random position 4.Delete at beggining
5.Delete at the end 6.Delete at random position7.Display 8.Search 9.Reverse 10.Exit
9
Linked list has reversed
*****Main Menu******Enter you choice:
1.Insert at beggining 2.Insert at the end 3.Insert at random position 4.Delete at beggining
5.Delete at the end 6.Delete at random position7.Display 8.Search 9.Reverse 10.Exit
7
Elements of Linked list in sequencial order:
30
15
10
*****Main Menu******Enter you choice:
1.Insert at beggining 2.Insert at the end 3.Insert at random position 4.Delete at beggining
5.Delete at the end 6.Delete at random position7.Display 8.Search 9.Reverse 10.Exit
10

Process returned 0 (0x0) execution time : 150.386 sPress any key to contiue
Experiment - 9
Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from
Beginning\n
5.Delete from last\n6.Delete the node after the given data\n7.Search\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);

if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}

}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}

}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL){
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void search() {
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else {
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL){
if(ptr->data == item)
{
printf("\nitem found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1) {
printf("\nItem not found\n");
}
}

}
OUTPUT:
****************Main
Menu*****************Enter you choice:
1.Insert at empty 2.Insert at beggining 3.Insert at the end 4.Insert at random
position5.Delete at beggining 6.Delete at the end 7.Delete at random
position
8.Display 9.Search
10.Exit1
Insert the value to insert:
10
Node Inserted!!
****************Main
Menu*****************Enter you choice:
1.Insert at empty 2.Insert at beggining 3.Insert at the end 4.Insert at random
position5.Delete at beggining 6.Delete at the end 7.Delete at random
position
8.Display 9.Search
10.Exit2
Insert the value to insert:
20
Node Inserted!!
****************Main
Menu*****************Enter you choice:
1.Insert at empty 2.Insert at beggining 3.Insert at the end 4.Insert at random
position5.Delete at beggining 6.Delete at the end 7.Delete at random
position
8.Display 9.Search
10.Exit3
Insert the value to insert:
40
Node Inserted!!
****************Main
Menu*****************Enter you choice:
1.Insert at empty 2.Insert at beggining 3.Insert at the end 4.Insert at random
position5.Delete at beggining 6.Delete at the end 7.Delete at random
position
8.Display 9.Search
10.Exit3
Insert the value to insert:
50
Node Inserted!!
****************Main
Menu*****************Enter you choice:
1.Insert at empty 2.Insert at beggining 3.Insert at the end 4.Insert at random
position5.Delete at beggining 6.Delete at the end 7.Delete at random
position
8.Display 9.Search 10.Exit8
Linked List in sequencial
order:20
10
40
50
****************Main
Menu*****************Enter you choice:
1.Insert at empty 2.Insert at beggining 3.Insert at the end 4.Insert at
random position5.Delete at beggining 6.Delete at the end 7.Delete at random
position
8.Display 9.Search 10.Exit4
Enter the position at which you want to insert:
3
Insert the value to insert:
30
Node Inserted!!
****************Main
Menu*****************Enter you choice:
1.Insert at empty 2.Insert at beggining 3.Insert at the end 4.Insert at
random position5.Delete at beggining 6.Delete at the end 7.Delete at random
position
8.Display 9.Search
10.Exit5
Node deleted!!
****************Main
Menu*****************Enter you choice:
1.Insert at empty 2.Insert at beggining 3.Insert at the end 4.Insert at
random position5.Delete at beggining 6.Delete at the end 7.Delete at random
position
8.Display 9.Search
10.Exit6
Node deleted!!
****************Main
Menu*****************Enter you choice:
1.Insert at empty 2.Insert at beggining 3.Insert at the end 4.Insert at
random position5.Delete at beggining 6.Delete at the end 7.Delete at random
position
8.Display 9.Search
10.Exit7
Enter the position at which you want to delete:
2
Node deleted!!
****************Main
Menu*****************Enter you choice:
1.Insert at empty 2.Insert at beggining 3.Insert at the end 4.Insert at
random position5.Delete at beggining 6.Delete at the end 7.Delete at random
position
8. Display 9.Search
10.Exit8
Linked List in sequencial order:
10
40
****************Main
Menu*****************Enter you choice:
1.Insert at empty 2.Insert at beggining 3.Insert at the end 4.Insert at
random position5.Delete at beggining 6.Delete at the end 7.Delete at random
position
8.Display 9.Search
10.Exit9
Insert the value to
search:40
After applying the search
algorithm:Found 40 at 2 th
position:
Enter you choice:
1. Insert at beggining 2.Insert at the end
3.Insert at random position 4.Delete at
beggining 5.Delete at the end 6.Delete at random
position7.Display 8.Search 9.Reverse
10.Exit
9
Linked list has reversed
*****Ma
in
Menu***
***Enter
you
choice:
1.Insert at beggining 2.Insert at the end
3.Insert at random position 4.Delete at
beggining 5.Delete at the end 6.Delete at random
position7.Display 8.Search 9.Reverse
10.Exit
7
Elements of Linked list in sequencial order:
30
15
10
*****Ma
in
Menu***
***Enter
you
choice:
1.Insert at beggining 2.Insert at the end
3.Insert at random position 4.Delete at
beggining 5.Delete at the end 6.Delete at random
position7.Display 8.Search 9.Reverse
10.Exit
10

Process returned 0 (0x0)


execution time : 150.386 sPress
any key to continue.
Experiment 10

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
struct node *head = NULL; struct
node *current = NULL;bool
isEmpty() {
return head == NULL;
}
int length() { int
length = 0;
if(head == NULL) {
return 0;
}
current = head->next;
while(current != head) {
length++;
current = current->next;
}
return length;
}
void insertFirst(int key, int data) {
struct node *link = (struct node*) malloc(sizeof(struct node));link->key =
key;
link->data = data;if
(isEmpty()) {
head = link;
head->next = head;
} else {
link->next = head;
head = link;
}
}
struct node * deleteFirst() { struct
node *tempLink = head;if(head-
>next == head) {
head = NULL;

return tempLink;
}
head = head->next;
return tempLink;
}
void printList() {
struct node *ptr = head;
printf("\n[ ");
if(head != NULL) { while(ptr-
>next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);ptr =
ptr->next;
}
}
printf(" ]");
}
void main() { insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printf("Original List: ");
printList();
while(!isEmpty()) {
struct node *temp = deleteFirst(); printf("\nDeleted
value:"); printf("(%d,%d) ",temp->key,temp-
>data);
}
printf("\nList after deleting all items: ");
printList();
}

Output

You might also like