Data Structures: 1.linear - Linked, Stack, Queue 2.nonlinear-Trees, Graph

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

DATA STRUCTURES

Data structures are widely used in many problems. Many times these are combined or modified
according to the problem.
For example :-
1) Stack is used in implementing recursive functions
2) Queue is used in serving requests of a single shared resource (printer, disk, CPU)
3) Linked list is used in implementing stacks, queues
4) Trees and graphs are used to implement search algorithms like bfs, dfs, and hence can be
used to check connectivity of two nodes, or to find shortest distance between them

CLASSIFICATION OF DATA STRUCTURES


1.Linear –linked,stack,queue
2.Nonlinear-trees,graph
1. Linked List: It is a collection of NODES, where node is a memory block which is
divided into two parts: INFORMATION field and ADDRESS field. Information field
holds the data part and Address part holds the address of the next node.
2. Stack: It is a linear List of elements, elements can be inserted or deleted only at one
end called the TOP of the stack. It uses the concept of LIFO (Last In First Out).
Consider the example of Stack: CD case, In CD case you put the CD one by one and the
last CD would be the one that will be first taken out from the CD case.
3. Queues: It is the list of elements, element can be inserted only at one end called the
REAR end and element can be deleted only at other end called the FRONT end. It uses
the concept of FIFO (First In First Out).You acn consider the example of queue at
the registration counter. The person standing at first position will be the one who get
first registered at the person standing at the last position will be registered at the last.
4. Trees: It is a non linear Data Structure, which is hierarchical in nature and must
maintain a hierarchy.
5. Graphs: It is a non linear data structure in which it is not necessary that the
relationship between the data items is in hierarchy.

Application of Stacks and Queues

Application of stack
1.C Program to Evaluate POSTFIX Expression Using Stack

#include<stdio.h>
int stack[20];
int top = -1;

void push(int x)
{
stack[++top] = x;
}

int pop()
{
return stack[top--];
}

int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;

OUTPUT:
Enter the expression :: 245+*

The result of expression 245+* = 18

2.Infix to Postfix Conversion in C


#include<stdio.h>
char stack[20];
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;
}

main()
{
char exp[20];
char *e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
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());
}
}

OUTPUT:

Enter the expression :: a+b*c


abc*+

Enter the expression :: (a+b)*c+(d-a)


ab+c*da-+

Application of queue:
First Come First Served (FCFS) : Itis a Non-Preemptive scheduling algorithm.
FIFO (First In First Out) strategy assigns priority to process in the order in
which they request the processor. The process that requests the CPU first is
allocated the CPU first. This is easily implemented with a FIFO queue for
managing the tasks. As the process come in, they are put at the end of the
queue. As the CPU finishes each task, it removes it from the start of the
queue and heads on to the next task.

Program:

#include<stdio.h>

int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);

printf("\nEnter Process Burst Time\n");


for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}

wt[0]=0; //waiting time for first process is 0

//calculating waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}

printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

//calculating turnaround time


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}

avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);

return 0;
}

Output
Enter total number of processes(maximum 20) : 5

Enter Process Burst Time


P[1] : 1
P[2] : 4
P[3] : 2
P[4] : 3
P[5] : 5

Process Burst Time Waiting Time Turn around Time

P[1] 1 0 1
P[2] 4 1 5
P[3] 2 5 7
P[4] 3 7 10
P[5] 5 10 15

Average Waiting Time : 4

Average Turn around Time : 7

You might also like