You are on page 1of 46

Madhuben and Bhanubhai

Patel Institute of
Technology

Data Structure
(3130702)
AY: 2020 - 21

Name : Akshit R. Varmora


Er. No. :
Sem : 3rd
Class : CE3
Practical 1

#include<stdio.h>

void ins(int[]);

void del(int[]);

void sort(int[]);

void search(int[]);

void display(int a[10]);

int a[10],i,n=10,j;

void main()

int c;

char ch;

printf("\n enter array elements");

for(i=0;i<10;i++)

scanf("%d",&a[i]);

printf("\n choose the operation number to conduct in array");

printf("\n 1- insert \n 2- delete \n 3- sort \n 4- search \n 5- exit \n 6- display\n 7- reverse\n8-


merge");

while(1)

printf("enter choice");

scanf("%d",&c);

switch(c)

case 1:

ins(a);

break;
case 2:

del(a);

break;

case 3:

sort(a);

break;

case 4:

search(a);

break;

case 5:

exit(1);

case 6:

display(a);

break;

case 7:

reverse(a);

case 8:

merge(a, 5);

default:

printf("wrong input");

break;

void ins(int a[10])

int i,x,pos,n=10;

printf("enter the element to be inserted");

scanf("%d",&x);

printf("enter the position to be inserted");

scanf("%d",&pos);
n++;

for(i=n;i>=pos;i--)

a[i]=a[i-1];

a[pos-1]=x;

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

printf("%d\n",a[i]);

void sort(int a[10])

int temp;

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

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

if(a[i]>a[j])

temp=a[i];

a[i]=a[j];

a[j]=temp;

printf("display sorted array:");

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

printf("\n%d",i,a[i]);

void del(int a[10])

int p,temp;

printf("enter position of element to be deleted");

scanf("%d",&p);
printf("\n deleted element:%d",a[p]);

for(temp=p;temp<n;temp++)

a[temp]=a[temp+1];

n=n-1;

printf("array after deletion:");

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

printf("\n%d",i,a[i]);

void search(int a[10])

int element;

printf("enter the element to be searched:");

scanf("%d",&element);

if(a[i]==element)

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

j=i;

printf("\n the location of the search element %d in the array is %d",element,j);

void display(int a[10])

for (int i = 0; i < n; i++)

printf("%d", a[i]);

}
void reverse(int a[10])

for (int i = n-1; i >= 0; i--)

printf("%d\t", a[i]);

void merge(int ar[], int s)

int ar2[5],ar3[10];

int i,j,k;

int p=5;

printf("\nEnter array to merge: ");

for(i=0;i<p;i++)

scanf("%d",&ar2[i]);

k=0;

j=0;

for(i=0;i<s+p;)

if(j<s && k<p)

if(ar[j]<ar2[k])

ar3[i]=ar[j];

k++;

else

ar3[k]=ar2[k];

k++;

}
i++;

else if(j==s)

for(;i<s+p;)

ar3[i]=ar2[k];

k++;

i++;

else

for(;i<s+p;)

ar3[i]=ar[j];

j++;

i++;

printf("\nSorted merged array: \n");

for(i=0;i<s+p;i++)

printf("%d\n",ar3[i]);

getch();

--> Output

5 (size of the array)

1 (array elements)

2
3

5 (Element to be searched)

Element found

Practical 2

#include<stdio.h>

int list[5],tos=-1,size=4,ch;

int main()

void push();

void display();

void pop();

void peep();

void update();

printf("\n*** Stack Menu ***");

printf("\n1:Push Operation");

printf("\n2:Display");

printf("\n3:Pop Operation");

printf("\n4:Peep Operation");

printf("\n5:Update Operation");

printf("\n0:Exit");

do

printf("\nEnter The Above Choice:->");

scanf("%d",&ch);

switch(ch)

{
case 0:

exit(0);

case 1:

push();

break;

case 2:

display();

break;

case 3:

pop();

break;

case 4:

peep();

break;

case 5:

update();

break;

while(ch!=0);

void push()

int value;

if(tos>=size)

printf("\nThe Stack is Overflow...");

return;

else

printf("\nEnter The Value:->");

scanf("%d",&value);
tos=tos+1;

list[tos]=value;

void display()

int i;

if(tos==-1)

printf("\nThere is Element not Avaible...");

return;

else

for(i=0;i<=tos;i++)

printf("\nThe Element is:->%d",list[i]);

void pop()

if(tos==0)

printf("\nThe stack is Underflow...");

return;

else

tos=tos-1;

printf("\nElement Delete Successfull.....");

void peep()

{
int s;

printf("\nEnter Position for search:->");

scanf("%d",&s);

if(tos-s<=-1)

printf("\nThe Stack is Overflow...");

return;

else

printf("\nThe Element is:->%d",list[tos-s]);

void update()

int it1,it2;

printf("\nEnter Position for change:->");

scanf("%d",&it1);

printf("\nEnter Number For Update:->");

scanf("%d",&it2);

if(tos-it1<=-1)

printf("\nThe Stack Underflow...");

return;

else

list[tos-it1]=it2;

printf("\nUpdte Successfull.....");

}
~ > Output

*** Stack Menu ***

1.Push

2.Pop

3.Display

4.Exit

Enter your choice(1-4):1

Enter element to push:3

*** Stack Menu ***

1.Push

2.Pop

3.Display

4.Exit

Enter your choice(1-4):1

Enter element to push:6

Stack Menu

1.Push

2.Pop

3.Display

4.Exit

Enter your choice(1-4):3

Stack is

...

*** Stack Menu ***

1.Push

2.Pop

3.Display
4.Exit

Enter your choice(1-4):2

Deleted element is 6

*** Stack Menu ***

1.Push

2.Pop

3.Display

4.Exit

Enter your choice(1-4):3

Stack is

...

*** Stack Menu ***

1.Push

2.Pop

3.Display

4.Exit

Enter your choice(1-4):2

Deleted element is 3

*** Stack Menu ***

1.Push

2.Pop

3.Display

4.Exit

Enter your choice(1-4):2

Stack is empty!!
Practical 3
#include<stdio.h>

char stack[20];

int top = -1;

void push(char x)

stack[++top] = x;

char pop()

if(top == -1)

return -1;

else

return stack[top--];

int priority(char x)

 if(x == '(')

return 0;

if(x == '+' || x == '-')

return 1;

if(x == '*' || x == '/')

return 2;

main()

char exp[20];

char *e, x;

printf("Enter the expression :: ");

scanf("%s",exp);

e = exp;
while(*e != '\0')

if(isalnum(*e))

printf("%c",*e);

else if(*e == '(')

push(*e);

else if(*e == ')')

while((x = pop()) != '(')

printf("%c", x);

else

while(priority(stack[top]) >= priority(*e))

printf("%c",pop());

push(*e);

e++;

while(top != -1)

printf("%c",pop());

--> Output

Enter infix : A + B + C + D

Prefix : +++ABCD

Stack underflow

Enter infix : (9 + 8) * (5 + 6)

Prefix : *+98+56

Value of expression : 187


Practical 4

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

#define max 100

float st[max];

int top=-1;

void push(float st[],float val);

float pop(float st[]);

float evapostexp(char exp[]);

void main()

float val;

char exp[100];

clrscr();

printf("enter postfix expression: ");

fflush(stdin);

gets(exp);

val=evapostexp(exp);

printf("\n value of the poostfix expression=%f",val);

getch();

float evapostexp(char exp[])

int i;

float op1,op2,value;

for(i=0;exp[i]!='\0';i++)

{
if(isdigit(exp[i]))

push(st,(float)(exp[i]-'0'));

else

op2=pop(st);

op1=pop(st);

switch(exp[i])

case '+':

value=op1 + op2;

break;

case '-':

value=op1 - op2;

break;

case '*':

value=op1 * op2;

break;

case '/':

value=op1 / op2;

break;

push(st,value);

return(pop(st));

void push(float st[],float val)

if(top==max-1)

printf("\n stack overflow");


}

else

top++;

st[top]=val;

float pop(float st[])

float val;

if(top==-1)

printf("\n stack underflow");

else

val=st[top];

top--;

return val;

--> Output

Enter the expression : 245+*

The result of expression 245+* = 18


Practical 5
#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)

/*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;

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”);

--> Output

Queue using Array

1.lnsertion

2.Deletion

3.Display

4.Exit

Enter the Choice:1

Enter no 1:10

Enter the Choice:1

Enter no 2: 54

Enter the Choice: 1


Enter no 3: 98

Enter the Choice:1

Enter no 4: 234

Enter the Choice: 3

Queue Elements are:

10 54 98 234

Enter the Choice:2

Deleted Element is 10

Enter the Choice:3

Queue Elements are:

54 98 234

Enter the Choice:4

Practical 6
#include <stdio.h>

#define MAX 5

int cqueue_arr[MAX];

int front = -1;

int rear = -1;

void insert(int item)

if ((front == 0 && rear == MAX - 1) || (front == rear + 1))

printf("Queue Overflow n");

return;

if (front == -1)

{
front = 0;

rear = 0;

else

if (rear == MAX - 1)

rear = 0;

else

rear = rear + 1;

cqueue_arr[rear] = item;

void deletion()

if (front == -1)

printf("Queue Underflown");

return;

printf("Element deleted from queue is : %dn", cqueue_arr[front]);

if (front == rear)

front = -1;

rear = -1;

else

if (front == MAX - 1)

front = 0;

else

front = front + 1;

}
void display()

int front_pos = front, rear_pos = rear;

if (front == -1)

printf("Queue is emptyn");

return;

printf("Queue elements :n");

if (front_pos <= rear_pos)

while (front_pos <= rear_pos)

printf("%d ", cqueue_arr[front_pos]);

front_pos++;

else

while (front_pos <= MAX - 1)

printf("%d ", cqueue_arr[front_pos]);

front_pos++;

front_pos = 0;

while (front_pos <= rear_pos)

printf("%d ", cqueue_arr[front_pos]);

front_pos++;

printf("n");

int main()

{
int choice, item;

do

printf("1.Insert \n");

printf("2.Delete \n");

printf("3.Display \n");

printf("4.Quit\n");

printf("Enter your choice : ");

scanf("%d", &choice);

switch (choice)

case 1:

printf("Input the element for insertion in queue : ");

scanf("%d", &item);

insert(item);

break;

case 2:

deletion();

break;

case 3:

display();

break;

case 4:

break;

default:

printf("Wrong choicen");

} while (choice != 4);

return 0;

--> Output

1.interest
2.delete

3.display

4.quit

Enter your choice :1

Input the element for insertion in queue : 34

1.interest

2.delete

3.display

4.quit

Enter your choice : 2

Input the element for insertion in queue : 45

1.interest

2.delete

3.display

4.quit

Enter your choice : 3

Queue elements

34 45

1.interest

2.delete

3.display

4.quit

Enter your choice : 4

Practical 7

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

void create();

void display();

void insert_begin();

void insert_end();

void insert_pos();

void delete_begin();

void delete_end();

void delete_pos();

struct node

int info;

struct node *next;

};

struct node *start=NULL;

int main()

int choice;

while(1){

printf("\nMENU\n");

printf("\n 1.Create \n");

printf("\n 2.Display \n");

printf("\n 3.Insert at the beginning\n");

printf("\n 4.Insert at the end \n");

printf("\n 5.Insert at specified position \n");

printf("\n 6.Delete from beginning\n");

printf("\n 7.Delete from the end \n");

printf("\n 8.Delete from specified position \n");

printf("\n 9.Exit\n");

printf("\n--------------------------------------\n");

printf("Enter your choice:t");


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("%dt",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=(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

ptr=start;

while(ptr->next !=NULL)

ptr=ptr->next ;

ptr->next =temp;

}
void insert_pos()

struct node *ptr,*temp;

int i,pos;

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

if(temp==NULL)

printf("nOut of Memory Space:n");

return;

printf("nEnter the position for the new node to be inserted:t");

scanf("%d",&pos);

printf("nEnter the data value of the node:t");

scanf("%d",&temp->info) ;

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 :%dt",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:%dt",ptr->info);

free(ptr);

else
{

ptr=start;

while(ptr->next!=NULL)

temp=ptr;

ptr=ptr->next;

temp->next=NULL;

printf("nThe deleted element is:%dt",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:%dt",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:%dt",ptr->info );

free(ptr);

--> Output

Created linked list

17864

Insert element at the end

178645

Ascending order

145678

Deleting node at 5

14567
Practical 8
#include <stdio.h>

#include <conio.h>

#include <malloc.h>

struct node

int data;

struct node *next;

};

struct node *start = NULL;

struct node *create_cll(struct node *);

struct node *display(struct node *);

struct node *insert_beg(struct node *);

struct node *insert_end(struct node *);

struct node *delete_beg(struct node *);

struct node *delete_end(struct node *);

struct node *delete_after(struct node *);

struct node *delete_list(struct node *);

int main()

int option;

clrscr();

do

printf("\n\n *****MAIN MENU *****");

printf("\n 1: Create a list");

printf("\n 2: Display the list");

printf("\n 3: Add a node at the beginning");

printf("\n 4: Add a node at the end");

printf("\n 5: Delete a node from the beginning");

printf("\n 6: Delete a node from the end");


printf("\n 7: Delete a node after a given node");

printf("\n 8: Delete the entire list");

printf("\n 9: EXIT");

printf("\n\n Enter your option : ");

scanf("%d", &option);

switch(option)

case 1: start = create_cll(start);

printf("\n CIRCULAR LINKED LIST CREATED");

break;

case 2: start = display(start);

break;

case 3: start = insert_beg(start);

break;

case 4: start = insert_end(start);

break;

case 5: start = delete_beg(start);

break;

case 6: start = delete_end(start);

break;

case 7: start = delete_after(start);

break;

case 8: start = delete_list(start);

printf("\n CIRCULAR LINKED LIST DELETED");

break;

}while(option !=9);

getch();

  return 0;

struct node *create_cll(struct node *start)

struct node *new_node, *ptr;


int num;

printf("\n Enter –1 to end");

printf("\n Enter the data : ");

scanf("%d", &num);

while(num!=–1)

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

new_node -> data = num;

if(start == NULL)

new_node -> next = new_node;

start = new_node;

else

{ ptr = start;

while(ptr -> next != start)

ptr = ptr -> next;

ptr -> next = new_node;

new_node -> next = start;

printf("\n Enter the data : ");

scanf("%d", &num);

return start;

struct node *display(struct node *start)

struct node *ptr;

ptr=start;

while(ptr -> next != start)

printf("\t %d", ptr -> data);


ptr = ptr -> next;

printf("\t %d", ptr -> data);

return start;

struct node *insert_beg(struct node *start)

struct node *new_node, *ptr;

int num;

printf("\n Enter the data : ");

scanf("%d", &num);

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

new_node -> data = num;

ptr = start;

while(ptr -> next != start)

ptr = ptr -> next;

ptr -> next = new_node;

new_node -> next = start;

start = new_node;

return start;

struct node *insert_end(struct node *start)

struct node *ptr, *new_node;

int num;

printf("\n Enter the data : ");

scanf("%d", &num);

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

new_node -> data = num;

ptr = start;

while(ptr -> next != start)

ptr = ptr -> next;

ptr -> next = new_node;


new_node -> next = start;

return start;

struct node *delete_beg(struct node *start)

struct node *ptr;

ptr = start;

while(ptr -> next != start)

ptr = ptr -> next;

ptr -> next = start -> next;

free(start);

start = ptr -> next;

return start;

struct node *delete_end(struct node *start)

struct node *ptr, *preptr;

ptr = start;

while(ptr -> next != start)

preptr = ptr;

ptr = ptr -> next;

preptr -> next = ptr -> next;

free(ptr);

return start;

struct node *delete_after(struct node *start)

struct node *ptr, *preptr;

int val;

printf("\n Enter the value after which the node has to deleted : ");

scanf("%d", &val);
ptr = start;

preptr = ptr;

while(preptr -> data != val)

preptr = ptr;

ptr = ptr -> next;

preptr -> next = ptr -> next;

if(ptr == start)

start = preptr -> next;

free(ptr);

return start;

struct node *delete_list(struct node *start)

struct node *ptr;

ptr = start;

while(ptr -> next != start)

start = delete_end(start);

free(start);

return start;

--> Output

Created Linked List:

82317

-- > Output after inserting 4 at the front of the linked list

482317

--> Output after inserting 5 at the front of the linked list

4823175

--> Output after deleting last node in Linked List

482317

--> Output after deleting node from specified node position : 3


48217

Practical 9

#include <stdio.h>

Int main()

Int c, first, last, middle, n, search, array[100];

Printf(“Enter number of elements\n”);

Scanf(“%d”, &n);

Printf(“Enter %d integers\n”, n);

For (c = 0; c < n; c++)

Scanf(“%d”, &array[c]);

Printf(“Enter value to find\n”);

Scanf(“%d”, &search);

First = 0;

Last = n – 1;

Middle = (first+last)/2;

While (first <= last) {

If (array[middle] < search)

First = middle + 1;

Else if (array[middle] == search) {

Printf(“%d found at location %d.\n”, search, middle+1);


Break;

Else

Last = middle – 1;

Middle = (first + last)/2;

If (first > last)

Printf(“Not found! %d isn’t present in the list.\n”, search);

Return 0;

--> OutPut

Enter number of the elements 7

Enter 7 integers

-4

11

43

485

Enter value to find

11

11 fund at location 5.

Practical 10
#include<stdio.h>

Int main()

Int count, temp, I, j, number[30];

Printf(“How many numbers are u going to enter?: “);

Scanf(“%d”,&count);

Printf(“Enter %d numbers: “,count);

For(i=0;i<count;i++)

Scanf(“%d”,&number[i]);

/* This is the main logic of bubble sort algorithm

*/

For(i=count-2;i>=0;i--){

For(j=0;j<=I;j++){

If(number[j]>number[j+1]){

Temp=number[j];

Number[j]=number[j+1];

Number[j+1]=temp;

Printf(“Sorted elements: “);

For(i=0;i<count;i++)

Printf(“ %d”,number[i]);

Return 0;

--> Output:

How many numbers you want to enter?: 5

Enter 5 numbers:
24675

Sorted elements: 2 4 5 6 7

You might also like