Professional Documents
Culture Documents
INDEX
S No. Programs Signature
1. Write a program for traversing an array.
2. Write a program for linear search.
3. Write a program for binary search.
4. Write a program for bubble sorting.
5. Write a program for insertion sorting.
6. Write a program for selection sorting.
7. Write a program for merge sorting.
8. Write a program for inserting an element in an array.
9. Write a program for deleting an element in an array.
10. Write a program to print a sphere matrix.
11. Write a program for linked list.
12. Write a program for searching in singly linked list.
13. Write a program to insert and search in a link list.
14. Write a program for delete an item in link list.
15. Write a program for delete a node in link list.
16. Write a program to divide a link list into two parts.
17. Write a program for insertion in circular link list.
18. Write a program for searching in circular link list.
19. Write a program for insertion element at beginning
random and end.
20. Write a program for reversing a link list.
21. Write a program to search a node in doubly link list.
22. Write a program for sorted link list.
23. Write a program to perform stack function using
push, pop, peek.
24. Write a program for stack postfix and infix.
25. Write a program to insert and delete an item from
Queue.
26. Write a program to implement circular Queue.
27. Write a program to implement Queue using link list.
Program 1:
Source Code:
#include<stdio.h>
int main()
int n;
scanf("%i",&n);
int a[n],b;
scanf("%i",&a[i]);
printf("%i\n",a[j]);
Output:
Program 2:
Source Code:
#include<stdio.h>
int main()
int a[5],b;
scanf("%i",&a[i]);
search(a);
return 0;
int b;
scanf("%i",&b);
if (a[j]==b)
return 0;
}
Output:
Program 3:
Source Code:
#include<stdio.h>
int main()
int n;
scanf("%i",&n);
int a[n],b;
scanf("%i",&a[i]);
scanf("%i",&b);
int first=0,mid;
int last=n;
while (first<=last)
{
if (a[mid]==b)
break;
else if (b>a[mid])
first=mid+1;
else if (b<a[mid])
last=mid-1;
mid=(first+last)/2;
return 0;
Output:
Program 4:
Source Code:
#include<stdio.h>
int main()
int n,t=1;
scanf("%i",&n);
int a[n],b;
scanf("%i",&a[i]);
if (a[j]>a[j+1])
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
printf("%i\n",a[j]);
}
}
Output:
Program 5:
Source Code:
#include <stdio.h>
int main()
int i,j,n,a[100],temp;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
{ if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
printf("Sorted array:");
for(i=0;i<n;i++){
printf("\n%d",a[i]);
return 0;
Output:
Program 6:
Source Code:
#include <stdio.h>
int main() {
int n,t=1;
scanf("%i",&n);
int a[n],b;
scanf("%i",&a[i]);
position = i;
position = j;
if (position != i) {
swap = a[i];
a[i] = a[position];
a[position] = swap;
printf("%i\n",a[j]);
Output:
Program 7:
Source Code:
#include <stdio.h>
int i, j, k;
i = 0;
j = 0;
k12
= beg;
while (i < n1 && j < n2)
a[k] = LeftArray[i];
i++;
else
a[k] = RightArray[j];
j++;
k++;
while (i<n1)
a[k] = LeftArray[i];
i++;
k++;
while (j<n2)
a[k] = RightArray[j];
j++;
k++;
int i;
printf("\n");
int main()
int a[5],b;
scanf("%i",&a[i]);
printArray(a, n);
mergeSort(a, 0, n - 1);
return 0;
Output:
Program 8:
Source Code:
#include<stdio.h>
int main()
int n;
scanf("%i",&n);
int a[n],b;
scanf("%i",&a[i]);
printf("%i\n",a[j]);
}
return 0;
Output:
Program 9:
Source Code:
#include<stdio.h>
int main()
int a[10],b;
scanf("%i",&a[i]);
scanf("%i",&b);
if (f>=b)
a[f]=a[f+1];
for(int j=0;j<=5;j++)
printf("%i\n",a[j]);
return 0;
Output:
Program 10:
Source Code:
Output:
Program 11:
Source Code:
/*link list*/
#include <stdio.h>
#include <stdlib.h>
struct node
int num;
}*stnode;
void displayList();
int main()
int n;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
scanf("%d", &n);
createNodeList(n);
displayList();
return 0;
}
void createNodeList(int n)
int num, i;
if(stnode == NULL)
else
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
tmp = stnode;
if(fnNode == NULL)
break;
else
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
void displayList()
if(stnode == NULL)
else
tmp = stnode;
while(tmp != NULL)
tmp = tmp->nextptr;
Output:
Program 12:
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node
int num;
stnode, *ennode;
int FindElement(int);
void main()
int n,i,FindElem,FindPlc;
stnode.nextptr=NULL;
ennode=&stnode;
printf("---------------------------------------------------------------\n");
scanf("%d", &n);
printf("\n");
for(i=0;i< n;i++)
scanf("%d",&ennode->num);
ennode=ennode->nextptr;
ennode->nextptr=NULL;
ennode=&stnode;
while(ennode->nextptr!=NULL)
ennode=ennode->nextptr;
printf("\n");
scanf("%d",&FindElem);
FindPlc=FindElement(FindElem);
if(FindPlc<=n)
else
printf(" This element does not exists in linked list.\n\n");
int ctr=1;
ennode=&stnode;
while(ennode->nextptr!=NULL)
if(ennode->num==FindElem)
break;
else
ctr++;
ennode=ennode->nextptr;
return ctr;
Output:
Program 13:
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node
int num;
stnode, *ennode;
int FindElement(int);
void main()
int n,i,FindElem,FindPlc;
stnode.nextptr=NULL;
ennode=&stnode;
printf("---------------------------------------------------------------\n");
scanf("%d", &n);
printf("\n");
for(i=0;i< n;i++)
ennode=ennode->nextptr;
ennode->nextptr=NULL;
ennode=&stnode;
while(ennode->nextptr!=NULL)
ennode=ennode->nextptr;
printf("\n");
scanf("%d",&FindElem);
FindPlc=FindElement(FindElem);
if(FindPlc<=n)
else
int ctr=1;
ennode=&stnode;
while(ennode->nextptr!=NULL)
if(ennode->num==FindElem)
break;
else
ctr++;
ennode=ennode->nextptr;
return ctr;
Output:
Program 14:
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num; //Data of the node
}*stnode;
int main()
int n,num,pos;
printf("\n\n Linked List : Delete the last node of Singly Linked List :\n");
printf("---------------------------------------------------------------\n");
scanf("%d", &n);
createNodeList(n);
displayList();
LastNodeDeletion();
printf("\n The new list after deletion the last node are : \n");
displayList();
return 0;
void createNodeList(int n)
int num, i;
else
scanf("%d", &num);
tmp = stnode;
if(fnNode == NULL) //check whether the fnnode is NULL and if so no memory allocation
break;
else
tmp = tmp->nextptr;
}
void LastNodeDeletion()
if(stnode == NULL)
else
toDelLast = stnode;
preNode = stnode;
while(toDelLast->nextptr != NULL)
preNode = toDelLast;
toDelLast = toDelLast->nextptr;
if(toDelLast == stnode)
stnode = NULL;
else
preNode->nextptr = NULL;
}
free(toDelLast);
void displayList()
if(stnode == NULL)
else
tmp = stnode;
while(tmp != NULL)
Output:
Program 15:
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node
}*stnode;
int n,num,pos;
printf("\n\n Linked List : Delete a node from the middle of Singly Linked List. :\n");
printf("-------------------------------------------------------------------------\n");
scanf("%d", &n);
createNodeList(n);
displayList();
scanf("%d", &pos);
if(pos<=1 || pos>=n)
MiddleNodeDeletion(pos);
displayList();
return 0;
}
void createNodeList(int n)
int num, i;
else
scanf("%d", &num);
tmp = stnode;
break;
}
else
tmp = tmp->nextptr;
int i;
if(stnode == NULL)
else
toDelMid = stnode;
preNode = stnode;
preNode = toDelMid;
toDelMid = toDelMid->nextptr;
if(toDelMid == NULL)
break;
if(toDelMid != NULL)
if(toDelMid == stnode)
stnode = stnode->nextptr;
preNode->nextptr = toDelMid->nextptr;
toDelMid->nextptr = NULL;
free(toDelMid);
else
void displayList()
if(stnode == NULL)
else
{
tmp = stnode;
while(tmp != NULL)
Output:
Program 16:
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct Node
int data;
};
while (ptr)
printf("%d\n", ptr->data);
ptr = ptr->next;
printf("NULL");
// Helper function to insert a new node at the beginning of the linked list
newNode->data = data;
newNode->next = *head;
*head = newNode;
int count = 0;
count++;
current=current->next;
return count;
/*
Split the given list's nodes into front and back halves
If the length is odd, the extra node should go in the front list.
*/
void frontBackSplit(struct Node* source, struct Node** frontRef, struct Node** backRef)
if (len < 2)
*frontRef = source;
*backRef = NULL;
return;
}
struct Node* current = source;
current = current->next;
*frontRef = source;
*backRef = current->next;
current->next = NULL;
int main(void)
// input keys
int n = sizeof(keys)/sizeof(keys[0]);
push(&head, keys[i]);
printList(a);
printList(b);
return 0;
Output:
Program 17:
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
}*stnode;
int main()
int n,num1,a,insPlc;
stnode = NULL;
printf("\n\n Circular Linked List : Insertion of data in circular linked list :\n");
printf("--------------------------------------------------------------------------------\n");
scanf("%d", &n);
ClListcreation(n);
a=1;
displayClList(a);
return 0;
void ClListcreation(int n)
int i, num;
if(n >= 1)
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
scanf("%d", &num);
newnode->num = num;
void displayClList(int m)
int n = 1;
if(stnode == NULL)
else
tmp = stnode;
if (m==1)
else
do {
tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
Output:
Program 18:
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
}*stnode,*ennode;
void displayClList();
int main()
int n,m;
int i,FindElem,FindPlc;
stnode = NULL;
ennode = NULL;
printf("\n\n Circular Linked List : Search an element in a circular linked list :\n");
printf("-------------------------------------------------------------------------\n");
scanf("%d", &n);
m=n;
ClListcreation(n);
displayClList();
scanf("%d", &FindElem);
FindPlc=FindElement(FindElem,m);
if(FindPlc<n)
return 0;
void ClListcreation(int n)
int i, num;
if(n >= 1)
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
scanf("%d", &num);
newnode->num = num;
int ctr=1;
ennode=stnode;
while(ennode->nextptr!=NULL)
if(ennode->num==FindElem)
break;
else
ctr++;
ennode=ennode->nextptr;
if (ctr==a+1)
break;
return ctr;
void displayClList()
int n = 1;
if(stnode == NULL)
else
{
tmp = stnode;
do {
tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
Output:
Program 19:
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
}*stnode;
int main()
int n,num1,a,insPlc;
stnode = NULL;
printf("\n\n Circular Linked List : Insert a node at the beginning random and end of a circular
linked list :\n");
printf("--------------------------------------------------------------------------------\n");
scanf("%d", &n);
ClListcreation(n);
a=1;
displayClList(a);
int choice=0;
printf("1. beginning\n");
printf("3. end\n");
printf("please enter the choice: ");
scanf("%i",&choice);
switch (choice)
case 3:
scanf("%d", &num1);
ClLinsertNodeAtEnd(num1);
a=2;
displayClList(a);
break;
case 2:
scanf("%d", &insPlc);
scanf("%d", &num1);
ClLinsertNodeAtAny(num1,insPlc);
a=2;
displayClList(a);
case 1:
scanf("%d", &num1);
ClLinsertNodeAtBeginning(num1);
a=2;
displayClList(a);
break;
default:
break;
return 0;
void ClListcreation(int n)
int i, num;
if(n >= 1)
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
scanf("%d", &num);
newnode->num = num;
if(stnode == NULL)
else
newnode->num = num;
newnode->nextptr = stnode;
curNode = stnode;
while(curNode->nextptr != stnode)
curNode = curNode->nextptr;
curNode->nextptr = newnode;
stnode = newnode;
int i;
if(stnode == NULL)
else if(pos == 1)
ClLinsertNodeAtBeginning(num);
else
newnode->num = num;
curNode = stnode;
curNode = curNode->nextptr;
newnode->nextptr = curNode->nextptr;
curNode->nextptr = newnode;
int a;
a=num1;
temp->num=a;
p=stnode;
while(p->nextptr!=stnode)
{
p=p->nextptr;
p->nextptr=temp;
temp->nextptr=stnode;
void displayClList(int m)
int n = 1;
if(stnode == NULL)
else
tmp = stnode;
if (m==1)
else
do {
tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
Output:
Program 20:
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node
}*stnode;
int main()
int n;
printf("\n\n Linked List : Create a singly linked list and print it in reverse order :\n");
printf("------------------------------------------------------------------------------\n");
scanf("%d", &n);
createNodeList(n);
displayList();
reverseDispList();
displayList();
return 0;
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
if(stnode == NULL) //check whether the stnode is NULL and if so no memory allocation
else
scanf("%d", &num);
tmp = stnode;
if(fnNode == NULL) //check whether the fnnode is NULL and if so no memory allocation
break;
else
void reverseDispList()
if(stnode != NULL)
prevNode = stnode;
curNode = stnode->nextptr;
stnode = stnode->nextptr;
while(stnode != NULL)
stnode = stnode->nextptr;
curNode->nextptr = prevNode;
prevNode = curNode;
curNode = stnode;
void displayList()
{
if(stnode == NULL)
else
tmp = stnode;
while(tmp != NULL)
Output:
Program 21:
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
};
void printList() {
printf("\n[head] <=>");
while(ptr != NULL) {
printf(" %d <=>",ptr->data);
ptr = ptr->next;
printf(" [last]\n");
link->data = data;
link->prev = NULL;
link->next = NULL;
if(head==NULL) {
head = link;
return;
current = head;
while(current->next!=NULL)
current = current->next;
current->next = link;
last = link;
link->prev = current;
int pos = 0;
if(head==NULL) {
return;
}
current = head;
while(current!=NULL) {
pos++;
if(current->data == data) {
return;
if(current->next != NULL)
current = current->next;
else
break;
int main() {
insert(10);
insert(20);
insert(30);
insert(1);
insert(40);
insert(56);
printList();
search(56);
return 0;
}
Output:
Program 22:
Source Code:
// Licence: https://bit.ly/2JK1psc
#include<stdio.h>
#include <stdlib.h>
struct node
int data;
};
int main()
int n,k,i,j;
startList=NULL;
scanf("%d",&n);
for(i=1;i<=n;i++)
if(startList==NULL)
{
scanf("%d",&newNode->data);
newNode->next=NULL;
startList = newNode;
temp1=startList;
else
scanf("%d",&newNode->data);
newNode->next=NULL;
temp1->next = newNode;
temp1=newNode;
for(i=n-2;i>=0;i--)
temp1=startList;
temp2=temp1->next;
for(j=0;j<=i;j++)
k=temp1->data;
temp1->data=temp2->data;
temp2->data=k;
temp1=temp2;
temp2=temp2->next;
}
}
t=startList;
while(t!=NULL)
printf("%d\t",t->data);
t=t->next;
Output:
Program 23:
Source Code:
#include<stdio.h>
#include<stdlib.h>
#define Size 4
void Push();
void Pop();
void show();
int main()
int choice;
while(1)
scanf("%d",&choice);
switch(choice)
case 1: Push();
break;
case 2: Pop();
break;
case 3: show();
break;
case 4: exit(0);
void Push()
int x;
if(Top==Size-1)
printf("\nOverflow!!");
else
scanf("%d",&x);
Top=Top+1;
inp_array[Top]=x;
void Pop()
if(Top==-1)
printf("\nUnderflow!!");
else
Top=Top-1;
void show()
if(Top==-1)
printf("\nUnderflow!!");
}
else
for(int i=Top;i>=0;--i)
printf("%d\n",inp_array[i]);
Output:
Program 24:
Source Code:
#include<stdio.h>
#include<ctype.h>
char stack[100];
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == '(')
return 0;
return 1;
return 2;
return 0;
int main()
char exp[100];
char *e, x;
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c ",*e);
push(*e);
else
printf("%c ",pop());
push(*e);
e++;
while(top != -1)
printf("%c ",pop());
}return 0;
Output:
Program 25:
Source Code:
/*
*/
#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("4.Quit \n");
scanf("%d", &choice);
switch (choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert()
int add_item;
if (rear == MAX - 1)
else
if (front == - 1)
front = 0;
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
} /* End of insert() */
void delete()
{
if (front == - 1 || front > rear)
return ;
else
front = front + 1;
} /* End of delete() */
void display()
int i;
if (front == - 1)
printf("Queue is : \n");
printf("\n");
} /* End of display() */
Output:
Program 26:
Source Code:
# include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
/*Begin of insert*/
front = 0;
rear = 0;
else
rear = 0;
else
rear = rear+1;
cqueue_arr[rear] = item ;
/*End of insert*/
/*Begin of del*/
void del()
if (front == -1)
printf("Queue Underflow\n");
return ;
front = -1;
rear=-1;
else
{
if(front == MAX-1)
front = 0;
else
front = front+1;
/*End of del() */
/*Begin of display*/
void display()
if(front == -1)
printf("Queue is empty\n");
return;
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
else
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");
/*End of display*/
/*Begin of main*/
int main()
int choice,item;
do
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
scanf("%d",&choice);
switch(choice)
case 1 :
scanf("%d", &item);
insert(item);
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice\n");
}while(choice!=4);
return 0;
/*End of main*/
Output:
Program 27:
Source Code:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void insert();
void delete();
void display();
void main ()
int choice;
while(choice != 4)
printf("\n*************************Main Menu*****************************\n");
printf("\n=================================================================\n");
scanf("%d",& choice);
switch(choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
}
void insert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW\n");
return;
else
printf("\nEnter value?\n");
scanf("%d",&item);
if(front == NULL)
front = ptr;
rear = ptr;
else
rear = ptr;
rear->next = NULL;
void delete ()
{
struct node *ptr;
if(front == NULL)
printf("\nUNDERFLOW\n");
return;
else
ptr = front;
free(ptr);
void display()
ptr = front;
if(front == NULL)
printf("\nEmpty queue\n");
else
while(ptr != NULL)
Output: