Professional Documents
Culture Documents
------------------------------------------------------------------------------------------------------------------------------------------
UNIT–I:
1. What is a data structure? Why we use data structures? What are the types of data
structures? Give examples.(GRIET/June/2015)
Ans: Data Structure:
A data structure is a systematic way of organizing and accessing data.
1. Data structures study how data is stored in a computer so that operations can be
implemented efficiently
2. Data structures are especially important when we have large amounts of information
3. Conceptual and concrete ways to organize data for efficient storage and manipulation.
1
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Stack is a Linear data structure which follows Last In First Out (LIFO)mechanism.It means:
the first element inserted is the last one to be removed.Stack uses a variable called top which
points the top most element in the stack. top is incremented while pushing (inserting) an
element into the stack and decremented while popping (deleting) an element from the stack
Applications of Stack:
2
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Queue:
Queue is a linear data structure which follows First in First out (FIFO) mechanism. It means: the
first element inserted is the first one to be removed. Queue uses two variables rear and front.
Rear is incremented while inserting an element into the queue and front is incremented while
deleting element from the queue
Note:
While inserting an element into the queue, queue is full condition should be checked
While deleting an element from the queue, queue is empty condition should be checked
Applications of Queues:
Real life examples
Waiting in line
Waiting on hold for tech support
Applications related to Computer Science
Threads
Job scheduling (e.g. Round-Robin algorithm for CPU allocation)
Linked List:
Linked list consists of nodes of data which are connected with each other. Every node consists of
two parts data and the link to other nodes. The nodes are created dynamically.
3
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Root Node: The starting node of a tree is called Root node of that tree
Terminal Nodes: The node which has no children is said to be terminal node or leaf .
Non-Terminal Node: The nodes which have children is said to be Non-Terminal Nodes
Degree: The degree of a node is number of sub trees of that node
Depth: The length of the largest path from root to terminals is said to be depth or height
of the tree
Siblings: The children of same parent are said to be siblings
Ancestors: The ancestors of a node are all the nodes along the path from the root to the
node
4
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
A Property Value
Number of nodes : 9
B C Height : 4
Root Node : A
Leaves : ED, H, I, F, C
D E F
Interior nodes : D, E, G
Number of levels : 5
Ancestors of H : I
G
Descendants of B : D,E, F
H I Siblings of E : D, F
Ans: Stack: Stack is a linear data structure in which a data item is inserted and deleted at one end
(called top of the stack). A stack is based on the principle Last in First Out (LIFO).It means: the
first element inserted into the stack is the last one to be removed from the stack.
Eg: pile of books, stack of plates, stack of coins etc
Operations on stack: Stack supports the following operations:
5
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Step 3 :stack[top]element
Pop operation: The pop operation is used to delete an element from the stack. In pop operation
the topmost element from the stack will be deleted. However before deleting an element from the
stack we have to see whether some element exists in the stack or not i.e if the stack is empty
we cannot do so. Therefore before deleting an element from the stack we have to check the
underflow condition.(Stack underflow : Attempt to delete an element when the stack is empty).
6
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
int top=-1;
void push(int);
void pop();
void print();
void main()
{
int item, ch;
while(1)
{
printf ("\NMAIN MENU");
printf ("\n1.PUSH (Insert) in the Stack");
printf ("\n2.POP (Delete) from the Stack");
printf(“\n3.Display”);
7
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
switch (ch)
{
case 1:printf ("\nEnter the Element to be pushed”);
scanf ("%d", &item);
push ( item);
break;
case 2: pop ();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
/*PUSH FUNCTION*/
void push (int element)
{ if (top == STACK_SIZE-1))
printf(“Stack is full”);
else
{ top=top+1;
stack [top] = element;
}
}
/*POP FUNCTION*/
void pop ()
{
if (top == -1
8
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
printf(“stack is empty”);
else
{ printf(“%d is popped from stack”,stack[top]);
top--;
}
}
/*FUNCTION TO DISPLAY STACK*/
void display (int stack[])
{ int i;
if (top == -1)
printf ("empty");
else
{ printf(“the elements in the stack are”);
for (i=top; i>=0; --i)
printf ("\n--------\n|%3d |\n--------",stack[i]);
}
6. Explain in detail how stacks can be used in a recursive function call with an example.
I.Stack:Top contains run-time Stack i.e stack of activation records of active functions which
stores function’s local environment i.e return address, arguments, local variables, return
value.
9
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Recursion:
A function calling itself is called a recursive function. (Direct recursion). A function call to another
function, which in turn calls the first function is called indirect recursion.
10
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
{
int fact;
if(n==1)
return 1;
else
fact=n*factorial(n-1);
return fact;
}
11
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Ex: + AB
3. Postfix: An operator is placed after the operands is called as potsfix expression or
reverse polish notation or suffix notation.
<opearand><operand><operator>
Ex: AB+
Stacks can be used to evaluate expressions and also to convert expressions from one form to
another form.
Rules for converting expressions:
1. Precedence rules decide the order in which different operators are applied .
High priority: *, /,%
Low priority: + ,-
2. Associativity rule decides the order in which multiple occurrences of the same level of
operators are applied .
) ( AB*
- (,- AB*
( (,-,( AB*
C (,-,( AB*C
* (,-,(,* AB*C
D (,-,(,* AB*CD
) (,- AB*CD*
) AB*CD*-
14
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
{
postfix[j]=pop(stack);
j++;
}
temp=pop(stack);
}
}//for
while((top!=-1))
{ postfix[j]=pop(stack);
j++;
}
postfix[j]='\0';
printf("postfix exp is:%s",postfix);
intgetpriority(char op)
{
if(op=='/'||op=='*'||op=='%')
return 1;
else if(op=='+'||op=='-')
return 0;
else
return -1;
15
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Using stacks, any postfix expression can be evaluated very easily. Every character of the postfix
expression is scanned from left to right.
If the character encountered is an operand, it is pushed onto the stack
If an operator is encountered, then the top two values are popped from the stack and the
operator is applied on these values.
The result is then pushed onto the stack
1. Scan every character of postfix expression from left to right and repeat step 3 to step
4until end of postfix expression
2. If an operand is encountered, then push it on to the stack.
3. If an operator is encountered, then
a) Pop the top two operands from the stack, where A is the top operand and B is
next-to-top operand.
b) Evaluate B operator A
c) Push the result of evaluation onto the stack
4. Print “result of expression” as the top of the stack.
16
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Sol: 934*8+4/-
17
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
case '%':value=(int)op1%(int)op2;
break;
}
push(stack,value);
}
}
result=pop(stack);
printf("\nvalue of the postfix expression=%.2f",result);
}
9. Define Recursion? With examples explain about Tail and Non-Tail Recursion.
(GRIET/June/2015)
Ans:
Recursion:A function which calls itself is called a recursive function, the call is recursive call
and the process of function implementation is recursion.
18
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Tail recursive functions are highly desirable because they are much more efficient to use the
amount of information that has to be stored on the system stack is independent of the number
of recursive calls.
Example:
int fact(int n)
{
return fact1(n,1);
}
int fact1(int n, int res)
{
if(n==1)
return res;
else
return fact1(n-1,n*res);
}
Non tail Recursion: whenever there is a pending operation to be performed, the function
becomes non-tail recursive. In such a non-tail recursive function, information about each
pending operation must be stored, so the amount of information directly depends on the
number of calls.
Example:
factorial(n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
When returning back from a recursive call, there is still one pending
operation, multiplication.Therefore, factorial is a non-tail recursive method
10. Give the applications of Stack.
Ans:
1. Converting a decimal number into a binary number
2. The simplest application of a stack is to reverse a word.
3. "undo" mechanism in text editors; this operation is accomplished by keeping all text
changes in a stack.
4. Language processing:
space for parameters and local variables is created internally using a stack.
compiler's syntax check for matching braces is implemented by using stack.
support for recursion
5. Expression Conversion
Infix to Postfix
Infix to Prefix
19
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Postfix to Infix
Prefix to Infix
6. Expression Evaluation
11. Convert the following Infix expression into Prefix expression? x+b-y+d*e/a.
(GRIET/June/2015)
Ans:
x+b-y+d*e/a ->x+b-y+[*de]/a
x+b-y+[/*dea]
[+xb]-y+[/*dea]
[-+xby]+[/*dea]
+-+xby/*dea
20