0% found this document useful (0 votes)
9 views

External Lab Pgms

A lab project program to get easily attached toit .....

Uploaded by

ropoc16581
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

External Lab Pgms

A lab project program to get easily attached toit .....

Uploaded by

ropoc16581
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

List of Data Structures Lab Programs for External Lab exam

1) Stack using Array

2) Stack using Linked list

3) Queue using Array

4) Queue using Linked list

5) Linear Search

6) Binary Search [Non Recursive]

7) Exchange Sort [Bubble Sort]

8) Selection Sort

9) Insertion Sort

10) Quick sort [Non recursion]

11) Heap Sort

12) Conversion of infix to postfix

13) Evaluation for postfix

14) Single linked list insertion

15) Single linked list Deletion

16) Single linked list Traverse

17) Removing duplicate elements(ordered & unordered)

18) Matrix Multiplication

19) Quick sort(Recursion)

20) Binary Search [Recursive]

1
1) Stack using Array
#include<stdio.h>
#include<stdlib.h>
int top,choice,i,size,item,stack[20];
void push(void);
void pop(void);
void display(void);

int main()
{
top=-1;
printf("enter the stack size:");
scanf("%d",&size);
printf("\nSTACK OPERATION USING ARRAYS.");
printf("-----------------------------\n\t");
printf("1,.PUSH \n\t 2.POP \n\t 3. DISPLAY \n\t 4. EXIT \n\t");
do
{
printf("enter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("exit");
}
default:
{
printf("please enter the valid choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}

void push()
{
if(top>=size-1)
2
{
printf("stack is overflow");
}
else
{
printf("enter the pushed elements :");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}

void pop()
{
if(top<0)
{
printf("stack is underflow:");
}
else
{
item=stack[top];
top=top-1;
}
printf("the poped elements is %d",item);
}

void display()
{
printf("the elements are:");
for(i=top;i>=0;i--)
{
printf("%d---->",stack[i]);
}
}

//OUTPUT

enter the stack size:5

STACK OPERATION USING ARRAYS.

-----------------------------

1,.PUSH

2.POP

3. DISPLAY

4. EXIT

enter the choice:1

3
enter the pushed elements :11

enter the choice:1

enter the pushed elements :22

enter the choice:1

enter the pushed elements :33

enter the choice:3

the elements are:33---->22---->11---->enter the choice:2

the poped elements is 33enter the choice:3

the elements are:22---->11---->

enter the choice:4

4
2) Stack using Linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head;
void push()
{
int data;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("unable to allocate memory");
}
else
{
printf("enter the elements:");
scanf("%d",&data);
if(head=='\0')
{
temp->data=data;
temp->link='\0';
head=temp;
}
else
{
temp->data=data;
temp->link=head;
head=temp;
}
}
}
void pop()
{
struct node *temp;
int item;
if(head==NULL)
{
printf("underflow");
}
else
{
item=head->data;
temp=head;
head=head->link;
free(temp);
}
printf("%d",item);
}
void display()
{
5
struct node *temp;
int i;
temp=head;
if(temp==NULL)
{
printf("\n STACK IS UNDERFL0W.");
}
else
{
printf("the elements are:");
while(temp!=NULL)
{
printf("%5d------------------->",temp->data);
temp=temp->link;
}
}
}

void display();
void push();
void pop();

int main()
{
int choice,data;
printf("STACK USING SINGLE LINKED LIST");
printf("----------------------\n");
printf("\n\t 1.PUSH \n\t 2.POP \n\t 3.DISPLAY \n\t 4.exit \n\t");
do
{
printf("enter the choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("exit point");
}
default:
{
6
printf("enter the valid choice (1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}

OUTPUT

STACK USING SINGLE LINKED LIST


----------------------

1.PUSH

2.POP

3.DISPLAY

4.exit

enter the choice 1

enter the elements:11

enter the choice 1

enter the elements:22

enter the choice 1

enter the elements:33

enter the choice 3

the elements are: 33-------------------> 22-------------------> 11


enter the choice 2
33enter the choice 3

the elements are: 22-----------------→11


enter the choice 4
exit point
enter the valid choice (1/2/3/4)

7
3) Queue using Array
#include<stdio.h>
#include<stdlib.h>
int rear,front,choice,i,size,item,queue[20];
void enqueue(void);
void dequeue(void);
void display(void);

int main()
{
front=0;
rear=0;
printf("enter the queue size:");
scanf("%d",&size);
printf("\nQUEUE OPERATION USING ARRAYS.");
printf("-----------------------------\n\t");
printf("1,.ENQUEUE \n\t 2. DEQUEUE \n\t 3. DISPLAY \n\t 4. EXIT \n\t");
do
{
printf("enter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
enqueue();
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("exit");
}
default:
{
printf("please enter the valid choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}

void enqueue()
{
8
printf("enter the enqueue elements :");
scanf("%d",&item);
if(rear==size)
{
printf("queue is overflow");
}
else
{
if(front==0&&rear==0)
{
front=1;
}
rear=rear+1;
queue[rear]=item;
}
}

void dequeue()
{
if(front==0)
{
printf("queue is underflow:");
}
else
{
item=queue[front];
if(front==rear)
{
rear=0;
front=0;
}
else
{
front=front+1;
}
}
printf("the dequeue elements is %d",item);
}

void display()
{
printf("the elements are:");
for(i=front;i<=rear;i++)
{
printf("%d---->",queue[i]);
}
}

OUTPUT

enter the queue size:3

9
QUEUE OPERATION USING ARRAYS.

-----------------------------

1,.ENQUEUE

2. DEQUEUE

3. DISPLAY

4. EXIT

enter the choice:1

enter the enqueue elements :11

enter the choice:1

enter the enqueue elements :22

enter the choice:1

enter the enqueue elements :33

enter the choice:1

enter the enqueue elements :44

queue is overflowenter the choice:2

the dequeue elements is 11enter the choice:3

the elements are:22---->33---->enter the choice:2

the dequeue elements is 22enter the choice:2

the dequeue elements is 33enter the choice:2

queue is underflow:the dequeue elements is 33enter the choice:4

exit please enter the valid choice(1/2/3/4)

10
4) Queue using Linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*front,*rear;

void enqueue()
{
int data;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("overflow");
}
else
{
printf("enter the elements:");
scanf("%d",&data);
temp->data=data;
if(front=='\0')
{
front=temp;
rear=temp;
front->link='\0';
rear->link='\0';
}
else
{
rear->link=temp;
rear=temp;
rear->link='\0';
}
}
}
void dequeue()
{
struct node *temp;
if(front==NULL)
{
printf("underflow");
}
else
{
temp=front;
front=front->link;
free(temp);
}
}
void display()
{
11
struct node *temp;
temp=front;
if(temp==NULL)
{
printf("\n QUEUE IS UNDERFL0W.");
}
else
{
printf("the elements are:");
while(temp!=NULL)
{
printf("%5d------------------->",temp->data);
temp=temp->link;
}
}
}

void display();
void enqueue();
void dequeue();

int main()
{
int choice;
printf("STACK USING SINGLE LINKED LIST");
printf("----------------------\n");
printf("\n\t 1.enqueue \n\t 2.dequeue \n\t 3.DISPLAY \n\t 4.exit \n\t");
do
{
printf("enter the choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
enqueue();
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("exit point");
}
default:
{
printf("enter the valid choice (1/2/3/4)");
12
}
}
}
while(choice!=4);
return 0;
}

//OUTPUT

STACK USING SINGLE LINKED LIST

----------------------

1.enqueue

2.dequeue

3.DISPLAY

4.exit

enter the choice 1

enter the elements:11

enter the choice 1

enter the elements:22

enter the choice 1

enter the elements:33

enter the choice 3

the elements are: 11-------------------> 22-------------------> 33------------------->

enter the choice 2

enter the choice 3

the elements are: 22-------------------> 33------------------->

enter the choice 4

exit point

enter the valid choice (1/2/3/4)

13
5) Linear Search
#include<stdio.h>
void main()
{
int i,found=0,n,location=0,key;
printf("enter the array size:");
scanf("%d",&n);
int a[n];
printf("enter the array elements :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the key value:");
scanf("%d",&key);
i=0;
while(i<n&&found==0)
{
if(a[i]==key)
{
found=1;
location=i;
}
else
{
i=i+1;
}
}
if(found==1)
{
printf("search is successfull.");
printf("key elements is=%d",key);
printf("location is =%d",location);
}
else
{
printf("search is unsuccessfull.");
}
}

//OUTPUT1

enter the array size:5

enter the array elements :4 1 7 2 9

enter the key value:2

search is successfull.

key elements is=2

location is =3

14
//OUTPUT2

enter the array size:4 1 7 2 9

enter the array elements :enter the key value:3

search is unsuccessfull.

15
6) Binary Search [Non Recursive]
#include<stdio.h>
void main()
{
int i,key,n,low,high,mid;
printf("enter the array size:");
scanf("%d",&n);
int a[n];
printf("enter the array elements in ascending order:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the key element:");
scanf("%d",&key);
low=0;
high=n-1;
while(high>=low)
{
mid=(low+high)/2;
if(key==a[mid])
{
break;
}
else
{
if(key>a[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
}
}
if(key!=a[mid])
printf("the key is not found");
else
printf("the key element is found at location=%d",mid+1);
}

OUTPUT1
enter the array size:5
enter the array elements in ascending order:2
4
6
8
10
enter the key element:4
the key element is found at location=2

16
OUTPUT2

enter the array size:5

enter the array elements in ascending order:2

enter the key element:7

the key is not found

17
7) Exchange Sort [Bubble Sort]
#include<stdio.h>
void main()
{
int n,i,j,t=0;
printf("enter the n value:");
scanf("%d",&n);
int a[n];
printf("enter the array values:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("elements are:");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}

//logic

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("sorted array elements are:");
for(j=0;j<n;j++)
{
printf("%d",a[j]);
}
}

OUTPUT

enter the n value:5

enter the array values:3 2 8 5 1

elements are:3 2 8 5 1

sorted array elements are: 1 2 3 5 8

18
8) Selection Sort
#include<stdio.h>
int main()
{
int n,i,j,temp,loc,min;
printf("enetr the array size:");
scanf("%d",&n);

int a[n];
printf("enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

//logic

for(i=0;i<n;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}

printf("array after sorting:");


for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
return 0;
}

OUTPUT

enter the array elements:

52814

array after sorting:1 2 4 5 8

19
9) Insertion Sort
#include<stdio.h>
int main()
{
int n,i,j,temp;
printf("enetr the array size:");
scanf("%d",&n);

int a[n];
printf("enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

//logic
for(i=0;i<n;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}

printf("array after sorting:");


for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
return 0;
}

OUTPUT:

enter the array size:4

enter the array elements:3 1 6 2

array after sorting:1 2 3 6

20
10) Quick sort [Non recursion]

#include<stdio.h>
void quick_sort(int[],int ,int );
int partition(int[],int ,int );
int main()
{
int n,i;
printf("enetr the array size:");
scanf("%d",&n);
int a[n];
printf("enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

quick_sort(a,0,n-1);

printf("array after sorting:");


for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}

void quick_sort(int a[],int l,int u)


{
int j;
if(l<u)
{
j=partition(a,l,u);
quick_sort(a,l,j-1);
quick_sort(a,j+1,u);
}
}

int partition(int a[],int l,int u)


{
int v,i,j,temp;
v=a[l];
i=l;
j=u+1;
do
{
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
{
21
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
while(i<j);
a[l]=a[j];
a[j]=v;
return j;
}

OUTPUT:

enter the array size:5

enter the array elements:4 1 9 2 7

array after sorting:1 2 4 7 9

22
11) Heap Sort
#include<stdio.h>
void create(int []);
void down_adjust(int [],int);
void main()
{
int n,i,last,temp;
printf("enter the array size:");
scanf("%d",&n);
int heap[n];
printf("enter the array elements:");
for(i=1;i<=n;i++)
{
scanf("%d",&heap[i]);
}
heap[0]=n;
create(heap);

while(heap[0]>1)
{
last=heap[0];
temp=heap[1];
heap[1]=heap[last];
heap[last]=temp;
heap[0]--;
down_adjust(heap,1);
}

printf("array after sorting:");


for(i=1;i<=n;i++)
{
printf("%d",heap[i]);
}
}
void create(int heap[])
{
int n,i;
n=heap[0];
for(i=n/2;i>=1;i--)
{

down_adjust(heap,i);
}
}

void down_adjust(int heap[],int i)


{
int j,temp,n,flag=1;
n=heap[0];
while(2*i<=n&&flag==1)
{
j=2*i;
if(j+1<=n&&heap[j+1]>heap[j])
j=j+1;
23
if(heap[i]>heap[j])
flag=0;
else
{
temp=heap[i];
heap[i]=heap[j];
heap[j]=temp;
i=j;
}
}
}
OUTPUT:

enter the array size: 5

enter the array elements:5 1 78 2 51

array after sorting:1 2 5 51 78

24
12) Conversion of infix to postfix

#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 :: ");


25
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 the expression :: 1+2

12+
26
13) Evaluation of postfix
#include<stdio.h>
int stack[20];
int top=-1;
void push(int x)
{
stack[++top]=x;
}
int pop()
{
stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1=1,n2=3,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;
}
}
27
push(n3);
}
e++;
}
printf("\n result of expression %s=%d\n",exp,pop());
return 0;
}

OUTPUT:

enter the expression:12-

result of expression 1 2 - = -1

28
14) Single linked list insertion
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head;
void createlist(int n);
void traverse();
void insertatbeginning(int data);
void insertatend(int data);
void insertatanyposition(int key,int data);
int main()
{
int n,data,choice,key;
printf("SINGLE LINKED LIST");
printf("----------------------\n");
printf("\n\t 1. createlist \n\t 2.traverse \n\t 3.insertatbeginning \n\t
4.insertatend \n\t 5.insertatanyposition \n\t 6.exit \n\t");
do
{
printf("enter the choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("enter the total number of nodes:");
scanf("%d",&n);
createlist(n);
break;
}
case 2:
{
traverse();
break;
}
case 3:
{
printf("enter the data to be insert at front:");
scanf("%d",&data);
insertatbeginning(data);
break;
}
case 4:
{
printf("enter the data to be inserted at last:");
scanf("%d",&data);
insertatend(data);
break;
}
case 5:
{
29
printf("enter the key:");
scanf("%d",&key);
printf("enter the data to be insert at desired position:");
scanf("%d",&data);
insertatanyposition(key,data);
break;
}
case 6:
{
printf("exit point");
}
default:
{
printf("enter the valid choice (1-6)");
}
}
}
while(choice!=6);
return 0;
}

void createlist(int n)
{
struct node *new,*ptr;
int data,i;
head=(struct node *)malloc(sizeof(struct node));
if(head==NULL)
{
printf("list is empty.");
}
printf("enter the data of node 1:");
scanf("%d",&data);
head->data=data;
head->link= NULL;
ptr=head;
for(i=2;i<=n;i++)
{
new=(struct node *)malloc(sizeof(struct node));
if(new==NULL)
{
printf("unable to allocate memory .");
}
printf("enter the data node %d:",i);
scanf("%d",&data);
new->data=data;
new->link=NULL;
ptr->link=new;
ptr=ptr->link;
}
}

void traverse()
{
struct node *ptr;
if(head==NULL)
30
{
printf("list is empty");
}
ptr=head;
while(ptr!=NULL)
{
printf("data-->=%d\t",ptr->data);
ptr=ptr->link;
}
}

void insertatbeginning(int data)


{
struct node *new;
new=(struct node *)malloc(sizeof(struct node));
if(new==NULL)
{
printf("unable to allocate memory");
}
else
{
new->data=data;
new->link=head;
head=new;
printf("data inserted successfully.");
}
}

void insertatend(int data)


{
struct node *new,*ptr;
new=(struct node *)malloc(sizeof(struct node));
if(new==NULL)
{
printf("unable to allocate memory.");
}
else
{
ptr=head;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=new;
new->data=data;
new->link=NULL;
}
}

void insertatanyposition(int key,int data)


{
struct node *new,*ptr;
new=(struct node *)malloc(sizeof(struct node));
if(new==NULL)
31
{
printf("unable to allocate memory.");
}
else
{
ptr=head;
while(ptr->data!=key&&ptr->link!=NULL)
{
ptr=ptr->link;
}
}
if(ptr->data!=key)
{
printf("key is not available in the list.");
}
else
{
new->link=ptr->link;
new->data=data;
ptr->link=new;
printf("data inserted successfully.");
}
}

OUTPUT:

SINGLE LINKED LIST--------------------

1. createlist

2.traverse

3.insertatbeginning

4.insertatend

5.insertatanyposition

6.exit

enter the choice :1

enter the total number of nodes:4

enter the data of node 1:11

enter the data node 2:22

enter the data node 3:33

enter the data node 4:44

enter the choice: 2

32
data-->=11 data-->=22 data-->=33 data-->=44

enter the choice:3

enter the data to be insert at front:55

data inserted successfully.

enter the choice: 2

data-->=55 data-->=11 data-->=22 data-->=33 data-->=44

enter the choice: 4

enter the data to be inserted at last:77

enter the choice: 2

data-->=55 data-->=11 data-->=22 data-->=33 data-->=44 data-->=77

enter the choice: 5

enter the key:44

enter the data to be insert at desired position:99

data inserted successfully.

enter the choice: 2

enter the key:44

enter the data to be insert at desired position:99

data inserted successfully.enter the choice 2

data-->=55 data-->=11 data-->=22 data-->=33 data-->=44 data-->=99 data-->=77

enter the choice: 6

exit point

enter the valid choice (1-6)

33
15) Single linked list Deletion
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head;
void createlist(int n);
void traverse();
void deleteatbeginning(void);
void deleteatend(void);
void deleteatanyposition(int position);
int main()
{
int n,data,choice,position;
printf("SINGLE LINKED LIST");
printf("----------------------\n");
printf("\n\t 1. createlist \n\t 2.traverse \n\t 3.deleteatbeginning \n\t
4.deleteatend \n\t 5.deleteatanyposition \n\t 6.exit \n\t");
do
{
printf("enter the choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("enter the total number of nodes:");
scanf("%d",&n);
createlist(n);
break;
}
case 2:
{
traverse();
break;
}
case 3:
{
deleteatbeginning();
break;
}
case 4:
{
deleteatend();
break;
}
case 5:
{
printf("enter the positon to be deleted:");
scanf("%d",&position);
deleteatanyposition(position);
break;
34
}
case 6:
{
printf("exit point");
}
default:
{
printf("enter the valid choice (1-6)");
}
}
}
while(choice!=6);
return 0;
}

void createlist(int n)
{
struct node *new,*ptr;
int data,i;
head=(struct node *)malloc(sizeof(struct node));
if(head==NULL)
{
printf("list is empty.");
}
printf("enter the data of node 1:");
scanf("%d",&data);
head->data=data;
head->link= NULL;
ptr=head;
for(i=2;i<=n;i++)
{
new=(struct node *)malloc(sizeof(struct node));
if(new==NULL)
{
printf("unable to allocate memory .");
}
printf("enter the data node %d:",i);
scanf("%d",&data);
new->data=data;
new->link=NULL;
ptr->link=new;
ptr=ptr->link;
}
}

void traverse()
{
struct node *ptr;
if(head==NULL)
{
printf("list is empty");
}
ptr=head;
while(ptr!=NULL)
{
35
printf("data-->=%d\t",ptr->data);
ptr=ptr->link;
}
}

void deleteatbeginning()
{
struct node *ptr;
if(head==NULL)
{
printf("list is empty.");
}
else
{
ptr=head;
head=head->link;
printf("data deleted successfully.");
}
}

void deleteatend()
{
struct node *ptr,*ptr1;
if(head==NULL)
{
printf("list is empty.");
}
else
{
ptr=head;
ptr1=head;
while(ptr->link==NULL)
{
ptr1=ptr;
ptr=ptr->link;
}
if(ptr==head)
{
head=NULL;
}
else
{
ptr1->link=NULL;
}
printf("data deleted successfully:");
}
}

void deleteatanyposition(int position)


{
int i,n;
struct node *ptr,*ptr1;

if(head==NULL)
36
{
printf("list is empty.");
}
else
{
ptr=head;
ptr1=head;
for(i=2;i<=n;i++)
{
ptr1=ptr;
ptr=ptr->link;
if(ptr==NULL)
break;
}
if(ptr!=NULL)
{
if(ptr==head)
{
head=head->link;
ptr->link=ptr->link;
ptr1->link=NULL;
free(ptr);
}
else
{
printf("invalid position unable to delete.");
}
}
}
}

OUTPUT:

SINGLE LINKED LIST----------------------

1. createlist

2.traverse

3.deleteatbeginning

4.deleteatend

5.deleteatanyposition

6.exit

enter the choice 1

enter the total number of nodes:4

enter the data of node 1:1

enter the data node 2:2

37
enter the data node 3:3

enter the data node 4:4

enter the choice 2

data-->=1 data-->=2 data-->=3 data-->=4 enter the choice 3

data deleted successfully.enter the choice 2

data-->=2 data-->=3 data-->=4 enter the choice 4

data deleted successful.

Data→=2 data→=3

Enter the choice 5

Enter the position to be deleted:2

Data deleted successfully

Data→=2

38
16) Single linked list Traverse
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head;
void createlist(int n);
void traverse();

int main()
{
int n,data;
printf("enter the total number of nodes:");
scanf("%d",&n);
createlist(n);
printf("\n data in the list.");
traverse();
return 0;
}

void createlist(int n)
{
struct node *new,*ptr;
int data,i;
head=(struct node *)malloc(sizeof(struct node));
if(head==NULL)
{
printf("list is empty.");
}
printf("enter the data of node 1:");
scanf("%d",&data);
head->data=data;
head->link= NULL;
ptr=head;
for(i=2;i<=n;i++)
{
new=(struct node *)malloc(sizeof(struct node));
if(new==NULL)
{
printf("unable to allocate memory .");
}
printf("enter the data node %d:",i);
scanf("%d",&data);
new->data=data;
new->link=NULL;
ptr->link=new;
ptr=ptr->link;
}
}

void traverse()
{
39
struct node *ptr;
if(head==NULL)
{
printf("list is empty");
}
else
{
ptr=head;
while(ptr!=NULL)
{
printf("%d-------->",ptr->data);
ptr=ptr->link;
}
}
}

OUTPUT:

enter the total number of nodes:4

enter the data of node 1:1

enter the data node 2:2

enter the data node 3:3

enter the data node 4:4

data in the list. 1--------> 2--------> 3--------> 4-------->

40
17) Removing duplicate elements(ordered & unordered)
#include<stdio.h>
void main()
{
int n,i,j,k;
printf("enetr the array size:");
scanf("%d",&n);

int a[n];
printf("enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

printf("\n original array is:\n");


for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
printf("\n New array is :\n");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;
}
else
{
j++;
}
}
}
printf("array after sorting:");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}

OUTPUT:
enetr the array size:4

enter the array elements:1 6 2 1


original array is:

1
6
41
2
1

New array is :
Array after sorting:
1 2 6

42
18) Matrix Multiplication
#include<stdio.h>
void main()
{
int i,m,n,p,q,k,j,sum=0;
printf("enter the rows and columns size:");
scanf("%d %d",&m,&n);
int a[m][n],c[m][n];
printf("enter the array elements :");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d\t",&a[i][j]);
}
}
printf("enter the row and cloumn size:");
scanf("%d %d",&p,&q);
int b[p][q];
if(n!=p)
{
printf("matrix is not multiplied.");
}
else
{
printf("enter the elements are:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d\t",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
sum=sum+(a[i][k]*b[k][j]);
}
c[i][j]=sum;
sum=0;
}
}
printf("\nproduct of matrices are:");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
}
43
}

OUTPUT:
enter the rows and columns size:2 2
enter the array elements :1

enter the row and cloumn size:2 2

enter the elements are:1

4
Product of matrices are:7 10 15 22

44
19) Quick sort(Recursion)
#include<stdio.h>

int a[50];

void qsort(int,int);

int split(int,int);

int main()

int n,i;

printf("How many elements?");

scanf("%d",&n);

printf("Enter %d elements:\n",n);

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

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

qsort(0,n-1);

printf("The resultant array:\n");

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

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

return 0;

void qsort(int start,int end)

int s;

if(start>=end)

return;

s=split(start,end);

qsort(start,s-1);

qsort(s+1,end);

int split(int start,int end)

45
{

int p=a[start];

int i=start,j=end,temp;

while(i<j)

while(a[i]<=p)

i++;

while(a[j]>p)

j--;

if(i<j)

temp=a[i],a[i]=a[j],a[j]=temp;

a[start]=a[j];

a[j]=p;

return j;

OUTPUT:

How many elements?9


Enter 9 elements:
40 90 60 5 13 10 20 45 50
The resultant array:
5 10 13 20 40 45 50 60 90

46
20) Binary Search [Recursive]
#include<stdio.h>
int binarysearch(int a[],int low,int high,int x)
{
int mid=(low+high)/2;
if(low>high)

return(-1);

if(a[mid]==x)
return (mid);
if(a[mid]<x)
return binarysearch(a,mid+1,high,x);
else
return binarysearch(a,low,mid-1,x);
}
int main()
{
int pos,search_item,len,i;
printf("enter the array size:");
scanf("%d",&len);

int a[len];
printf("enter the array elements:");
for(i=0;i<len;i++)
{
scanf("%d",&a[i]);
}
printf("enter the elements to search");
scanf("%d",&search_item);
pos=binarysearch(a,0,len-1,search_item);

if(pos<0)
printf("cannot find the element %d in array\n",search_item);
else
printf("position of %d in the array is %d",search_item,pos+1);
return 0;
}

OUTPUT:

enter the array size:4

enter the array elements:1

enter the elements to search3

position of 3 in the array is 3


47

You might also like