0% found this document useful (0 votes)
8 views

DS Study Material Module 1

Uploaded by

dwarakadileep05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

DS Study Material Module 1

Uploaded by

dwarakadileep05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Easy Theory questions – Module I

(Study infix to postfix conversion and postfix evaluation from note book)

1.Difference between linear and non-linear data structures

2. Basic operations on all data structures

a) Insertion – adding a new data item


b)deletion - deleting an existing data item
c)searching – for a particular data item
d)traversal – accessing all data items exactly once
e)sorting- arranging data items in an order
f)merging – combining data items in two collections
3. Explain stack
a)A linear DS in which insertion and deletion can be done at ONE END ONLY. This end is
called top.
b)Stack is a Last In First Out(LIFO) data structure, means the last inserted element will be
first deleted.
4. Array representation of stack
(First explain stack)
struct stack
{
int st[20];
int top;
}
5. Basic operations on stack
a)Push – insertion to stack is called ‘push’
b)pop- deleting an element from stack is called ‘pop’
c)traverse- accessing all elements in the stack(ex. For displaying)
6. Implementation of stack using array( Algorithm & code)
Push a new data item(Algorithm) push(data)
Check If stack is full, that is, if top==max-1. {
If(top==max-1)
If yes, Print stack is full printf(“stack full/overflow);
Else, Increment top to point to the next free location else
Then insert the new data at location pointed by top. {
top++;
st[top]=data;
}
}
Popping a data from stack(Algorithm)
Check if stack is empty. That is, if top==-1 pop()
If yes, print stack is empty {
Else, print the stack top element and decrement top. If(top==-1)
Printf(“stack is empty”);
else
{
X=st[top];
Printf(“%d”,X);
top--;
}
}
Traversal

Check if stack is empty. That is, if top==-1 traverse()


If yes, print stack is empty {
Else,
print the stack elements from 0th to top. . If(top==-1)
Printf(“stack is empty”);
else
{
For(i=0;i<top;i++)
Printf(“%d”,st[i]);
}
}

To check whether stack is empty isEmpty()


Check if top==-1 {
If yes, print stack is empty If(top==-1)
Printf(“stack is empty”);
}
To check whether stack is full isFull()
Check if top==max-1 {
If yes, print stack is full If(top==-1)
Printf(“stack is full”);
}

7. Applications of stack

Some applications of stack are:


1. Conversion of an infix expression into its corresponding postfix form.
2. Evaluation of arithmetic expressions.
3. Recursion.
4. Parenthesis checking.
5. Reversing a list
6. Number system conversion (Ex. Decimal to binary, decimal to hex etc)

8. Explain Queue
a)A linear DS in which insertion and deletion can be done at TWO ENDS called REAR and
FRONT. Insertion at REAR end and deletion at FRONT end.
b)Queue is a First In First Out(FIFO) data structure, means the firsst inserted element
will be first deleted.

9. Basic operations on queue


a)enqueue – insertion to queue is called ‘enqueue’
b)dequeue- deleting an element from queue is called ‘dequeue’
c)traverse- accessing all elements in the queue(ex. For displaying)
10. Array representation of queue
struct queue
{
int q[20];
int rear,front;
}

When the first element is inserted,

When remaining elements are inserted,


11. Basic Operations on queue

Insert a new data item(Algorithm) enqueue(data)


Check If queue is full, that is, if rear==max-1. {
If(rear==max-1)
If yes, Print queue is full printf(“queue full/overflow);
Else , check if queue is empty, that is if front==-1 else if (front==-1)
Increment front and rear { front++; rear++;
q[rear]=data; }
Else, Increment rear to point to the next free location else
Then insert the new data at location pointed by rear. {
rear++;
q[rear]=data;
}
}

Deleting a data from queue(Algorithm)


Check if queue is empty. That is, if front==-1 dequeue()
If yes, print queue is empty {
Else, print the front element of queue and increment front. If(front==-1)
Printf(“queue is empty”);
else
{
X=q[front];
Printf(“%d”,X);
front++;
}
}

Traversal

Check if queue is empty. That is, if front==-1 traverse()


If yes, print queue is empty {
Else,
print the queue elements from front to rear. If(front==-1)
Printf(“queue is empty”);
else
{
For(i=front;i<rear;i++)
Printf(“%d”,q[i]);
}
}

To check whether queue is empty isEmpty()


Check if front==-1 or front>rear {
If yes, print stack is empty If((front==-1) || (front>rear))
Printf(“queue is empty”);
}
To check whether queue is full isFull()
Check if rear==max-1 {
If yes, print queue is full If(rear==-1)
Printf(“queue is full”);
}

12. Types of queue


DOUBLE ENDED QUEUE (DEQUEUE)
In double ended queue insertions and deletions can be done from both the ends.

There are five operations in double ended queue.


1. Insertion at rear end.
2. Insertion at front end.
3. Deletion from rear end.
4. Deletion from front end.
5. Traversal.

PRIORITY QUEUE
a)Priority queue is a variation of simple queue
b)In Priority queue each element has a value associated with it called its priority.
c)When deletion operation is performed we remove the element with highest
priority.
d)In a priority queue the elements are stored in the ascending order of priority
value so that the element with highest priority will always be at the front position.
So elements are inserted into the queue in the order of priority.

CIRCULAR QUEUE
Consider the following case in a simple queue of maximum size 5.

Here Front points to the index 2 and rear points to 4 ie MAXSIZE-1. If we try to
insert a new element into the above queue, it will not be possible because
rear=MAXSIZE-1 eventhough two vacant positions are there at front. This is the
limitation of a simple queue. To avoid such limitation we can use circular queue.
In this queue the rear is incremented in such a way that after rear points to
MAXSIZE-1 it will point to 0 if that position is empty. In this case the queue
becomes full only if rear+1 points to front.
The pictorial representation of a circular queue is:

After deleting 18 and 7, the queue becomes:

Now we can insert 4 more elements into the above queue. But in a simple queue
we can insert only 2 more elements.
After inserting three elements 30, 47 and 14 queue becomes

You might also like