0% found this document useful (0 votes)
45 views29 pages

Lab Exam Pactice

The document discusses implementations of linked lists, stacks, and queues using both arrays and linked lists. It includes functions for insertion, deletion, traversal and other standard operations on these common data structures.

Uploaded by

sarkarsolanki045
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)
45 views29 pages

Lab Exam Pactice

The document discusses implementations of linked lists, stacks, and queues using both arrays and linked lists. It includes functions for insertion, deletion, traversal and other standard operations on these common data structures.

Uploaded by

sarkarsolanki045
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/ 29

Linked list (insert,delete, reverse)

#include<stdio.h>

#include<stdlib.h>

struct node{

int info;

struct node* next;

};

struct node* create(struct node* head, int n)

struct node *ptr, *temp;

int i, item;

if(head != NULL)

printf("No list has been created");

return (head);

temp = head;

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

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

printf("Enter data: ");

scanf("%d", &item);

ptr->info = item;

ptr->next = NULL;

if(head == NULL)

head = ptr;
}

else

temp->next = ptr;

temp = ptr;

return(head);

void traverse(struct node* head)

struct node* loc;

loc = head;

while(loc != NULL)

printf("%d->",loc->info);

loc = loc->next;

struct node *insert(struct node *head, int pos, int data)

struct node *loc, *ptr;

int i;

if(head == NULL)

printf("No list has been formed");

return(head);

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

ptr->info = data;

ptr->next = NULL;

if(pos == 0)

ptr->next = head;

head = ptr;

return(head);

else

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

loc=loc->next;

ptr->next = loc->next;

loc->next = ptr;

return(head);

struct node* delete(struct node* head, int pos)

struct node *loc, *locn;

int i;

if(head == NULL)

printf("No list has been formed");

return(head);

loc = head;
if(pos == 1)

head = loc->next;

free(loc);

return(head);

else

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

loc = loc->next;

locn = loc->next;

loc->next = locn->next;

free(locn);

return(head);

struct node* rev(struct node* head)

struct node *loc, *locp, *locn;

if(head == NULL)

printf("No list has been made");

return(head);

loc = head;

locp = NULL;

while(loc!= NULL)

{
locn = loc->next;

loc->next = locp;

locp = loc;

loc = locn;

head = locp;

return(head);

int main()

int d, ch, p, n;

struct node *head = NULL;

while(1)

printf("\n0.Exit\n1.Input\n2.Display\n3.Insert\n4.Delete");

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

scanf("%d", &ch);

switch(ch)

case 0:exit(0);

case 1:printf("Enter the range of list: ");

scanf("%d", &d);

head = create(head, d);

break;

case 2:printf("The obtained list is: \n");

traverse(head);

break;

case 3:printf("Enter the position and data to be inserted: \n");

scanf("%d %d", &p, &n);


head = insert(head,p,n);

break;

case 4:printf("Enter the position to be deleted: \n");

scanf("%d", &p);

head = delete(head, p);

break;

case 5:head = rev(head);

break;

default:printf("Invalid input");

Stack(using array)
#include<stdio.h>

#include<stdlib.h>

#define MAX 6

int stack[MAX];

int top = -1;

int isFull()

if(top == MAX-1)

return 1;

return 0;

int isEmpty()

{
if(top == -1)

return 1;

return 0;

void push(int d)

if (isFull())

printf("Overflow...");

else

top++;

stack[top] = d;

void pop()

int x;

if (isEmpty())

printf("Underflow...");

else

x = stack[top];

printf("Successfully removed %d", x);

top--;

}
}

void traverse()

int i;

for(i = top;i>=0; i--)

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

int main()

int ch, n;

while(1)

printf("\n0.Exit\n1.Push\n2.Pop\n3.Display");

printf("\n\nEnter your choice");

scanf("%d", &ch);

switch(ch)

case 0:exit(0);

case 1:printf("Enter data to push:\n");

scanf("%d", &n);

push(n);

break;

case 2:pop();

break;

case 3:traverse();

break;

default:printf("Invalid choice");

}
}

Stack(using linked list)

#include<stdio.h>

#include<stdlib.h>

struct node

int info;

struct node *next;

};

struct node *top = NULL;

void Push(int item)

struct node * ptr;

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

if(top == NULL)

ptr->info = item;

ptr->next = NULL;

top = ptr;

else

ptr -> info = item;

ptr -> next = top;


top = ptr;

int Pop()

if(top == NULL)

printf("Underflow");

return -1;

else

struct node * temp = top;

int temp_data = top -> info;

top = top->next;

free(temp);

return temp_data;

void traverse()

struct node *loc;

printf("The stack is \n");

loc = top;

while(loc!=NULL)

printf("%d->", loc->info);

loc = loc->next;
}

int main()

int ch, n;

while(1)

printf("\n0.Exit\n1.Push\n2.Pop\n3.Display");

printf("\n\nEnter your choice\n");

scanf("%d", &ch);

switch(ch)

case 0:exit(0);

case 1:printf("Enter data :\n");

scanf("%d", &n);

Push(n);

break;

case 2:printf("Popped element is %d", Pop());

break;

case 3:traverse();

break;

default:printf("Invalid choice");

Queue(using array)
#include<stdio.h>

#include<stdlib.h>
# define MAX 5

int arr[MAX];

int front = -1, rear = -1;

int isFull()

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

return 1;

return 0;

int isEmpty()

if(front == -1 && rear == -1)

return 1;

return 0;

void Enqueue(int d)

if(isFull())

printf("Queue is full");

return;

if(rear == -1)

front = 0;

rear = 0;

else if(rear == MAX - 1)


{

rear = 0;

else

rear = rear + 1;

arr[rear] = d;

return;

void Dequeue()

int x;

if(isEmpty())

printf("Queue is empty");

return;

x = arr[front];

printf("Successfully removed %d", x);

if(front == rear)

front = -1;

rear = -1;

else if(front == MAX - 1)

front = 0;

else
{

front = front + 1;

return;

void traverse()

int i;

if (front == -1)

printf("Queue empty");

return;

if(front <= rear)

for(i=front;i<=rear;i++)

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

else

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

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

for(i=front;i<=MAX-1;i++)

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

return;

int main()

{
int ch, n;

while(1)

printf("\n0.Exit\n1.Enqueue\n2.Dequeue\n3.Display");

printf("\n\nEnter your choice\n");

scanf("%d", &ch);

switch(ch)

case 0:exit(0);

case 1:printf("Enter data :\n");

scanf("%d", &n);

Enqueue(n);

break;

case 2:Dequeue();

break;

case 3:traverse();

break;

default:printf("Invalid choice");

Queue(using linked list)


#include<stdio.h>

#include<stdlib.h>

struct node

int info;

struct node *next;


};

struct node *front = NULL;

struct node *rear = NULL;

void Enqueue(int item)

struct node * ptr;

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

ptr -> info = item;

ptr -> next = NULL;

if((front == NULL) && (rear == NULL))

front = rear = ptr;

else

rear -> next = ptr;

rear = ptr;

int Dequeue()

if(front == NULL)

printf("Underflow");

return -1;

else

{
struct node * temp = front;

int temp_data = front -> info;

front = front->next;

free(temp);

return temp_data;

void traverse()

struct node *loc;

if ((front == NULL)&&(rear == NULL))

printf("\nQueue Empty");

else

printf("The queue is \n");

loc = front;

while(loc)

printf("%d->", loc->info);

loc = loc->next;

int main()

int ch, n;

while(1)
{

printf("\n0.Exit\n1.Enqueue\n2.Dequeue\n3.Display");

printf("\n\nEnter your choice\n");

scanf("%d", &ch);

switch(ch)

case 0:exit(0);

case 1:printf("Enter data :\n");

scanf("%d", &n);

Enqueue(n);

break;

case 2:printf("Popped element is %d", Dequeue());

break;

case 3:traverse();

break;

default:printf("Invalid choice");

Array operations(insert, delete, reverse,sort)

#include<stdio.h>

#include<stdlib.h>

# define MAX 20

int arr[MAX], brr[MAX];

int n;

void input()

int i;
printf("Enter the size of array:\n");

scanf("%d", &n);

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

printf("Enter data: ");

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

return;

void display()

int i;

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

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

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

return;

void insert(int pos, int data)

int i;

n++;

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

arr[i]=arr[i-1];

arr[pos-1] = data;

printf("Successfully inserted %d", arr[pos-1]);


return;

void delete(int pos)

int i;

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

arr[i]=arr[i+1];

n--;

return;

void rev()

int i,j;

for(i=n-1,j = 0;i>=0; i--,j++)

brr[j]=arr[i];

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

arr[i]=brr[i];

return;

void sort()

int i,j,temp;
for(i=0;i<n;i++)

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

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

temp = arr[i];

arr[i]= arr[j];

arr[j] = temp;

return;

int main()

int ch, p,d;

while(1)

printf("\n0.Exit\n1.Input\n2.Insert\n3.Delete\n4.Reverse\n5.Sort\n6.Display");

printf("\n\nEnter your choice\n");

scanf("%d", &ch);

switch(ch)

case 0:exit(0);

case 1:input();

break;

case 2:printf("Enter the position and data to be inserted:\n");

scanf("%d%d",&p, &d);

insert(p,d);
break;

case 3:printf("Enter the position to be deleted:\n");

scanf("%d", &p);

delete(p);

break;

case 4:rev();

break;

case 5:sort();

break;

case 6:display();

break;

default:printf("Invalid choice");

Merging two arrays and sorting them


#include<stdio.h>

void sort(int arr[20], int n) //to sort the resultant array

int i, j, swap;

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

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

if (arr[j] > arr[j+1]) /* For decreasing order use < */

swap = arr[j];
arr[j] = arr[j+1];

arr[j+1] = swap;

void merge(int arr1[20], int arr2[20], int n1, int n2) //merging arrays

int i, j, k;

int res[40];

i = 0;

j = 0;

k = 0;

// Merging starts

while (i < n1 && j < n2)

if (arr1[i] <= arr2[j])

res[k] = arr1[i];

i++;

k++;

else

res[k] = arr2[j];

k++;

j++;

/* Some elements in array 'arr1' are still remaining where as array 'arr2' is exhausted */
while (i < n1)

res[k] = arr1[i];

i++;

k++;

/* Some elements in array 'arr2' are still remaining where as array 'arr1' is exhausted */

while (j < n2)

res[k] = arr2[j];

k++;

j++;

sort(res, (n1+n2));

//Displaying elements of array 'res'

printf("\nMerged array is :");

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

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

int main()

int arr1[20], arr2[20];

int i, n1, n2;

printf("Enter no of elements in 1st array :\n");

scanf("%d", &n1);

printf("Enter elements of 1st array : \n");

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

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

printf("\nEnter no of elements in 2nd array :\n");

scanf("%d", &n2);

printf("Enter elements of 2nd array : \n");

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

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

merge(arr1, arr2, n1, n2);

return 0;

BST(with 3 traversal, search)

#include<stdio.h>

#include<stdlib.h>

struct BST {

struct BST *left;

int info;

struct BST *right;

};

struct BST *loc, *par, *par2;

struct BST* addnode(struct BST *root, int x )

struct BST *ptr;

loc = root;
par = NULL;

while(loc != NULL)

if(x == loc->info)

printf("Duplicate Entry");

return root;

par = loc;

if (x < loc->info)

loc = loc -> left;

else

loc = loc->right;

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

ptr -> info = x;

ptr -> left = NULL;

ptr -> right = NULL;

if(par == NULL)

root = ptr;

else if(x < par->info)

par->left = ptr;

else{
par->right = ptr;

return root;

void Inorder(struct BST *root)

if(root != NULL)

Inorder(root->left);

printf(" %d ", root->info);

Inorder(root->right);

void Preorder(struct BST *root)

if(root != NULL)

printf(" %d ", root->info);

Preorder(root->left);

Preorder(root->right);

void Postorder(struct BST *root)

if(root != NULL)

Postorder(root->left);

Postorder(root->right);
printf(" %d ", root->info);

struct BST *Search (struct BST *root, int p)

loc = root;

while(loc != NULL)

if(loc->info == p)

printf("%d found", p);

return root;

if(loc->info < p)

return Search(loc->right, p);

else

return Search(loc->left, p);

printf("%d not found", p);

return root;

int main()

{
struct BST* root = NULL;

int ch, d, q;

while(1)

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

printf("\n0.Exit\n1.Insert\n2.Inorder\n3.Preorder\n4.Postorder\n5.Search\n6.Delete\n");

printf("\nEnter you choice please\n");

scanf("%d", &ch);

switch(ch)

case 0: exit(0);

case 1: printf("\nEnter data to BST: ");

scanf("%d", &d);

root = addnode(root, d);

break;

case 2: Inorder(root);

break;

case 3: Preorder(root);

break;

case 4: Postorder(root);

break;

case 5: printf("\nEnter data you want to locate\n");

scanf("%d", &q);

root = Search(root, q);

break;

default:printf("Invalid choice");

break;

You might also like