You are on page 1of 21

DATA STRUCTURES & ALGORITHMS

DA5

NAME: UTKARSH GOYAL

REGISTRATION NO: 19BIT0402

SLOT: L13+L14

FACULTY: PROF.MYTHILI N.

LAB ASSESSMENT-5

Question 1

Implement circular singly linked list by tracing the first and last node
Low Level: Implement any one of the technique [6 marks]
Middle Level: Implement the problem by using both the techniques
[2 marks]
High Level: Implement the above problem to delete every nth nod e till
the list is left with a single node.( n need to be collected as
input)

LOW LEVEL CODE-

#include<stdio.h>
#include<ctype.h>
#include<malloc.h>

struc t n ode
{
int data ;
s truc t n ode *nex t;
};
struc t n ode *last = NULL ;
void insertatend()
{
int x ;
s truc t n ode * temp;
temp = (s truc t node *)malloc(sizeof(struct node));
printf("e nter data : ");
scanf(" %d" ,&x);
temp- >data=x;
if(last==NULL)
{
last = temp;
temp- >next = last;
}
else
{
temp- >next = last->nex t;
last- >nex t = tem p;
last = temp;
}
}
void insertatbe g()
{
int x ;
s truc t n ode * temp;
temp = (s truc t node *)malloc(sizeof(struct node));
printf("e nter data : ");
scanf(" %d" ,&x);
temp- >data=x;
if(last==NULL)
{
last = temp;
temp- >next = last;
}
else
{
temp- >next = last->nex t;
last- >nex t = tem p;
}
}
void insertatmiddle()
{
int i,pos ;
s truc t n ode * temp,*q;
temp = (s truc t node *)malloc(sizeof(struct node));
printf("e nter data : ");
scanf(" %d" ,&temp- >data);
printf("e nter position : ");
scanf(" %d" ,&pos);
q = last->nex t;
f or(i=0;i<pos-1 ;i++)
{
q = q- >next;
if(q == last->nex t)
{
printf(" No Such Position" );
return;
}
}
temp- >next = q->nex t;
q- >nex t = tem p;
if(q == last)
{
last = temp;
}
}
void display()
{
s truc t n ode *q;
if(last == NULL)
{
printf ("List is empty \n");
}
else
{
q = last->next;
while(q != la st)
{
printf("%d\ n",q- >data);
q = q- >next;
}
printf ("%d\n",q- >data);
}
}
void delete()
{
int x;
struc t n ode *tem p,*q;
printf("En ter the da ta to dele te : ");
scanf("%d",&x);
if ((last->nex t == last) && (last - >data == x))
{
temp = last;
last = NULL ;
free(temp);
return;
}
q = last ->nex t;
if(q->data == x)
{
temp = q;
last->next = q->next;
free(temp);
return;
}
while(q->next != last)
{
if(q->next- >data == x)
{
temp = q- >next;
q->next = temp ->nex t;
free(temp);
return;
}
q = q->nex t;
}
if(q->next- >data == x)
{
temp = q- >next;
q->next = last->next;
free(temp);
last = q ;
return;
}
printf(" No Such Data Found");
}

int main()
{
int option;
do
{
printf ("1. Insert at beg \n");
printf ("2. Insert a t end \ n");
printf ("3. Inse rt at mid \n");
printf ("4. Delete by Data \n" );
printf ("5. Display\n");
printf ("6. exit\n");
scanf("%d" ,&option);
switc h(option)
{
case 1:
inse rtatbeg();
brea k;
case 2:
inse rtate nd();
brea k;
case 3:
inse rtatmiddle();
brea k;
case 4:
delete();
brea k;
case 5:
display();
brea k;
}
}while(option!=6);
retu rn 0;
}

MEDIUM LEVEL CODE-

#include<stdio.h>
#include<stdlib.h>
struc t n ode
{
int data ;
s truc t n ode *nex t;
};
struc t n ode *head;

void insertatbe g();


void insertatend();
void insertatmid();
void delete Beginning();
void deleteEnd();
void deleteSpecific();
void display() ;

int main()
{
int option;
do
{
printf ("1. Insert at beg \n");
printf ("2. Insert a t end \ n");
printf ("3. Inse rt at mid \n");
printf ("4. Delete at beg \n" );
printf ("5. Delete at end \n");
printf ("6. Delete by da ta \n");
printf ("7. Display\n");
printf ("8. exit\n" );
scanf("%d" ,&option);
switc h(option)
{
case 1:
inse rtatbeg();
brea k;
case 2:
inse rtate nd();
brea k;
case 3:
inse rtatmid();
brea k;
case 4:
deleteBeginning();
brea k;
case 5:
deleteEnd();
brea k;
case 6:
deleteSpecific();
brea k;
case 7:
display();
brea k;
}
}while(option!=8);
retu rn 0;
}

void insertatbe g()


{
s truc t n ode * ptr,*temp;
int item ;
ptr = (struct node *)malloc(sizeof (struc t node));
printf(" \nEnte r the node data?");
scanf(" %d" ,&item);
ptr->da ta = item;
if(head == NULL)
{
head = ptr;
ptr- >nex t = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp- >next;
ptr- >nex t = head;
temp -> next = ptr;
head = ptr;
}
printf(" \nnode inserted\n");
}

void insertatend()
{
s truc t n ode * ptr,*temp;
int item ;
ptr = (struct node *)malloc(sizeof (struc t node));
printf("\ nEnte r Data?");
scanf(" %d" ,&item);
ptr->da ta = item;
if(head == NULL)
{
head = ptr;
ptr->nex t = head;
}
else
{
temp = head;
while(temp ->nex t != head)
{
temp = temp- >next;
}
temp- >next = ptr;
ptr->nex t = head;
}
printf(" \nnode inserted\n");
}

void insertatmid()
{
int value, data;
printf("En ter the value : " );
scanf("%d",&value);
printf("En ter the da ta afte r which you want to inse rt : ");
scanf("%d",&da ta);
struc t n ode *ptr,*temp;
ptr = (struct node*)malloc(sizeof(struc t node));
ptr- >data = value;
if(head == NULL)
{
head = ptr;
ptr- >nex t = head;
}
else
{
temp = head;
while(temp->data != data)
{
if(temp->nex t == head)
{
printf(" Given node is not found in the l ist!!!");
return;
}
else
{
temp = temp- >next;
}
}
ptr- >nex t = tem p->next;
temp- >next = ptr;
printf("\ nInsertion su ccess!!!");
}
}

void delete Beginning()


{
struc t n ode *tem p;
if(head == NULL)
printf("List is Em pty!!! Deletion not possible!!!");
else
{
temp = head;
if(temp->nex t == head)
{
head = NULL;
free(temp);
}
else
{
head = head- >next;
free(temp);
}
printf("\ nDeletion suc cess!!!");
}
}

void deleteEnd()
{
if(head == NULL)
printf("List is Em pty!!! Deletion not possible!!!");
else
{
struc t n ode *tem p1 = head, *temp2 ;
if(temp1->nex t == head)
{
head = NULL;
free(temp1);
}
else
{
while(temp1->next != head)
{
temp2 = temp1;
temp1 = tem p1 ->nex t;
}
temp2- >next = head;
free(temp1);
}
printf("\ nDeletion suc cess!!!");
}
}

void deleteSpecific()
{
int delValue;
printf("En ter the da ta you want to dele te : ");
scanf("%d",&delValue);
if(head == NULL)
printf("List is Em pty!!! Deletion not possible!!!");
else
{
struc t n ode *tem p1 = head, *temp2 ;
while(temp1->data != delValue)
{
if(temp1->nex t == head)
{
printf("\ nGiven node is not found in the list!!!");
return;
}
else
{
temp2 = temp1;
temp1 = tem p1 ->nex t;
}
}
if(temp1->nex t == head)
{
head = NULL;
free(temp1);
}
else
{
if(temp1 == head)
{
temp2 = head;
while(temp2->next != head)
temp2 = temp2- >next;
head = head- >next;
temp2- >next = head;
free(temp1);
}
else
{
if(temp1->nex t == head)
{
temp2- >next = head;
}
else
{
temp2- >next = temp1- >next;
}
free(temp1);
}
}
printf("\ nDeletion suc cess!!!");
}
}

void display()
{
s truc t n ode * ptr;
ptr=head;
if(head == NULL)
{
printf ("\nnothing to print");
}
else
{
printf ("\n printing values ... \n");

while(ptr- >next != head)


{

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


ptr = ptr ->nex t;
}
printf ("%d\n", ptr- >data);
}

}
HIGH LEVEL CODE -

#include<stdio.h>
#include<stdlib.h>
struc t n ode
{
int data ;
s truc t n ode *nex t;
};

struc t n ode *head;


void insertatbe g();
void insertatend();
void insertatmid();
void delete() ;
void display() ;

int main()
{
int option;
do
{
printf ("1. Insert at beg \n");
printf ("2. Insert a t end \ n");
printf ("3. Inse rt at mid \n");
printf ("4. Delete eve ry elem ent at kth position \n");
printf ("5. Display\n");
printf ("6. exit\n");
scanf("%d" ,&option);
switc h(option)
{
case 1:
inse rtatbeg();
brea k;
case 2:
inse rtate nd();
brea k;
case 3:
inse rtatmid();
brea k;
case 4:
delete();
brea k;
case 5:
display();
brea k;
}
}while(option!=6);
retu rn 0;
}

void insertatbe g()


{
s truc t n ode * ptr,*temp;
int item ;
ptr = (struct node *)malloc(sizeof (struc t node));
printf(" \nEnte r the node data?");
scanf(" %d" ,&item);
ptr->da ta = item;
if(head == NULL)
{
head = ptr;
ptr- >nex t = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp- >next;
ptr- >nex t = head;
temp -> next = ptr;
head = ptr;
}
printf(" \nnode inserted\n");
}

void insertatend()
{
s truc t n ode * ptr,*temp;
int item ;
ptr = (struct node *)malloc(sizeof (struc t node));
printf("\ nEnte r Data?");
scanf(" %d" ,&item);
ptr->da ta = item;
if(head == NULL)
{
head = ptr;
ptr->nex t = head;
}
else
{
temp = head;
while(temp ->nex t != head)
{
temp = temp- >next;
}
temp- >next = ptr;
ptr->nex t = head;
}
printf(" \nnode inserted\n");
}

void insertatmid()
{
int value, data;
printf("En ter the value : " );
scanf("%d",&value);
printf("En ter the da ta afte r which you want to inse rt : ");
scanf("%d",&da ta);
struc t n ode *ptr,*temp;
ptr = (struct node*)malloc(sizeof(struc t node));
ptr- >data = value;
if(head == NULL)
{
head = ptr;
ptr- >nex t = head;
}
else
{
temp = head;
while(temp->data != data)
{
if(temp->nex t == head)
{
printf(" Given node is not found in the l ist!!!");
return;
}
else
{
temp = temp- >next;
}
}
ptr- >nex t = tem p->next;
temp- >next = ptr;
printf("\ nInsertion su ccess!!!");
}
}

void display()
{
s truc t n ode * ptr;
ptr=head;
if(head == NULL)
{
printf ("\nnothing to print");
}
else
{
printf ("\n printing values ... \n");

while(ptr- >next != head)


{

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


ptr = ptr ->nex t;
}
printf ("%d\n", ptr- >data);
}
}

void delete()
{
int k,i;
printf("En ter the position : ");
scanf("%d",& k);

if(head == NULL)
{
printf("List is Em pty!!! Deletion not possible!!!");
return;
}
while(1)
{
struc t n ode *cu rr=head,*prev;
if (curr ->nex t == head && curr == head)
{
break;
}
for (i = 0; i <k-1 ; i++)
{
prev = curr;
cu rr = curr- >next;
}
if (curr == head)
{
prev = head;
while (prev ->nex t != head)
pre v = prev- >next;
head = curr->next;
prev ->nex t = head;
f ree(cu rr);
}
else if (cu rr- >next == head)
{
prev ->nex t = head;
f ree(cu rr);
}
else
{
prev ->nex t = cu rr- >nex t;
f ree(cu rr);
}

dis play();
}
}

Question 2
Assume in the Regional Passport Office, a multitude of applicants arrive
each day for passport renewal. A list is maintained in the database to
store the renewed passports arranged in the increased order of passport
ID. The list already would contain there cords renewed till the previous
day. Apply Insertion sort technique to place the current day’s records in
the list.

Later the office personnel wish to sort the records based on the date of
renewal so as to know the count of renewals done each day. Taking into
consideration the fact that each record has sever al fields (around 25
fields), follow Selection sort logic to implement the same.

Low Level: Implement any one of the sorting technique [6


marks]
Middle Level: Implement the problem for both the sorting techniques
[2 marks]
High Level: Also your program should check the stability of both
the algorithms [2
marks]

CODE-

#include<iostream>
#include<string>

using namespace std;

struct pass
{
int id;
string date;
};

int main()
{
int i,j,min,n;
pass t;
pass a[100],b[100];
cout<<"Enter number of records: ";
cin>>n;
cout<<"Enter records: ";
for(i=0;i<n;i++)
{
cout<<"\nid: ";
cin>>a[i].id;
cout<<"date: ";
cin>>a[i].date;
}

for(i=0;i<n;i++)
{
b[i]=a[i];
}

for (i=1;i<n;i++)
{
t=a[i];
j=i-1;
while(a[j].id > t.id && j>=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=t;

for (i=0;i<n;i++)
{
cout<<"\nid: ";
cout<<a[i].id;
cout<<"\ndate: ";
cout<<a[i].date;
}

for (i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(b[j].date<b[min].date)
{
min=j;
}
}
t=b[i];
b[i]=b[min];
b[min]=t;
}

for (i=0;i<n;i++)
{
cout<<"\ndate: ";
cout<<b[i].date;
cout<<"\nid: ";
cout<<b[i].id;

Question 3

In a shopping mall, for each of the products sold, the following information is
stored: a unique code, a name, a price, amount in stock, date received, expiry date.
For keeping track of its stock, the clerk would use a computer program based on a
search tree data structure. Write a program to help this person, by implementing
the following operations:

• Insert an item with all its associated data.


• Find an item by its code, and support updating of the item found.
• List all items in the increasing order of code.
• Implement other traversals to list all the items in the shop
• Delete an item given by its code.
• Exit

Low Level: Implement the above problem excluding deletion module [6


marks]

Middle Level: Implement the above problem to perform all operations [2


marks]

High Level: Also print the details of those products which have run out of
stock first followed by those products which are still in stock.
Increasing order of item code should be maintained
[2 marks]

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

struc t n ode
{
int code ,price,stoc k;
char name[20],date[10],expda te[10];

struc t n ode *leftChild;


struc t n ode * rightChild;
};

struc t n ode * root = NULL;

void insert()
{
struc t n ode *tem pNode = (struct node*) malloc(sizeof(struct node));
struc t n ode *cu rrent;
struc t n ode *pa rent;

printf("En ter the unique code : ");


scanf("%d",& tempNode ->c ode);
printf("En ter the name : ");
scanf("%s",tempNode - >name);
printf("En ter the price : ");
scanf("%d",& tempNode ->price);
printf("En ter the stoc k : ");
scanf("%d",& tempNode ->stoc k);
printf("En ter the da te : ");
scanf("%s",tempNode - >date);
printf("En ter the expi ry date : ");
scanf("%s",tempNode - >expdate);

tempNode ->leftChild = NULL;


tempNode ->rightChild = NULL;

if(root == NULL)
{
root = tempNode;
}
else
{
curren t = root;
parent = NULL;

while(1)
{
parent = curre nt;

if(tempNode- >c ode < parent- >c ode)


{
current = current ->leftChild;
if(current == NULL)
{
parent- >leftChild = tem pNode ;
return;
}
}
else
{
current = current ->rightChild;

if(current == NULL)
{
parent- >rightChild = tem pNode ;
return;
}
}
}
}
printf("Inse rti on sucessfull \n");
}

void inorder_ trave rsal(struc t node* root)


{
if(root != NULL)
{
inorder_ trave rsal(root - >leftChild);
printf("unique code : %d \n",root- >code);
printf("name : %s \n",root- >name);
printf(" price : %d \n",root- >price);
printf("stoc k : %d \n",root- >stock);
printf("da te : %s \n" ,root->da te);
printf("ex piry date : %s \n",root- >expda te);
printf("************************** \n");
inorder_ trave rsal(root - >rightChild);
}
}

void pre_order_ trave rsal(struc t node* root)


{
if(root != NULL)
{
printf("unique code : %d \n",root- >code);
printf("name : %s \n",root- >name);
printf(" price : %d \n",root- >price);
printf("stoc k : %d \n",root- >stock);
printf("da te : %s \n" ,root->da te);
printf("ex piry date : %s \n",root- >expda te);
printf("************************** \n");
pre_order_ trave rsal(root - >leftChild);
pre_order_ trave rsal(root - >rightChild);
}
}
void post_ orde r_traversal(struct node * root)
{
if(root != NULL)
{
post_ orde r_traversal(root ->leftChild);
post_ orde r_trave rsal(root->rightChild);
printf("unique code : %d \n",root- >code);
printf("name : %s \n",root- >name);
printf(" price : %d \n",root- >price);
printf("stoc k : %d \n",root- >stock);
printf("da te : %s \n" ,root->da te);
printf("ex piry date : %s \n",root- >expda te);
printf("************************** \n");
}
}

struc t n ode* search(int data)


{
struc t n ode *cu rrent = root;
printf(" Visiting elements: ");
while(current- >code != da ta)
{
if(current != NULL)
printf(" %d ",cu rrent->code);
if(current ->c ode > data)
{
current = current ->leftChild;
}
else
{
current = current ->rightChild;
}

if(current == NULL)
{
return NULL;
}
}
retu rn curre nt;
}

void displays toc k(struct node* root)


{
if(root != NULL)
{
displaystoc k(root->leftChild);
if(root->stoc k != 0)
{
printf("unique code : %d \n",root- >code);
printf("name : %s \n",root- >name);
printf(" price : %d\n",root- >price);
printf("stoc k : %d\n",root- >stock);
printf("da te : %s\n" ,root->da te);
printf("ex piry date : %s \n",root- >expda te);
printf("************************** \n");
}
displaystoc k(root->rightChild);
}
}

void displayou tstock(struc t node* root)


{
if(root != NULL)
{
displayoutstock(root ->leftChild);
if(root->stoc k == 0)
{
printf("unique code : %d \n",root- >code);
printf("name : %s \n",root- >name);
printf(" price : %d\n",root- >price);
printf("stoc k : %d\n",root- >stock);
printf("da te : %s\n" ,root->da te);
printf("ex piry date : %s \n",root- >expda te);
printf("************************** \n");
}
displayoutstock(root ->rightChild);
}
}

int main()
{
int option,data ,option2 ;
do
{
printf ("1. Insert\n");
printf ("2. display\n");
printf ("3. Pre order traversal \n");
printf ("4. Post Orde r trave rsal \n");
printf ("5. Search \n");
printf ("6. Display stoc k and out of stoc k\n");
printf ("7. exit\n" );
scanf("%d" ,&option);
switc h(option)
{
case 1:
inse rt();
brea k;
case 2:
inorde r_traversal(root);
brea k;
case 3:
pre_ orde r_traversal(root);
brea k;
case 4:
post_order_ traversal(root);
brea k;
case 5:
printf("Ente r the unique code of the item to searc h : ");
scanf("%d" ,&data);
stru ct node * tem p = search(data);
if(temp != NULL)
{
printf("[ %d] Element fou nd." , temp - >code);
printf("\ n");
}
else
{
printf("[ %d ] Element not found . \n", data);
}
if(temp != NULL)
{
printf(" Press 1 to update the fields 2 to exit : ");
scanf("%d",& opti on2);
if(option2 == 1)
{
printf("Enter the name : ");
scanf("%s",temp- >name);
printf("Enter the price : ");
scanf("%d",& temp- >price);
printf("Enter the s toc k : ");
scanf("%d",& temp- >stock);
printf("Enter the da te : ");
scanf("%s",temp- >date);
printf("Enter the expi ry date : ");
scanf("%s",temp- >expda te);
}
}
break;
case 6:
printf("I tems in stock are : \n");
displaystoc k(root);
printf("I tems out of stock are : \n");
displayoutstock(root);
break;
}
}while(option!=7);
retu rn 0;
}

You might also like