0% found this document useful (0 votes)
119 views6 pages

Assignment II

The document contains the solutions to 3 programming questions on data structures. For question 1, the student implements a stack and queue using linked lists in C. For question 2, the student writes a C program to reverse a list of integers using a stack. For question 3, the student simulates a round-robin process scheduler using priority scheduling in C. Pseudocode is provided to sort processes by priority and calculate waiting times and turnaround times.
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)
119 views6 pages

Assignment II

The document contains the solutions to 3 programming questions on data structures. For question 1, the student implements a stack and queue using linked lists in C. For question 2, the student writes a C program to reverse a list of integers using a stack. For question 3, the student simulates a round-robin process scheduler using priority scheduling in C. Pseudocode is provided to sort processes by priority and calculate waiting times and turnaround times.
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/ 6

17/04/2020

Assignment on Programming and Data Structures


Name- Kshitij Soni Roll No. 19PE10041

Ques 1. Implement Stack and Queue using linked list


Ans PART A : STACK

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

struct Node
{
int data;
struct Node *next;
}*top = NULL;

void push(int);
void pop();
void display();

void main()
{
int choice, value;
printf("\n:: Stack using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}

PART B : QUEUE
#include<stdio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;

void insert(int);
void delete();
void display();

void main()
{
int choice, value;
printf("\n:: Queue Implementation using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
insert(value);
break;
case 2: delete(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void insert(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else{
rear -> next = newNode;
rear = newNode;
}
printf("\nInsertion is Success!!!\n");
}
void delete()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n",temp->data);
}
}

Ques 2: Reverse a list of integers by using a Stack


Ans:

#include <stdio.h>
#include <string.h>

#define MAX 100 /*maximum no. of characters*/


/*stack variables*/
int top=-1;
int item;
/***************/

/*string declaration*/
char stack_string[MAX];

/*function to push character (item)*/


void pushChar(char item);

/*function to pop character (item)*/


char popChar(void);

/*function to check stack is empty or not*/


int isEmpty(void);

/*function to check stack is full or not*/


int isFull(void);

int main()
{
char str[MAX];

int i;

printf("Input a string: ");


scanf("%[^\n]s",str); /*read string with spaces*/
/*gets(str);-can be used to read string with spaces*/

for(i=0;i<strlen(str);i++)
pushChar(str[i]);

for(i=0;i<strlen(str);i++)
str[i]=popChar();

printf("Reversed String is: %s\n",str);

return 0;
}

/*function definition of pushChar*/


void pushChar(char item)
{
/*check for full*/
if(isFull())
{
printf("\nStack is FULL !!!\n");
return;
}

/*increase top and push item in stack*/


top=top+1;
stack_string[top]=item;
}
/*function definition of popChar*/
char popChar()
{
/*check for empty*/
if(isEmpty())
{
printf("\nStack is EMPTY!!!\n");
return 0;
}

/*pop item and decrease top*/


item = stack_string[top];
top=top-1;
return item;
}

/*function definition of isEmpty*/


int isEmpty()
{
if(top==-1)
return 1;
else
return 0;
}

/*function definition of isFull*/


int isFull()
{
if(top==MAX-1)
return 1;
else
return 0;
}

Ques 3: Simulate a round-robin queue of processes (that uses single CPU)

Ans 3:
/*PRIORITY SCHEDULING*/

#include <stdio.h>
#include <conio.h>

void main()
{
int i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0;
float awt,atat;
clrscr ();
printf("\n-----------------PRIORITY SCHEDULING-------------------\n");
printf("Enter the number of process");
scanf("%d",&n);
for(i=0;i<n;i++)
{
pid[i]=i;
printf("Enter the Burst time of Pid %d:",i);
scanf("%d",&pr[i]);
}
//Sorting start
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if (pr[i]>pr[j])
{
t=pr[i];
pr[i]=pr[j];
pr[j]=t;

t=bt[i];
bt[i]=bt[j];
bt[j]=t;

t=pid[i];
pid[i]=pid[j];
pid[j]=t;

}
}
//Sorting finished
tat[0]=bt[0];
wt[0]=0;

for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
}
printf("\n---------------------------------------------------------
-----\n");
printf("Pid\tPrority\tBurst time\tWaiting
Time\tTurnArroundTime\n");
printf("\n---------------------------------------------------------
-----\n");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",
pid[i],pr[i],bt[i],wt[i],tat[i]);
}
for(i=0;i<n;i++)
{
ttat=ttat+tat[i];
twt=twt+wt[i];
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n\nAvg.Waiting Time: %f\n Avg.Turn Around Time:
%f\n",awt,atat);
getch();
}
***Note – Copy the exact code and run the program.***

You might also like