You are on page 1of 85

DSA PRACTICAL FILE

By: Vipul Saraswat


BCA Shift – 1

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;

printf("please enter the size of array: ");

scanf("%i",&n);

int a[n],b;

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

printf("please enter the no. ");

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

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

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

Output:
Program 2:
Source Code:
#include<stdio.h>

int search(int a[]);

int main()

int a[5],b;

for (int i=1;i<=5;i++)

printf("please enter the no. ");

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

search(a);

return 0;

int search(int a[])

int b;

printf("please enter the no to be searched: ");

scanf("%i",&b);

for (int j=1;j<=5;j++)

if (a[j]==b)

printf("the no found on %i location of the array.",j);

return 0;

}
Output:

Program 3:
Source Code:
#include<stdio.h>

int main()

int n;

printf("please enter the size of array: ");

scanf("%i",&n);

int a[n],b;

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

printf("please enter the no. ");

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

printf("please enter the no to be searched : ");

scanf("%i",&b);

int first=0,mid;

int last=n;

mid = (first + last)/2;

while (first<=last)

{
if (a[mid]==b)

printf("the number found at %i.",mid-1);

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;

printf("please enter the size of array: ");

scanf("%i",&n);

int a[n],b;

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

printf("please enter the no. ");

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

for (int l=0;l<n-1;l++)

for (int j=0;j<n-l-1;j++)

if (a[j]>a[j+1])

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

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

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

}
}

Output:

Program 5:
Source Code:
#include <stdio.h>

int main()

int i,j,n,a[100],temp;

printf("Enter the number of elements you want to insert in the array:");

scanf("%d",&n);

printf("Enter the array elements:");

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;

printf("please enter the size of array: ");

scanf("%i",&n);
int a[n],b;

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

printf("please enter the no. ");

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

int i, j, position, swap;

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

position = i;

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

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

position = j;

if (position != i) {

swap = a[i];

a[i] = a[position];

a[position] = swap;

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

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

Output:
Program 7:
Source Code:
#include <stdio.h>

void merge(int a[], int beg, int mid, int end)

int i, j, k;

int n1 = mid - beg + 1;

int n2 = end - mid;

int LeftArray[n1], RightArray[n2];

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

LeftArray[i] = a[beg + i];

for (int j = 0; j < n2; j++)

RightArray[j] = a[mid + 1 + j];

i = 0;

j = 0;

k12

= beg;
while (i < n1 && j < n2)

if(LeftArray[i] <= RightArray[j])

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++;

void mergeSort(int a[], int beg, int end)


{

if (beg < end)

int mid = (beg + end) / 2;

mergeSort(a, beg, mid);

mergeSort(a, mid + 1, end);

merge(a, beg, mid, end);

void printArray(int a[], int n)

int i;

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

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

printf("\n");

int main()

int a[5],b;

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

printf("please enter the no. ");

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

int n = sizeof(a) / sizeof(a[0]);

printf("Before sorting array elements are - \n");

printArray(a, n);

mergeSort(a, 0, n - 1);

printf("After sorting array elements are - \n");


printArray(a, n);

return 0;

Output:

Program 8:
Source Code:
#include<stdio.h>

int main()

int n;

printf("please enter the size of array: ");

scanf("%i",&n);

int a[n],b;

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

printf("please enter the no. ");

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

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

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

return 0;

Output:

Program 9:
Source Code:
#include<stdio.h>

int main()

int a[10],b;

for (int i=0;i<=5;i++)//1

printf("please enter the no. ");

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

printf("please enter the position for the no to be deleted: ");//2

scanf("%i",&b);

for (int f=0;f<=5;f++)


{

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;

struct node *nextptr;

}*stnode;

void createNodeList(int n);

void displayList();

int main()

int n;

printf("\n\n Linked List : To create and display Singly Linked List :\n");

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

printf(" Input the number of nodes : ");

scanf("%d", &n);

createNodeList(n);

printf("\n Data entered in the list : \n");

displayList();

return 0;

}
void createNodeList(int n)

struct node *fnNode, *tmp;

int num, i;

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

if(stnode == NULL)

printf(" Memory can not be allocated.");

else

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode->num = num;

stnode->nextptr = NULL;

tmp = stnode;

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

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

if(fnNode == NULL)

printf(" Memory can not be allocated.");

break;

else

printf(" Input data for node %d : ", i);

scanf(" %d", &num);


fnNode->num = num;

fnNode->nextptr = NULL;

tmp->nextptr = fnNode;

tmp = tmp->nextptr;

void displayList()

struct node *tmp;

if(stnode == NULL)

printf(" List is empty.");

else

tmp = stnode;

while(tmp != NULL)

printf(" Data = %d\n", tmp->num);

tmp = tmp->nextptr;

Output:
Program 12:
Source Code:
#include <stdio.h>

#include <stdlib.h>

struct node

int num;

struct node *nextptr;

stnode, *ennode;

int FindElement(int);

void main()

int n,i,FindElem,FindPlc;

stnode.nextptr=NULL;
ennode=&stnode;

printf("\n\n Linked List : Search an element in a Singly Linked List :\n");

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

printf(" Input the number of nodes : ");

scanf("%d", &n);

printf("\n");

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

ennode->nextptr=(struct node *)malloc(sizeof(struct node));

printf(" Input data for node %d : ",i+1);

scanf("%d",&ennode->num);

ennode=ennode->nextptr;

ennode->nextptr=NULL;

printf("\n Data entered in the list are :\n");

ennode=&stnode;

while(ennode->nextptr!=NULL)

printf(" Data = %d\n",ennode->num);

ennode=ennode->nextptr;

printf("\n");

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

scanf("%d",&FindElem);

FindPlc=FindElement(FindElem);

if(FindPlc<=n)

printf(" Element found at node %d \n\n",FindPlc);

else
printf(" This element does not exists in linked list.\n\n");

int FindElement(int FindElem)

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;

struct node *nextptr;

stnode, *ennode;

int FindElement(int);

void main()

int n,i,FindElem,FindPlc;

stnode.nextptr=NULL;

ennode=&stnode;

printf("\n\n Linked List : Search an element in a Singly Linked List :\n");

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

printf(" Input the number of nodes : ");

scanf("%d", &n);

printf("\n");

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

ennode->nextptr=(struct node *)malloc(sizeof(struct node));

printf(" Input data for node %d : ",i+1);


scanf("%d",&ennode->num);

ennode=ennode->nextptr;

ennode->nextptr=NULL;

printf("\n Data entered in the list are :\n");

ennode=&stnode;

while(ennode->nextptr!=NULL)

printf(" Data = %d\n",ennode->num);

ennode=ennode->nextptr;

printf("\n");

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

scanf("%d",&FindElem);

FindPlc=FindElement(FindElem);

if(FindPlc<=n)

printf(" Element found at node %d \n\n",FindPlc);

else

printf(" This element does not exists in linked list.\n\n");

int FindElement(int FindElem)

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

struct node *nextptr; //Address of the node

}*stnode;

void createNodeList(int n); //function to create the list

void LastNodeDeletion(); //function to delete the last nodes

void displayList(); //function to display the list

int main()

int n,num,pos;

printf("\n\n Linked List : Delete the last node of Singly Linked List :\n");

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

printf(" Input the number of nodes : ");

scanf("%d", &n);

createNodeList(n);

printf("\n Data entered in the list are : \n");

displayList();

LastNodeDeletion();

printf("\n The new list after deletion the last node are : \n");

displayList();

return 0;

void createNodeList(int n)

struct node *fnNode, *tmp;

int num, i;

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


if(stnode == NULL) //check whether the stnode is NULL and if so no memory allocation

printf(" Memory can not be allocated.");

else

// reads data for the node through keyboard

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode-> num = num;

stnode-> nextptr = NULL; //Links the address field to NULL

tmp = stnode;

//Creates n nodes and adds to linked list

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

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

if(fnNode == NULL) //check whether the fnnode is NULL and if so no memory allocation

printf(" Memory can not be allocated.");

break;

else

printf(" Input data for node %d : ", i);

scanf(" %d", &num);

fnNode->num = num; // links the num field of fnNode with num

fnNode->nextptr = NULL; // links the address field of fnNode with NULL

tmp->nextptr = fnNode; // links previous node i.e. tmp to the fnNode

tmp = tmp->nextptr;
}

// Deletes the last node of the linked list

void LastNodeDeletion()

struct node *toDelLast, *preNode;

if(stnode == NULL)

printf(" There is no element in the list.");

else

toDelLast = stnode;

preNode = stnode;

/* Traverse to the last node of the list*/

while(toDelLast->nextptr != NULL)

preNode = toDelLast;

toDelLast = toDelLast->nextptr;

if(toDelLast == stnode)

stnode = NULL;

else

/* Disconnects the link of second last node with last node */

preNode->nextptr = NULL;
}

/* Delete the last node */

free(toDelLast);

// function to display the entire list

void displayList()

struct node *tmp;

if(stnode == NULL)

printf(" No data found in the empty list.");

else

tmp = stnode;

while(tmp != NULL)

printf(" Data = %d\n", tmp->num); // prints the data of current node

tmp = tmp->nextptr; // advances the position of current node

Output:
Program 15:
Source Code:
#include <stdio.h>

#include <stdlib.h>

struct node

int num; //Data of the node

struct node *nextptr; //Address of the node

}*stnode;

void createNodeList(int n); //function to create the list

void MiddleNodeDeletion(int pos); //function to delete a node from middle

void displayList(); //function to display the list


int main()

int n,num,pos;

printf("\n\n Linked List : Delete a node from the middle of Singly Linked List. :\n");

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

printf(" Input the number of nodes : ");

scanf("%d", &n);

createNodeList(n);

printf("\n Data entered in the list are : \n");

displayList();

printf("\n Input the position of node to delete : ");

scanf("%d", &pos);

if(pos<=1 || pos>=n)

printf("\n Deletion can not be possible from that position.\n ");

if(pos>1 && pos<n)

printf("\n Deletion completed successfully.\n ");

MiddleNodeDeletion(pos);

printf("\n The new list are : \n");

displayList();

return 0;

}
void createNodeList(int n)

struct node *fnNode, *tmp;

int num, i;

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

if(stnode == NULL) //check whether the stnode is NULL and if so no memory


allocation

printf(" Memory can not be allocated.");

else

// reads data for the node through keyboard

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode-> num = num;

stnode-> nextptr = NULL; //Links the address field to NULL

tmp = stnode;

//Creates n nodes and adds to linked list

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

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

if(fnNode == NULL) //check whether the fnnode is NULL and if so no memory


allocation

printf(" Memory can not be allocated.");

break;
}

else

printf(" Input data for node %d : ", i);

scanf(" %d", &num);

fnNode->num = num; // links the num field of fnNode with num

fnNode->nextptr = NULL; // links the address field of fnNode with NULL

tmp->nextptr = fnNode; // links previous node i.e. tmp to the fnNode

tmp = tmp->nextptr;

void MiddleNodeDeletion(int pos)

int i;

struct node *toDelMid, *preNode;

if(stnode == NULL)

printf(" There are no nodes in the List.");

else

toDelMid = stnode;

preNode = stnode;

for(i=2; i<=pos; i++)


{

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

printf(" Deletion can not be possible from that position.");

void displayList()

struct node *tmp;

if(stnode == NULL)

printf(" No data found in the list.");

else
{

tmp = stnode;

while(tmp != NULL)

printf(" Data = %d\n", tmp->num); // prints the data of current node

tmp = tmp->nextptr; // advances the position of current node

Output:
Program 16:
Source Code:
#include <stdio.h>

#include <stdlib.h>

// A Linked List Node

struct Node

int data;

struct Node* next;

};

// Helper function to print a given linked list

void printList(struct Node* head)

struct Node* ptr = head;

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

void push(struct Node** head, int data)

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = *head;
*head = newNode;

// Return the total number of nodes in a list

int findLength(struct Node* head)

int count = 0;

struct Node* current = head;

while (current != NULL)

count++;

current=current->next;

return count;

/*

Split the given list's nodes into front and back halves

and return the two lists using the reference parameters.

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)

int len = findLength(source);

if (len < 2)

*frontRef = source;

*backRef = NULL;

return;

}
struct Node* current = source;

int hopCount = (len - 1) / 2; // (figured these with a few drawings)

for (int i = 0; i < hopCount; i++) {

current = current->next;

// Now cut at current

*frontRef = source;

*backRef = current->next;

current->next = NULL;

int main(void)

// input keys

int keys[] = {6, 3, 4, 8, 2, 9};

int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list

struct Node* head = NULL;

// construct a linked list

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

push(&head, keys[i]);

struct Node *a = NULL, *b = NULL;

frontBackSplit(head, &a, &b);

// print linked list


printf("Front List: ");

printList(a);

printf("\nBack List: ");

printList(b);

return 0;

Output:

Program 17:
Source Code:
#include <stdio.h>

#include <stdlib.h>

struct node {

int num;

struct node * nextptr;

}*stnode;

struct node *tail,*p,*q,*store;


void ClListcreation(int n);

void ClLinsertNodeAtEnd(int num);

void displayClList(int a);

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

printf(" Input the number of nodes : ");

scanf("%d", &n);

ClListcreation(n);

a=1;

displayClList(a);

return 0;

void ClListcreation(int n)

int i, num;

struct node *preptr, *newnode;

if(n >= 1)

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

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode->num = num;

stnode->nextptr = NULL;

preptr = stnode;

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


{

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

printf(" Input data for node %d : ", i);

scanf("%d", &num);

newnode->num = num;

newnode->nextptr = NULL; // next address of new node set as NULL

preptr->nextptr = newnode; // previous node is linking with new node

preptr = newnode; // previous node is advanced

preptr->nextptr = stnode; //last node is linking with first node

void displayClList(int m)

struct node *tmp;

int n = 1;

if(stnode == NULL)

printf(" No data found in the List yet.");

else

tmp = stnode;

if (m==1)

printf("\n Data entered in the list are :\n");

else

printf("\n After insertion the new list are :\n");


}

do {

printf(" Data %d = %d\n", n, tmp->num);

tmp = tmp->nextptr;

n++;

}while(tmp != stnode);

Output:

Program 18:
Source Code:
#include <stdio.h>

#include <stdlib.h>
struct node {

int num;

struct node * nextptr;

}*stnode,*ennode;

void ClListcreation(int n);

int FindElement(int FindElem, int n);

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

printf(" Input the number of nodes : ");

scanf("%d", &n);

m=n;

ClListcreation(n);

displayClList();

printf(" Input the element you want to find : ");

scanf("%d", &FindElem);

FindPlc=FindElement(FindElem,m);

if(FindPlc<n)

printf(" Element found at node %d \n\n",FindPlc);


else

printf(" This element does not exists in linked list.\n\n");

return 0;

void ClListcreation(int n)

int i, num;

struct node *preptr, *newnode;

if(n >= 1)

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

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode->num = num;

stnode->nextptr = NULL;

preptr = stnode;

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

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

printf(" Input data for node %d : ", i);

scanf("%d", &num);

newnode->num = num;

newnode->nextptr = NULL; // next address of new node set as NULL

preptr->nextptr = newnode; // previous node is linking with new node

preptr = newnode; // previous node is advanced

preptr->nextptr = stnode; //last node is linking with first node


}

int FindElement(int FindElem, int a)

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()

struct node *tmp;

int n = 1;

if(stnode == NULL)

printf(" No data found in the List yet.");

else

{
tmp = stnode;

printf("\n\n Data entered in the list are :\n");

do {

printf(" Data %d = %d\n", n, tmp->num);

tmp = tmp->nextptr;

n++;

}while(tmp != stnode);

Output:

Program 19:
Source Code:
#include <stdio.h>

#include <stdlib.h>

struct node {

int num;

struct node * nextptr;

}*stnode;

struct node *tail,*p,*q,*store;

void ClListcreation(int n);

void ClLinsertNodeAtBeginning(int num);

void ClLinsertNodeAtAny(int num, int pos);

void ClLinsertNodeAtEnd(int num);

void displayClList(int a);

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

printf(" Input the number of nodes : ");

scanf("%d", &n);

ClListcreation(n);

a=1;

displayClList(a);

int choice=0;

printf("1. beginning\n");

printf("2. any position\n");

printf("3. end\n");
printf("please enter the choice: ");

scanf("%i",&choice);

switch (choice)

case 3:

printf(" Input the data to be inserted : ");

scanf("%d", &num1);

ClLinsertNodeAtEnd(num1);

a=2;

displayClList(a);

break;

case 2:

printf(" Input the position to insert a new node : ");

scanf("%d", &insPlc);

printf(" Input data for the position %d : ", insPlc);

scanf("%d", &num1);

ClLinsertNodeAtAny(num1,insPlc);

a=2;

displayClList(a);

case 1:

printf(" Input data to be inserted at the beginning : ");

scanf("%d", &num1);

ClLinsertNodeAtBeginning(num1);

a=2;
displayClList(a);

break;

default:

break;

return 0;

void ClListcreation(int n)

int i, num;

struct node *preptr, *newnode;

if(n >= 1)

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

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode->num = num;

stnode->nextptr = NULL;

preptr = stnode;

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

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

printf(" Input data for node %d : ", i);

scanf("%d", &num);

newnode->num = num;

newnode->nextptr = NULL; // next address of new node set as NULL

preptr->nextptr = newnode; // previous node is linking with new node

preptr = newnode; // previous node is advanced


}

preptr->nextptr = stnode; //last node is linking with first node

void ClLinsertNodeAtBeginning(int num)

struct node *newnode, *curNode;

if(stnode == NULL)

printf(" No data found in the List yet.");

else

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

newnode->num = num;

newnode->nextptr = stnode;

curNode = stnode;

while(curNode->nextptr != stnode)

curNode = curNode->nextptr;

curNode->nextptr = newnode;

stnode = newnode;

void ClLinsertNodeAtAny(int num, int pos)

struct node *newnode, *curNode;

int i;
if(stnode == NULL)

printf(" No data found in the List yet.");

else if(pos == 1)

ClLinsertNodeAtBeginning(num);

else

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

newnode->num = num;

curNode = stnode;

for(i=2; i<=pos-1; i++)

curNode = curNode->nextptr;

newnode->nextptr = curNode->nextptr;

curNode->nextptr = newnode;

void ClLinsertNodeAtEnd(int num1)

int a;

a=num1;

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

temp->num=a;

p=stnode;

while(p->nextptr!=stnode)
{

p=p->nextptr;

p->nextptr=temp;

temp->nextptr=stnode;

void displayClList(int m)

struct node *tmp;

int n = 1;

if(stnode == NULL)

printf(" No data found in the List yet.");

else

tmp = stnode;

if (m==1)

printf("\n Data entered in the list are :\n");

else

printf("\n After insertion the new list are :\n");

do {

printf(" Data %d = %d\n", n, tmp->num);

tmp = tmp->nextptr;

n++;
}while(tmp != stnode);

Output:

Program 20:
Source Code:
#include <stdio.h>
#include <stdlib.h>

struct node

int num; //Data of the node

struct node *nextptr; //Address of the node

}*stnode;

void createNodeList(int n); //function to create the list

void reverseDispList(); //function to convert the list in reverse

void displayList(); //function to display the list

int main()

int n;

printf("\n\n Linked List : Create a singly linked list and print it in reverse order :\n");

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

printf(" Input the number of nodes : ");

scanf("%d", &n);

createNodeList(n);

printf("\n Data entered in the list are : \n");

displayList();

reverseDispList();

printf("\n The list in reverse are : \n");

displayList();

return 0;

void createNodeList(int n)

{
struct node *fnNode, *tmp;

int num, i;

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

if(stnode == NULL) //check whether the stnode is NULL and if so no memory allocation

printf(" Memory can not be allocated.");

else

// reads data for the node through keyboard

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode-> num = num;

stnode-> nextptr = NULL; //Links the address field to NULL

tmp = stnode;

//Creates n nodes and adds to linked list

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

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

if(fnNode == NULL) //check whether the fnnode is NULL and if so no memory allocation

printf(" Memory can not be allocated.");

break;

else

printf(" Input data for node %d : ", i);

scanf(" %d", &num);

fnNode->num = num; // links the num field of fnNode with num

fnNode->nextptr = NULL; // links the address field of fnNode with NULL

tmp->nextptr = fnNode; // links previous node i.e. tmp to the fnNode


tmp = tmp->nextptr;

void reverseDispList()

struct node *prevNode, *curNode;

if(stnode != NULL)

prevNode = stnode;

curNode = stnode->nextptr;

stnode = stnode->nextptr;

prevNode->nextptr = NULL; //convert the first node as last

while(stnode != NULL)

stnode = stnode->nextptr;

curNode->nextptr = prevNode;

prevNode = curNode;

curNode = stnode;

stnode = prevNode; //convert the last node as head

void displayList()
{

struct node *tmp;

if(stnode == NULL)

printf(" No data found in the list.");

else

tmp = stnode;

while(tmp != NULL)

printf(" Data = %d\n", tmp->num); // prints the data of current node

tmp = tmp->nextptr; // advances the position of current node

Output:

Program 21:
Source Code:
#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *prev;

struct node *next;

};

struct node *head = NULL;

struct node *last = NULL;

struct node *current = NULL;

//display the list

void printList() {

struct node *ptr = head;

printf("\n[head] <=>");

//start from the beginning

while(ptr != NULL) {

printf(" %d <=>",ptr->data);

ptr = ptr->next;

printf(" [last]\n");

//Create Linked List

void insert(int data) {


// Allocate memory for new node;

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

link->data = data;

link->prev = NULL;

link->next = NULL;

// If head is empty, create new list

if(head==NULL) {

head = link;

return;

current = head;

// move to the end of the list

while(current->next!=NULL)

current = current->next;

// Insert link at the end of the list

current->next = link;

last = link;

link->prev = current;

void search(int data) {

int pos = 0;

if(head==NULL) {

printf("Linked List not initialized");

return;
}

current = head;

while(current!=NULL) {

pos++;

if(current->data == data) {

printf("%d found at position %d\n", data, pos);

return;

if(current->next != NULL)

current = current->next;

else

break;

printf("%d does not exist in the list\n", data);

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;

struct node *next;

};

int main()

struct node *temp1,*temp2, *t,*newNode, *startList;

int n,k,i,j;

startList=NULL;

printf("Input number of elements in the linked list?");

scanf("%d",&n);

printf("Input the elements in the linked list:\n");

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

if(startList==NULL)
{

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

scanf("%d",&newNode->data);

newNode->next=NULL;

startList = newNode;

temp1=startList;

else

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

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++)

if(temp1->data > temp2->data)

k=temp1->data;

temp1->data=temp2->data;

temp2->data=k;

temp1=temp2;

temp2=temp2->next;

}
}

printf("Sorted order is: \n");

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

int Top=-1, inp_array[Size];

void Push();
void Pop();

void show();

int main()

int choice;

while(1)

printf("\nOperations performed by Stack");

printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");

printf("\n\nEnter the choice:");

scanf("%d",&choice);

switch(choice)

case 1: Push();

break;

case 2: Pop();

break;

case 3: show();

break;

case 4: exit(0);

default: printf("\nInvalid choice!!");

void Push()

int x;
if(Top==Size-1)

printf("\nOverflow!!");

else

printf("\nEnter element to be inserted to the stack:");

scanf("%d",&x);

Top=Top+1;

inp_array[Top]=x;

void Pop()

if(Top==-1)

printf("\nUnderflow!!");

else

printf("\nPopped element: %d",inp_array[Top]);

Top=Top-1;

void show()

if(Top==-1)

printf("\nUnderflow!!");
}

else

printf("\nElements present in the stack: \n");

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];

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;

return 0;

int main()

char exp[100];

char *e, x;

printf("Enter the expression : ");

scanf("%s",exp);

printf("\n");
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());

}return 0;

Output:
Program 25:
Source Code:
/*

* C Program to Implement a Queue using an Array

*/

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

} /* End of switch */

} /* End of while */

} /* End of main() */

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;

} /* End of insert() */

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;

} /* End of delete() */

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

} /* End of display() */

Output:
Program 26:
Source Code:
# include<stdio.h>

# define MAX 5

int cqueue_arr[MAX];

int front = -1;

int rear = -1;

/*Begin of insert*/

void insert(int item)

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

printf("Queue Overflow \n");


return;

if (front == -1) /*If queue is empty */

front = 0;

rear = 0;

else

if(rear == MAX-1) /*rear is at last position of queue */

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 ;

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

if(front == rear) /* queue has only one element */

front = -1;

rear=-1;

else
{

if(front == MAX-1)

front = 0;

else

front = front+1;

/*End of del() */

/*Begin of display*/

void display()

int front_pos = front,rear_pos = rear;

if(front == -1)

printf("Queue is empty\n");

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

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

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 :
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;

struct node *next;

};

struct node *front;


struct node *rear;

void insert();

void delete();

void display();

void main ()

int choice;

while(choice != 4)

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

printf("\n=================================================================\n");

printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");

printf("\nEnter your choice ?");

scanf("%d",& choice);

switch(choice)

case 1:

insert();

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:

printf("\nEnter valid choice??\n");

}
void insert()

struct node *ptr;

int item;

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

if(ptr == NULL)

printf("\nOVERFLOW\n");

return;

else

printf("\nEnter value?\n");

scanf("%d",&item);

ptr -> data = item;

if(front == NULL)

front = ptr;

rear = ptr;

front -> next = NULL;

rear -> next = NULL;

else

rear -> next = ptr;

rear = ptr;

rear->next = NULL;

void delete ()

{
struct node *ptr;

if(front == NULL)

printf("\nUNDERFLOW\n");

return;

else

ptr = front;

front = front -> next;

free(ptr);

void display()

struct node *ptr;

ptr = front;

if(front == NULL)

printf("\nEmpty queue\n");

else

{ printf("\nprinting values .....\n");

while(ptr != NULL)

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

ptr = ptr -> next;

Output:

You might also like