Bca Unit 3 Data Structures
Bca Unit 3 Data Structures
1
Q) Write short notes on Dynamic Memory allocation? (5m)
Dynamic Allocation: Allocation of memory to variables at run-time is known as
dynamic memory allocation. C provides four functions to allocate memory at runtime.
They are
1. malloc( ): Allocates a block of memory of particular size at runtime and returns a
pointer of void type.
Syntax: ptrvar=(type*)malloc(bytesize);
Eg: p=(int *) malloc(sizeof(int));
1. Linked lists are dynamic data structures. i.e., they can grow or shrink during the
execution of a program.
2. Linked lists have efficient memory utilization. Memory is allocated whenever it is
required and it is de-allocated (removed) when it is no longer needed.
2
3. Insertion and Deletions are easier and efficient. Elements can be inserted or deleted at
first, last or anywhere in the list.
4. Many complex applications can be easily carried out with linked lists.
3
Double Linked List:
It is a linked list in which all the nodes are linked together by multiple
links , which helps in accessing both successor(next) node and
predecessor(previous) node.
Each node in double linked list has two links, one points to address of
next node and other points to address of previous node.
In this, the elements can be traversed in both forward & backward
direction.
Structure of node:
Example:
Eg:
5
Basic operations on single linked list:
1.creation: To create an empty linked list.
Algorithm:
Start
1. Define the structure Node, that contains data field and Next Field.
2. Declare a pointer L of Node type
3. Set L=NULL
Code:
struct node
{
int data;
struct node *next;
};
struct node *L=NULL;
Algorithm: beginsert(int x)
start
1. Create a new node temp
2. Set temp->data=x
6
temp->next=L
3. L=temp
End
3. DELETION:
It is used remove a node from a list. The deletion operation can be performed in three ways.
They are
Deleting beginning Element from the list
Deleting last Element from the list
Deleting Specific element from the list
Deleting an element from beginning of list
8
Algorithm: begdelete()
1. If L=NULL then
a) Print “ No elements”
b) Go to end
2. temp=L;
3. L=temp->next;
4. free(temp);
Algorithm: lastdelete( )
1. If L=NULL then
c) Print “ No elements”
d) Go to end
2. Temp=L
3. If temp->next !=NULL
a) L1=Temp
b) Temp=temp->next
c) Go to step 3
4. L1->next=NULL
5. Print temp->data “ is deleted”
6. Free(temp)
9
Deleting specific element from List
Algorithm: deleteelement( x)
1. If L=NULL then
a) Print “ No elements”
b) Go to end
2. IF (L->data=x) then
1) L=L->next
2) Go to end
3. temp=L
4. while(temp->data!=x)
a) L1=temp
b) temp=temp->next
5. L1->Next = Temp->next
6. Free (temp)
4.Traverse:
Algorithm: traverse( )
Start
1. If L=NULL then
a) Print “no elements”
b) Go to end
2. Temp = L
3. If temp!=NULL Then
a) Print “ temp->data”
b) Temp=temp->next
c) Go to step 3
2.INSERTION: Adding an element into Doubly linked list is known as Insertion operation.
Algorithm : lastinsert(int x)
Start
1. Create a new node Temp
2. Set Temp->prev=NULL
Temp->data=x
Temp->next=NULL
3. if L = NULL then
a) L=temp
b) Go to End
4. L1=L
5. While L1->Next !=NULL
L1=L1->next
6. L1->NEXT=TEMP
7. TEMP->PREV=L1
end
Inserting, an element after specific location:
Algorithm : afterinsert(int x)
Start
1. Read insert Location to Loc
2. Create a new node temp
3. Set temp->prev=NULL
12
temp->data=x
temp->next= NULL
4. if L = NULL then
c) L=temp
d) Go to End
5. If loc > count
a) Print “ can’t able to insert the node”
b) Go to End
6. L1=L
7. For i=1 to Loc-1
L1=L1->next
8. Temp->next=L1->next
9. Temp->next->prev=temp
10. L1->next = temp
11. Temp->prev=L1
End
3. DELETION:
It is used remove a node from a double linked list. The deletion operation can be performed in
three ways. They are
Deleting beginning Element from the list
Deleting last Element from the list
Deleting Specific element from the list
Algorithm: begdelete()
1. If L=NULL then
a) Print “ No elements”
13
b) Go to end
2. temp=L
3. L=L->next
4. If L!=NULL
L->prev=NULL
5. Free(temp)
Algorithm: lastdelete( )
1. If L=NULL then
a) Print “ No elements”
b) Go to end
2. Temp=L
3. If temp->next !=NULL
a) Temp=Temp->next
b) Go to step 3
4. Temp->prev->next=NULL
5. Print temp->data “ is deleted”
6. Free(temp)
4. if(temp->data!=x)
a) L1=temp
14
b) temp=temp->next
c) goto step 4
5. L1->Next = Temp->next
6. If (Temp->next!=NULL)
Temp->next->prev=L1
7. Free (temp)
4.TRAVERSE:
Traversing means visiting each element in the list. Double linked list Supports both forward
and Backward Traversing.
Algorithm: traverse()
1. If L=NULL then
a) Print “ list is empty”
b) Goto end
2. Temp=L
3. If Temp!=NULL then
a) L1=Temp
b) Print Temp->data
c) Temp=Temp->next
d) Goto step 3
4. If (L1!=NULL)
a) Print L1->data
b) L1=L1->prev
c) Goto step 4
end
Q) Explain the Advantages of Double linked list over single Linked List? (**5m**)
In double linked lists we perform both forward and backward traversing where
as in single linked list only forward traversing is possible.
Eg:
Forward Traversing : 1 5 7
BackWard Traversing : 7 5 1
Reversing the doubly linked list is very easy when compared to single linked list.
Double linked list implementation is easy when compared to single linked list..
15
Deletion of nodes in double linked list is easy as compared to a Singly Linked
List.
In Circular linked list, each node is connected to next node using a single link.
Structure of Node:
Algorithm: beginsert(int x)
start
1. Create a new node temp
2. Set Temp->data=x
Temp->next=NULL
3. L1=L
4. While(L1->next !=L)
L1=L1->next
5. L1->next=temp
6. Temp->next=L
7. L=Temp
Algorithm: lastinsert(int x)
start
1. Create a new node temp
2. Set Temp->data=x
Temp->next=NULL
3. L1=L
4. While (L1->next!=L)
L1=L1->next
5. L1->next=temp
6. Temp->next=L
Algorithm : afterinsert(int x)
Start
1. Create a new node temp
2. Set Temp->data=x
Temp->next=NULL
17
3. Read location to LOC
4. L1=L
5. For i=0 to LOC-1
L1=L1->next
6. Temp->next=L1->next
7. L1->next=Temp
3. DELETION:
It is used remove a node from a circular linked list. The deletion operation can be performed
in three ways. They are
Deleting beginning Element from the list
Deleting last Element from the list
Deleting Specific element from the list
Algorithm: begdelete( )
start
1. If L=NULL then
c) Print “ No elements”
d) Go to end
2. Temp=L
18
3. If L->next=L then
a) L=Null
b) Free(temp)
c) Goto end
4. L1=L
5. while L1->next!=L then
L1=L1->next
6. L1->next=L->next
7. L=L1->next
8. Free(temp)
Deleting an element at Last of list
Algorithm: lastdelete( )
start
1. If L=NULL then
a) Print “ No elements”
b) Go to end
2. Temp=L
3. If L->next=L then
a) L=Null
b) Free(temp)
c) Goto end
4. If L->data=x then
a) L1=L
b) While L1->next!=L
L1=L1->next
c) L=L->next
19
d) L1->next=L
e) Free(Temp)
f) Go to end
5. If Temp->data!=x then
a) L1=Temp
b) Temp=Temp->next
c) Goto 5
6. L1->next=Temp->next
7. Free(Temp)
4.TRAVERSE:
Traversing means visiting each element in the list. Double linked list Supports both forward
and Backward Traversing.
Algorithm: traverse()
1. If L=NULL then
a) Print “ list is empty”
b) Goto end
2. Temp=L
3. While Temp->next!=L
a. Print Temp->data
b. Temp=Temp->next
4. Print Temp->data
23 36 72 82
20
There are two types of header linked lists
1. Singly or Doubly Header Linked List
2. Singly or Doubly Circular Header Linked List
1. Singly or Doubly Header Linked List
1. It is a list whose last node contains the NULL pointer.
2. In the header linked list the start pointer always points to the header node.
3. start -> next = NULL indicates that the header linked list is empty.
4. The operations that are possible on this type of linked list are Insertion, Deletion, and
Traversing.
Algorithm:
Start
Step 1:Initialize the Header Linked List
header->next = NULL
Start
1. Create a new node temp.
2. Set temp->data = x and temp->next = NULL.
3. Initialize a pointer current = header.
4. Traverse the list until current->next = NULL.
5. Set current->next = temp.
End
Algorithm:
1. Create a node header.
2. Set header->next = header to make it circular.
3. Input: Data x to insert at the end.
Start
4. Create a new node temp.
5. Set temp->data = x and temp->next = header.
6. Initialize a pointer current = header.
7. Traverse the list until current->next == header.
8. Set current->next = temp.
End
Similarly add Ele[2]=-5, Ele[3]=0, Ele[4]=99 elements is added and set Next[2] =2,
Next[3]=3, Next[4]=-1(last element in list)
23
Add another node
Q) Differentiate between Linked List and Arrays (or) Linked List Versus
Arrays?(*****5m or Essay***)
24
6. It is possible to grow or shrink 6. It is not possible to
the size of the list. increase/decrease the size of the
array.
7. While inserting or deleting the 7. While inserting or deleting we
elements we need not change need to change the positions of
positions. elements.
8. Memory shortage will not occur 8. Memory shortage may occur
as the elements are stored when there are contiguous
randomly. locations.
9. It uses single, double , circular 9. It has single , 2D ,multi-
list Dimensional arrays
Stack is a linear data structure in which elements are inserted and deleted from one end
called Top end.
It follows LIFO(Last-In First-Out) organisation. In this the last element inserted is the
first element to be deleted.
In stack, the insertion operation is known as PUSH and the deletion operation is known
as POP.
Stack is an Abstract Data Type (ADT) and has the following operations.
1. Creation of empty stack
4. If stack is not full, PUSH an element into stack from Top end
25
26
STACKS REPRESENTATIONS: Stacks can be represents using two data structures. They
are
1. Array representation: It is used to represent stacks of fixed size
2. Linked representation: It is used to represent stacks of variable size.
Operations of stacks using arrays:
1. creation()
2. isempty()
3. isfull()
4. push(int x)
5. pop()
6. peep()
7. traverse()
Start
1: Read size of the stack to "maxsize".
2: Create an array s[maxsize].
3: Set top= -1. End
2. isempty():This function is used to check whether stack is empty (or) not
Start
1: if (top==-1) then
Print “stack is empty”
[otherwise]
Print “stack is not empty”
End
27
3. isfull():This function is used to check whether the stack is full (or) not
Start
1: if (top==maxsize-1) then
Print “stack is full”
[otherwise]
Print “stack is not full”
End
4. push (int x): Before inserting an element into the stack we check whether the stack is
full or not. If it is full i.e Top>=maxsize-1 then overflow condition occurs otherwise
the element is inserted at the top position. This operation of the stack is called Push.
Start
1: Read an element x
2: if(top==maxsize-1)
a) Print "stack is overflow"
b) Go to end
3: set top=top+1
4: s[top]=x.
End
28
5. pop (): Before deleting an element from stack we check whether stack is empty or not. If it
is empty i.e., top=-1 then “under flow” condition occurs otherwise the element at top position
is deleted. This operation of stack is known as “POP”
Start
1) If (top= -1)
2) x = s[top]
3) top = top -1
4) print x is deleted
End
6. peep (): It is used to find the top most element in stack.
Start
1. If (top= -1)
b) Goto end
2. Print s[top]
End
7. traverse (): It is used to print the elements in stack from top to bottom.
Start 1) If (top= -1)
a) Print “under flow”
b) Goto end
2) Set i=top
3) If i>=0 then
i) print s[i]
ii) i=i-1
iii) goto 3 33 22 1
29
Q) Explain various operations of Linked Stack? (OR) Explain about the
Representation of Stacks using Linked Lists? (****VIMP****)
Stack is a linear data structure in which elements are inserted and deleted from
one end called Top end.
It follows LIFO(Last-In First-Out) organisation. In this the last element inserted
is the first element to be deleted.
In stack, the insertion operation is known as PUSH and the deletion operation is
known as POP.
The stacks which are represented using linked lists is known as Linked Stack.
Operations:
1. creation()
2. isempty()
3. push(int x)
4. pop()
5. traverse()
Start
1. Create a Node structure with two fields Data & Next.
3. Set TOP=NULL
End
30
2. isempty (): It is used to check whether stack is empty or not.
Start
1. If (TOP=NULL) then Display “stack is empty” Else
Display “stack is not empty”
End
3. push (int x): This operation is used to add an element into the linked stack
Start
1. Create a new Node Temp
3. If (TOP==NULL) Then
a) TOP=Temp
b) Go to End
4. temp->next=TOP
5. TOP=Temp [end-if]
4. pop (): This operation is used to delete an element from the linked stack
Start
31
1. If (TOP==NULL) Then
b) Go to End
2. Set Temp=TOP
3. TOP = TOP->next
End
32
5. Traverse (): It is used to print the elements from Top to bottom.
Start
1. If (TOP==NULL) Then
b) Go to End [otherwise]
2. Set Temp=TOP
3. While Temp!=NULL
a) Print Temp->data
b) Temp=Temp->next [end-if]
End
44 33 22 11
33
Explain about the Applications of Stacks? (5m) (*****VVVVIMP*********)
Stack is a linear data structure in which elements are inserted and deleted from
one end called Top end.
It follows LIFO (Last-In First-Out) organisation. In this the last element inserted
is the first element to be deleted.
Applications of stacks:
The algorithm transforms the given infix expression into postfix expression. This algorithm
uses stack to hold the operators & left parenthesis temporarily.
Algorithm to convert Infix to Postfix
Let, X is an infix expression and Y be the postfix expression.
1. Scan infix expression from left to right and repeat Step 2 to 6 for each element of infix
expression.
34
2. If an operand is encountered, add it to postfix expression Y.
a) If incoming operator priority is same or lower than the operator at top then
Repeatedly pop from Stack and add to postfix expression Y
b) If stack is empty or incoming operator priority is greater than operator at top
then Add operator to Stack.
5. If a right parenthesis is encountered, then:
a) Repeatedly pop from Stack and add to postfix expression Y until a left
parenthesis is encountered.
b) Remove the left Parenthesis.
6. Repeatedly pop from stack until stack is empty and add it postfix expression Y.
35
Program for infix to postfix
#include<stdio.h> #include<ctype.h> char s[100];
int top=-1;
int isoperator(char ch)
{
if ((ch=='*') ||(ch=='+') || (ch=='-') ||(ch=='/')) return(1);
else return(0);
}
void push(char ch)
{
top=top+1; s[top]=ch;
}
int pop()
{
char x1; x1=s[top];
top--; return(x1);
}
int priority(char ch)
{
if(ch=='(') return 0;
if(ch=='+'||ch=='-') return 1;
if(ch=='*' ||ch=='/') return 2;
}
main()
{
36
char exp[100], ch,x; int i=0;
puts("enter infix expression"); gets(exp);
ch=exp[i];
while (ch!='\0')
{
if (isalnum(ch)) printf("%c", ch);
else if(ch=='(') push(ch);
else if (isoperator(ch)==1)
{
while((priority(s[top])>=priority(ch))&&(top!=-1))
{
x=pop(); printf("%c",x);
}
push(ch);
}
else if (ch==')')
{
x=pop(); while(x!='(')
{
printf("%c",x); x=pop();
}
} i++;
ch=exp[i];
}
while(top!=-1)
{
x=pop(); printf("%c",x);
}
37
}
The algorithm transforms the given infix expression into prefix expression. This algorithm
uses stack to hold the operators & left parenthesis temporarily.
Algorithm to convert Infix to Postfix
Let, X is an infix expression and Y be the prefix expression. 1.Reverse the given Infix
Expression.
2. Scan expression from left to right and repeat Step 3 to 6 for each element of infix
expression.
3. If an operand is encountered, add it to prefix expression Y.
a) If incoming operator priority is same or higher than the operator at top then
Repeatedly pop from Stack and add to prefix expression Y
b) Add operator to Stack.
a) Repeatedly pop from Stack and add to prefix expression Y until a right
parenthesis ( ( ) is encountered.
b) Remove the right Parenthesis.
7. Repeatedly pop from stack until stack is empty and add it prefix expression Y.
38
Q) Explain about the Evaluation of Postfix Expression? (5m)
1. Read an element from postfix expression and For each element of postfix expression
repeat step 2 to step 3
2. If element is operand, then
4. If no more elements pop the result from stack else goto step 1
39
40
Q) Explain about the Evaluation of prefix Expression (5m)
5. If no more elements pop the result from stack else goto step 1
EXAMPLE:
41
42
43
Q) What is Recursion? Explain about it
Recursion is the process of calling the same function within itself any no of times.
Stacks are used to implement recursive procedures and transforming recursive
procedures to non-recursive procedures.
It is used to solve complex algorithms like towers of honai, merge sort, quicksort etc
Characteristics of Recursive Function
1. Each time a function calls to itself must be closer to the solution
2. There must be some condition to stop the process of computation; otherwise, it may
result in infinite loop.
Every recursive solution has two cases. They are
Base Case – In this, the problem is simply solved directly without making further
calls
44
Eg: if n==0
return 1;
Recursive Case – In this , the problem is solved by calling the same function within
itself any
no of times
Eg: return n * fact(n-1)
Example:
int fact (int n)
{
If (n==0)
return(1);
Else return(n*fact(n-1));
}
45