You are on page 1of 29

DATA STRUCTURES AND

ALGORITHMS

CYCLE SHEET→1
SLOT→B1(ITA5002)

SUBMITTED BY:
NAME : ARUN.G
REG.NO :20MCA0043
1. Use a circular singly linked list to implement Round Robin process scheduling algorithm in
which each process is provided a fixed time (quantum) to execute and is pre-empted after that
time period to allow the other process to execute. Assume a set of ‘n’ processes are ready for
execution. Read the time quantum and for each of the processes, read the total execution.
Name the processes as ‘A’, ‘B’ and so on in sequence. Each node should contain the name of
the process, its total execution time and the remaining execution time. If a process completes
its execution, remove it from the list after displaying its name and the completion time.

Input format:
First line contains the value of ‘n’, the number of processes
Second line contains the time quantum
The remaining lines contain the total execution time of the processes in order.
5
2
6
3
7
5
1

Output:
E9
B 12
A 18
D 21
C 22
PROGRAM:

#include <iostream>

using namespace std;

class node

public:

char name;

int rt;

int tm;

node* next;

};

class rr

public:

node* head=NULL;

int j=65;

void insert(int n)

node* nn=new node;

nn->name=j++;

nn->tm=n;

nn->rt=nn->tm;
if(head==NULL)

head=nn;

head->next=head;

else

node* temp=head;

while(temp->next!=head)

temp=temp->next;

nn->next=temp->next;

temp->next=nn;

void quantum(int t,int y)

node* temp=head;

int c=0;

while(head!=NULL)

temp->rt=temp->rt-t;

c=c+t;

if(temp->rt<=0)
{

c=c+temp->rt;

cout<<temp->name;

cout<<c<<endl;

del(temp->name);

if(temp->next==temp)

break;

temp=temp->next;

void del(char x)

node* p=NULL;

node* temp=head;

if(head->name==x)

while(temp->next!=head)

temp=temp->next;

p=head;

temp->next=head->next;

head=head->next;

delete p;

else
{

while(temp->name!=x)

p=temp;

temp=temp->next;

p->next=temp->next;

delete temp;

};

int main()

rr robin;

int i,n,y,t;

cin>>y;

cin>>t;

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

cin>>n;

robin.insert(n);

robin.quantum(t,y);

return 0;

}
OUTPUT:
2. Create a singly linked list to store the ‘n’ user given words in the increasing order of
their word length. Traverse the list to print the words. Later reverse the list to store the
words in the decreasing order of their word length and display the list.
Input format:
First line contains the value of ‘n’, the number of words
The remaining ‘n’ lines contain the ‘n’ words
Sample Input and Output:
Input:
students of data structures and algorithms
Output:
algorithms structures students data and of
of and data students structures algorithms

PROGRAM:

#include <iostream>
#include <string.h>
using namespace std;

struct node
{
string word;
int wordLength;
bool flag;
node* next;
};

node* createNode()
{
struct node *temp = new node;
cin >> temp->word;
temp->wordLength = temp->word.length();
temp->next = nullptr;
return temp;
}

int main()
{
int n;
struct node* head = nullptr;
struct node* temp = nullptr;
struct node* headTemp = nullptr;
cout << "Enter the number of words" << endl;
cin >> n;
cout << "Enter the word " <<endl;
for(int i = 0; i < n; i++)
{
if(i == 0)
{
head = createNode();
headTemp = head;
}
else
{
temp = createNode();
headTemp->next = temp;
headTemp = headTemp->next;
}
}
headTemp->next = head;

temp = head;
struct node *preNode = new node;
int counter;
string arr[n];
for(counter = 0; counter < n; counter++)
{
int tempInt = 0;
preNode = nullptr;
do
{
if(temp->wordLength >= tempInt)
{
if(preNode != nullptr)
{
preNode->flag = false;
}
tempInt = temp->wordLength;
temp->flag = true;
preNode = temp;
temp = temp->next;
}
else
{
temp = temp->next;
}
}
while(temp != head);
do
{
if((tempInt == temp->wordLength) && (temp->flag == true))
{
cout << temp->word << " ";
arr[counter] = temp->word;
temp->wordLength = 0;
}
temp = temp->next;
}
while(temp != head);
}
cout << endl;

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


{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}

OUTPUT:
3. Given an arithmetic expression, use the appropriate ADT to evaluate it and print the
result.

Input Format:
First line contains the infix expression
Output
Result of the expression

Sample Input and Output:


5-8*(2+1)%6
Output:
5

PROGRAM:

#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
using namespace std;
const int size =100;
char infix[size],postfix[size],stack[size];
int top=-1;

int precedence(char ch);


char pop();
char topelement();
void push(char ch);
int eval(char *postf);
int precedence(char ch)
{
switch(ch)
{

case '%' : return 4;


case '/' : return 4;
case '*' : return 4;
case '+' : return 3;
case '-' : return 3;
default : return 0;
}
}

char pop()
{
char ret;
if(top!=-1)
{
ret =stack[top];
top--;
return ret;
}
else
return '#';
}

char topelement()
{
char ch;
if(top!=-1)
ch=stack[top];
else
ch='#';
return ch;
}

void push(char ch)


{
if(top!=size-1)
{
top++;
stack[top]= ch;
}
}

int eval(char *postf)


{
char *p;
int op1,op2,result;

p=&postf[0];
while(*p!='\0')
{
if(isdigit(*p))
{
push(*p-48);}
else
{
op1=pop();
op2=pop();
switch(*p)
{
case '+':
result = op2 + op1;
break;

case '-':
result = op2 - op1;
break;

case '/':
result = op2 / op1;
break;

case '*':
result = op2 * op1;
break;

case '%':
result = op2 % op1;
break;

default:
return 0;
}
push(result);
}
p++;
}
result=pop();
return result;
}
int main()
{
char ele,elem;
int prep,pre,popped,j=0;
strcpy(postfix," ");
cin>>infix;

for(int i=0;infix[i]!=0;i++)
{
while(infix[i] == ' ' || infix[i] == '\t'){
i++;
}

if(infix[i]!='('&&infix[i]!=')'&&infix[i]!='*'&&infix[i]!='%'&&infix[i]!='/'&&infix[i]!='+'&
&infix[i]!='-')
postfix[j++]=infix[i];
else if(infix[i]=='(')
{
elem=infix[i];
push(elem);
}
else if(infix[i]==')')
{
while((popped=pop()) != '(')
postfix[j++]=popped;
}
else
{
elem=infix[i];
pre=precedence(elem);
ele=topelement();
prep=precedence(ele);
if(pre > prep)
push(elem);
else
{
while(prep >= pre)
{
if(ele=='#')
break;
popped=pop();
ele=topelement();
postfix[j++]=popped;
prep=precedence(ele);
}
push(elem);
}
}
}
while((popped=pop())!='#')
postfix[j++]=popped;
postfix[j]='\0';
cout<<eval(&postfix[0])<<endl;
system("pause");
return 0;
}

OUTPUT:
4. The daily temperature recordings of a city are stored in a dynamic stack for a period
of one month. You are consulted to find the minimum temperature recorded in the
entire month, in constant time. Write a program to accomplish this.
Hint: Use another stack (dynamic stack). Push the new temperature into the first stack and
push either the new temperature or the current minimum, whichever is minimum, into the
second stack. At the end, return the top element from the second stack as the minimum
temperature.
Input format:
First line contains the number of days for which temperature recordings have to be stored.
The next ‘n’ lines contain the actual temperature readings.
Sample Input:
6 //7
36,33,38,32,37,34,35 //36 33 38 32 37 34 35

Output:
32

Stack 1 Stack2
35 32
34 32
37 32
32 32
38 33
33 33
36 36

PROGRAM:
#include<iostream>
using namespace std;
int stackMin();

int main()
{
stackMin();
return 0;
}
int stackMin()
{
int stack1[100],stack2[100],length,i,top;
cin>>length;
for(i=0;i<length;i++)
{
cin>>stack1[i];
}
for(i=0;i<length;i++)
{
if(top == -1)
{
stack2[++top] = stack1[i];
}
else if(stack2[top] > stack1[i])
{
stack2[++top] = stack1[i];
}
}
cout<<stack2[top];
}

OUTPUT:
5. For the course ‘Problem Solving with Data Structures and Algorithms’, read the
CAT-I marks of all the ‘n’ students in a class and store them in a two-way list. As the
exam is conducted for 30 marks, only those students with a score of 15 or more are
considered to have passed. The course instructor wishes to modify the same list so as
to retain the marks lesser than 15 in the beginning of the list and the marks greater
than or equal to 15 at the end of the list.
For this, start the traversal from both the ends. In the left side, if a score less than 15 is found,
retain it and keep moving to the subsequent nodes till you find a node with score >=15. In
the right side, if a node with a score >=15 is found, retain it and keep moving towards the
front until a node with score <15 is found. Now exchange these two nodes. Repeat this till the
left-side and the right-side traversal pointers cross each other. At last display the contents of
the list.
28⇆10 ⇆ 17 ⇆ 12 ⇆21 ⇆26 ⇆14 ⇆7 ⇆19⇆23⇆ 8 ⇆13
L R
Exchange 28 and 13, as 28 >15 and 13<15.
Next, move L to 10 and as 10 <15, move L to 17. Move R to 8. So, exchange 17 and 8.
……….
Input:
12
28 10 17 12 21 26 14 7 19 23 8 13
Output:
13 10 8 12 7 14 26 21 19 23 17 28

PROGRAM:

#include <iostream>
using namespace std;

class node
{
public:
int data;
node *prev, *next;
};
class marks
{
public:
node *Head=NULL,*Last=NULL;

void insert(int x)
{
node *temp=Head;
node *nn=new node;
nn->data=x;
nn->prev=nn->next=NULL;

if(Head==NULL)
{
Head=nn;
Last=nn;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=nn;
nn->prev=temp;
Last=nn;
}
}
void compare()
{
node *temp=Head;
node *temp1=Last;
int a;
while((temp!=temp1->next || temp1!=temp->prev)&&(temp!=NULL &&
temp1!=NULL))
{
if(temp->data<15)
temp=temp->next;

if(temp1->data>=15)
temp1=temp1->prev;

if(temp->data>=15 && temp1->data<15)


{
a=temp->data;
temp->data=temp1->data;
temp1->data=a;
temp=temp->next;
temp1=temp1->prev;
}
}
}
void print()
{
node *temp=Head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
};

int main()
{
marks m;
int i=0,x,n;
cin>>n;
while(i++<n)
{
cin>>x;
m.insert(x);
}
m.compare();
m.print();
return 0;
}

OUTPUT:
6. A stack can be used to convert a decimal number into its binary equivalent. But
unfortunately you are allowed access to only the functions of a dynamic queue (say,
enqueue(), dequeue(), queuecount() etc). Still the following two indirect methods can
be adopted to implement the stack using the functions of a queue. Implement both the
methods to obtain the desired result.
i) Using a single queue,Q
Push:
a) Find the count of the elements of the queue, C
b) Enqueue the new element
c) Dequeue ‘C’ elements and enqueue them again
Example:
Initially C=0. Enqueue the first element 10. Dequeue ‘0’ elements and enqueue
them. So, the queue remains unaltered
10

Next if the user wishes to push 20, C=1; Enqueue 20; Dequeue 1 element and
enqueue it again
10 20

20 10

User wishes to push 30; C=2; Enqueue 30; Dequeue 2 elements and enqueue
them.
20 10 30

30 20 10

Pop:
a) Dequeue the first element and return it as the popped element
In the above example, dequeue the first element 30 and return it

ii) Using two queues, Q1 and Q2


Push:
If Q1 is not empty, enqueue the element into Q1. Else enqueue into Q2
Example:
Let’s say we have to push two elements, 10, 20 and 30. Enqueue them into Q2
Q1

Q2 10 20 30
Pop:
a) If Q1 is not empty, transfer n-1 elements from Q1 to Q2 and dequeue the last
element from Q1 and return it as the popped element.
b) If Q2 is not empty, transfer n-1 elements from Q2 to Q1 and dequeue the last
element from Q2 and return it as the popped element.
10 20
Q1

Q2 30
Now return 30 as the popped element. If 40 has to be pushed next,
enqueue 40
into Q1.

10 20 40
Q1

Q2

Sample Input:
19
Output:
10011

PROGRAM:

#include <iostream>

using namespace std;

class node

public:

int data;

node* next;

};
class queue

public:

node* front=NULL, *rear=NULL;

void enqueue(int x)

node* nn =new node;

nn->data=x;

nn->next=NULL;

if(rear==NULL)

front=nn;

rear=nn;

else

rear->next=nn;

rear=nn;

int dequeue()

node* temp=front;

if((front==NULL) || (front==rear->next))

cout<<"empty";
else

front=front->next;

return(temp->data);

delete temp;

int countqueue()

int c=0;

node* temp=front;

while(temp!=NULL)

c++;

temp=temp->next;

return c;

int push(int x)

int c,i=0,y;

node* temp=front;

enqueue(x);

c=countqueue();
while(i++<c-1)

y=dequeue();

enqueue(y);

return 0;

int pop()

node* temp=front;

while(front!=NULL)

cout<<front->data;

dequeue();

void dec_bin(int x)

int y,z;

y=x/2;

z=x%2;

push(z);

while(y>0)

z=y%2;
push(z);

y=y/2;

pop();

};

int main()

int x;

queue q;

cin>>x;

q.dec_bin(x);

return 0;

OUTPUT:

You might also like