Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ds Solbank U2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Soundarya Institute of Management and Science

CA-C3T: DATA STRUCTURES

UNIT - II [12 Hours]

Linked list: Definition, Representation of Singly Linked List in memory, Traversing a Singly linked list, Searching in
a Singly linked list, Memory allocation, Garbage collection, Insertion into a singly linked list, Deletion from a
singly linked list; Doubly linked list, Header linked list, Circular linked list. Stacks: Definition, Array representation
of stacks, Linked representation of stacks, Stack as ADT, Arithmetic Expressions: Polish Notation, Conversion of
infix expression to postfix expression, Evaluation of Post fix expression, Application of Stacks, Recursion, Towers
of Hanoi, Implementation of recursive procedures by stack. Queues: Definition, Array representation of queue,
Linked list representation of queues. Types of queue: Simple queue, Circular queue, Double-ended queue,
Priority queue, Operations on Queues, Applications of queues

Two Mark Questions


1. Define Linked List?
A linked list is a linear data structure that can be defined as collection of objects
called nodes which are randomly stored in the memory.

2. What are the advantages and disadvantages of linked list over arrays.
Advantages • Efficient Memory Utilization (Arbitrary memory allocation)
• Insertion and deletion operations are easier and efficient
Disadvantages • Extra Memory spaces
• Random Access

3. What is Dynamic Memory Allocation.


The process of allocating memory during the execution of the program or the process of
allocating memory to the variables at the runtime is called dynamic memory allocation.
Ex: malloc()
4. Differentiate between malloc() & calloc()
Malloc() Calloc()
• malloc() allocates a memory block of given size (in bytes) • calloc() allocates the memory and
and returns a pointer to the beginning of the block. also initializes every byte in the
• malloc() doesn’t initialize the allocated memory. allocated memory to 0

• malloc() takes a single argument, which is the number of • calloc() takes two arguments:
bytes to allocate. 1) Number of blocks to be allocated.
2) Size of each block in bytes.

5. Define Garbage collection.


The garbage collector (GC) manages the allocation and release of memory. The garbage
collector serves as an automatic memory manager. Based on a periodic time basis or based
on specific criteria GC collects the memory which is no more in use.
6. Differentiate between Singly linked list with Doubly linked list
Singly linked list Doubly linked list
The Singly linked list has two segments: The doubly linked list has three segments.
data and link. First is data and second, third are the
pointers.

It permits traversal components only in It permits two way traversal.


one way.

7. Define Stack. List any two basic operations.


Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle.
Basic operations are :
o push(): When we insert an element in a stack then the operation is known as a push. If the
stack is full then the overflow condition occurs.
o pop(): When we delete an element from the stack, the operation is known as a pop. If the
stack is empty means that no element exists in the stack, this state is known as an underflow
state.
8. List any two applications of stacks
Applications of Stacks are :
(a) Recursion
(b) Infix to postfix conversion
(c) Reversal of a string
(d) Infix to Prefix conversion
(e) Checking the Parenthesis matching.
9. Write Stack as ADT.
A Stack s of type T is a finite sequence of elements with the operations :
• isFull(s), This is used to check whether stack is full or not
• isEmpty(s), This is used to check whether stack is empty or not
• push(x), This is used to push x into the stack
• pop(), This is used to delete one element from top of the stack
• peek(s), This is used to get the top most element of the stack
• size(s), this function is used to get number of elements present into the stack

10. Convert infix to postfix for ( a + b / c * d) – f + e


(a+b/c*d)–f+e
➔ (a+bc/*d)–f+e
➔ (a+bc/d*)–f+e
➔ (ab+c/d*)–f+e
➔ (ab+c/d*)–fe+
➔ ab+c/d*fe+-
11. Define Recursion.
Recursion is a process in which a function calls itself as a subroutine.
Ex: fibo(n) = fibo(n-1) + fibo(n-2)
12. Define Queue. List its basic operations.
A queue can be defined as an ordered list which enables insert operations to be performed
at one end called REAR and delete operations to be performed at another end called FRONT.
Queue is referred to be as First In First Out list.

13. List different types of queues.


• Simple Queue or Linear Queue
• Circular Queue
• Priority Queue
• Double Ended Queue (or Deque)

14. Define priority queue.


It is a special type of queue in which the elements are arranged based on the
priority associated with it. Suppose some elements occur with the same priority, they will be
arranged according to the FIFO principle.

15. List any two applications of queues.


• Applied as waiting lists for a single shared resource like CPU, Disk, Printer.
• Applied as buffers on MP3 players and portable CD players.
• Applied on Operating system to handle interruption.
• Applied to add song at the end or to play from the front.
16. Write Queue as ADT.
Queue q of type T is a finite sequence of elements with the operations :
• CreateQueue(q) creates a new queue that is empty
• Insert(item) : Add an element on the end of the queue
• Delete() : Remove the element at the beginning of the queue
• IsEmpty(q) : Returns true, if the queue is empty
• Traverse(q) : Access all the elements in the queue

17. What is underflow and overflow conditions in Queue?


Overflow : The condition arises when the queue is completely full in spite of that we try to
insert an item into a queue.
Insert
10 20 30 40 50 Overflow
Ove
Underflow : The condition arises when the queue is empty , we try to delete
an element from a queue.
Delete
Underflow
18. Define double ended queue or Dequeue.
In Deque or Double Ended Queue, insertion and deletion can be done from both ends of the
queue either from the front or rear. Deque has two types : Input restricted deque and Output
restricted Deque. Ex: Dequeue can be used in Palindrome checker

19. Distinguish between linear queue and circular queue.

Linear Queue Circular Queue


Arranges the data in a linear pattern. Arranges the data in a circular order where the
rear end is connected with the front end.
The insertion and deletion operations Insertion and deletion are not fixed and it can be
are fixed i.e, done at the rear and front done in any position.
end respectively.
Long Answers ( 4M / 5M / 8 M Questions )
• Refer LAB Cycle programs (4,5,6,7,8,9,10,11,12,13)
• Sample format to write the answer listed below
• Note (only few questions are listed) rest please refer LAB programs.

1. Write a C-function or algorithm to insert an item at the beginning of the single linked list.
typedef struct node
{
int info;
struct node *link;
}NODE;

NODE *header=NULL;

void INSERT_BEGIN (int item)


{
NODE *newnode;
newnode = (NODE *) malloc(sizeof(NODE));
newnode->info=item;
newnode->link=NULL;
header= newnode;
}

2. Write a C-function or algorithm to insert an item at the end of the single linked list.
typedef struct node
{
int info;
struct node *link;
}NODE;

NODE *header=NULL;

void INSERT_END (int item)


{
NODE *newnode,*curptr;
newnode = (NODE *) malloc(sizeof(NODE));
newnode->info=item;
newnode->link=NULL;
if(header==NULL)
header=newnode;
else
{
curptr=header;
while(curptr->link !=NULL)
{
curptr=curptr->link;
}
curptr->link=newnode;
}
}
3. Write a C-function or algorithm to delete an item from the beginning of the single linked list.

typedef struct node


{
int info;
struct node *link;
}NODE;

NODE *header=NULL;

void DELETE_BEGIN(int item)


{
NODE *curptr=header, *prevptr=header;

if(header==NULL)
{
printf("\n EMPTY LIST");
}
else if(header->info==item)
{
header=header->link;
free(curptr);
}
else
{
while(curptr!=NULL)
{
if(curptr->info==item)
{
prevptr->link=curptr->link;
free(curptr);
curptr=curptr->link->link;
}
else
{
prevptr=curptr;
curptr=curptr->link;
}
}
}
}
4. Write a recursive program to find the GCD of three numbers.

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

int GCD(int m, int n)


{
if(n==0)
{
return(m);
}
else if(n>m)
{
return(GCD(n,m));
}
else
{
return(GCD(n,m%n));
}
}

void main()
{
// three nos 4,6,8
int gcd12, gcd3;
clrscr();
gcd12=GCD(4,6);
printf("\n GCD between 4 & 6 = %d",gcd12);
gcd3=(GCD(gcd12,8));
printf("\n GCD between 4,6 & 8 = %d",gcd3);
getch();
}

5. Write a recursive program for Tower of Hanoi problem.


# include <stdio.h>

void toh(int n, char source, char temp, char dest)


{
If(n > 0)
{
toh(n-1, source, dest, temp);
printf(“Move Disk %d %c -> %c \n”, n, source, dest);
count++;
toh(n-1, temp, source, dest);
}
}
void main()
{
char source = ‘S’, temp = ‘T’, dest = ‘D’;
int n;
printf(“Enter the number of disks : \n”);
scanf(“%d”, &n);
printf(“Sequence is : “);
toh(n,source, temp, dest);
printf(“\n The number of Moves : %d”, count);
}

6. Write a C-function or algorithm to insert elements into circular queue.


ARRAY IMPLEMENTATION : (LINKED LIST CAN ALSO BE WRITTEN)

#define size 4
int front = - 1;
int rear = - 1;
int queue[size];

void insert_CQ(int item)


{
if ((front == 0 && rear == size - 1) || (front == rear + 1))
{
printf("queue is full");
return;
}
else if (rear == - 1)
{
rear++;
front++;
}
else if (rear == size - 1 && front > 0)
{
rear = 0;
}
else
{
rear++;
}
queue[rear] = item;
}
7. Write a C-function or algorithm to delete element from linear queue.
ARRAY IMPLEMENTATION : (LINKED LIST CAN ALSO BE WRITTEN)

#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;

void delete_Q()
{
if (front == - 1 || front > rear)
{
printf("\nQueue Underflow ");
return ;
}
else
{
printf("\nElement deleted from queue is : %d", queue_array[front]);
front = front + 1;
}
}

8. Write a C program to add 6X3 + 10X2 + 0X + 5 and 4X2 + 2X + 1

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct polynomial
{
int coeff;
int power;
struct polynomial *LINK;
};
typedef struct polynomial NODE;

NODE *poly1=NULL,*poly2=NULL,*poly3 = NULL;


NODE *create_poly();
NODE *add_poly(NODE *poly1,NODE *poly2);
void display_poly(NODE *ptr);
/* To create the polynomial*/
NODE *create_poly()
{
int flag;
int coeff,pow;
NODE *tmp_node =(NODE *)malloc(sizeof(NODE));//create thefirst node
NODE *poly=tmp_node;
do
{
printf("\n Enter coeff:");
scanf("%d",&coeff);
tmp_node->coeff=coeff;
printf("\n Enter Pow:");
scanf("%d",&pow);
tmp_node->power = pow;
tmp_node->LINK=NULL;
printf("\n Do you want to add more terms? (Y=1/N=0):");
scanf("%d",&flag);
if(flag==1)
{
tmp_node->LINK=(NODE *) malloc(sizeof(NODE));
tmp_node = tmp_node->LINK;
tmp_node -> LINK = NULL;
}
} while(flag);
return poly;
}

/*add two polynomial */


NODE *add_poly(NODE *poly1, NODE *poly2)
{
NODE *tmp_node,*poly;//Temporary storage for the linked list
tmp_node=(NODE *)malloc(sizeof(NODE));
tmp_node->LINK = NULL;
poly3=tmp_node;
//Loop while both of the linked list have value
while(poly1&&poly2)
{
if(poly1->power > poly2->power)
{
tmp_node->power=poly1->power;
tmp_node->coeff=poly1->coeff;
poly1=poly1->LINK;
}
else if (poly1->power < poly2->power)
{
tmp_node->power = poly2-> power;
tmp_node->coeff =poly2->coeff;
poly2 = poly2->LINK;
}
else
{
tmp_node->power = poly1->power;
tmp_node->coeff = poly1->coeff+poly2->coeff;
poly1=poly1->LINK;
poly2=poly2->LINK;
}
if(poly1&&poly2)
{
tmp_node->LINK=(NODE *)malloc(sizeof(NODE));
tmp_node=tmp_node->LINK;
tmp_node->LINK=NULL;
}
}
//Loop while either of the linked list has value
while(poly1||poly2)
{
tmp_node->LINK =(NODE *)malloc(sizeof(NODE));
tmp_node=tmp_node->LINK;
tmp_node->LINK=NULL;
if(poly1)
{
tmp_node->power=poly1->power;
tmp_node->coeff=poly1->coeff;
poly1=poly1->LINK;
}
if(poly2)
{
tmp_node->power=poly2->power;
tmp_node->coeff=poly2->coeff;
poly2=poly2->LINK;
}
}
}
/* Display polynomial */
void display(NODE *ptr)
{
while(ptr!=NULL)
{
printf("%dX^%d",ptr->coeff,ptr->power);
ptr=ptr->LINK;
if(ptr!=NULL)
printf(" + ");
}
}

int main()
{
clrscr();
printf("\n Create 1st Polynomial: ");
poly1=create_poly();
printf("\n First polynomial : ");
display(poly1);
printf("\n Create 2nd Polynomial:");
poly2=create_poly();
printf("\n Second polynomial :");
display(poly2);
add_poly(poly1,poly2);
printf("\n Addition of Two polynomials : ");
display(poly3);
getch();
}
9. Evaluate Postfix expression using stack 6, 3, +, 5, *, 2, 3, +, +
• Add # at the end of P
• Repeat scanning P from left to right and repeat step 3 and 4 until # is encountered
• If an operand is encountered, push it onto stack
• If an operator is encountered
• (a) Remove top two elements A & B
• (b) Evaluate B (op) A
• (c) Push the result back onto Stack
• The result will be on the top of stack
• Exit

P=63+5*23++#
Symbol Operation Performed STACK
6 PUSH(6) 6
3 PUSH(3) 6 3
+ POP A=3 , B = 6 => B + A = 6 + 3 = 9 , PUSH(9) 9
5 PUSH(5) 9 5
* POP A=5, B = 9 => B * A = 5 * 9 = 45 , PUSH(45) 45
2 PUSH(2) 45 2
3 PUSH(3) 45 2 3
+ POP A=3, B=2 => 3 + 2 = 5 , PUSH(5) 45 5
+ POP A=5 , B=45 => 5 + 45 = 50 , PUSH(50) 50
# # STOP
Therefore P[top] = 50

P=874*+3-2*#
Symbol Operation Performed STACK
8 PUSH(8) 8
7 PUSH(7) 8 7
4 PUSH(4) 8 7 4
* A=4, B=7 B * A => 7 * 4 = 28 , PUSH(28) 8 28
+ A=28, B=8 B + A => 28 + 8 =36, PUSH(36) 36
3 PUSH(3) 36 3
- A=3, B=36, B - A=> 36-3=33, PUSH(33) 33
2 PUSH(2) 33 2
* A=2, B=33 , B * A => 33 * 2 = 66 PUSH(66) 66
# STOP
10. Convert infix to postfix Q = A + ( B * C – ( D / E F ) * G ) * H )
• Push ( on the stack and add right parenhesis at the end
• Scan Q from left to right
• if operand is found, add it to P
• if operator is encountered
• (a) pop from stack and add to p which has the same or higher precedence
• (b)push operator on to stack
• if ) encountered
• (a) Pop from stack add to P each operator until ( parenthis is encountered
• (b) Remove (
• Exit
Convert infix to postfix Q = ( x ^ y / ( 5 * z ) + 2 )

You might also like