3. What is a Stack?
A stack is a data structure of ordered items such
that items can be inserted and removed only at
one end.
3
4. This strategy states that the element that is
inserted last will come out first.
You can take a pile of plates kept on top of
each other as a real-life example.
The plate which we put last is on the top and
since we remove the plate that is at the top,
we can say that the plate that was put last
comes out first.
5. In order to make manipulations in a stack, there
are certain operations provided to us.
push() to insert an element into the stack
pop() to remove an element from the stack
top() Returns the top element of the stack.
isEmpty() returns true is stack is empty else
false
size() returns the size of stack
6.
7. Push:
Adds an item to the stack. If the stack is full, then it is said to
be an Overflow condition.
Algorithm for push:
begin
if stack is full
return
endif
else
increment top
stack[top] assign value
end else
end procedure
8. Pop:
Removes an item from the stack. The items are popped in the
reversed order in which they are pushed. If the stack is empty,
then it is said to be an Underflow condition.
Algorithm for pop:
begin
if stack is empty
return
endif
else
store value of stack[top]
decrement top
return value
end else
end procedure
9. Top:
Returns the top element of the stack.
Algorithm for Top:
begin
return stack[top]
end procedure
isEmpty:
Returns true if the stack is empty, else false.
Algorithm for isEmpty:
begin
if top < 1
return true
else
return false
end procedure
10. Types of Stacks:
Register Stack: This type of stack is also a memory element
present in the memory unit and can handle a small
amount of data only. The height of the register stack is
always limited as the size of the register stack is very small
compared to the memory.
Memory Stack: This type of stack can handle a large
amount of memory data. The height of the memory stack
is flexible as it occupies a large amount of memory data.
11. Infix to Postfix /Prefix conversion
Redo-undo features at many places like editors,
photoshop.
Forward and backward features in web
browsers
Used in many algorithms like Tower of
Hanoi, tree traversals, stock span problems,
and histogram problems
12. There are two ways to implement a stack
Using array
Using linked list
13. Advantages of array implementation:
Easy to implement.
Memory is saved as pointers are not involved.
Disadvantages of array implementation:
It is not dynamic.
It doesn’t grow and shrink depending on
needs at runtime.
14. There are two ways we can implement a
stack:
Using an array
Using a linked list
14
15. Implementing a stack using an array is fairly
easy.
The bottom of the stack is at data[0]
The top of the stack is at data[numItems-1]
push onto the stack at data[numItems]
pop off of the stack at data[numItems-1]
15
16. We can use a stack to reverse the letters in a
word.
How?
16
17. Read each letter in the word and push it onto
the stack
When you reach the end of the word, pop
the letters off the stack and print them out.
17
19. What is a queue?
A data structure of ordered items such that items
can be inserted only at one end and removed at
the other end.
Example
A line at the supermarket
19
20. What can we do with a queue?
Enqueue - Add an item to the queue
Dequeue - Remove an item from the queue
These ops are also called insert and getFront
in order to simplify things.
20
21. A queue is called a FIFO (First in-First out)
data structure.
What are some applications of queues?
Round-robin scheduling in processors
Input/Output processing
Queueing of packets for delivery in networks
21
22. few more functions are required to make
the above-mentioned queue operation
efficient. These are −
peek() − Gets the element at the front of
the queue without removing it.
isfull() − Checks if the queue is full.
isempty() − Checks if the queue is empty.
23. Insert Operation
Queues maintain two data pointers, front and rear. Therefore, its
operations are comparatively difficult to implement than that of
stacks.
The following steps should be taken to enqueue (insert) data into a
queue −
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce overflow error and exit.
Step 3 − If the queue is not full, increment rear pointer to point the
next empty space.
Step 4 − Add data element to the queue location, where the rear is
pointing.
Step 5 − return success.
24. Accessing data from the queue is
a process of two tasks − access
the data where front is pointing
and remove the data after access.
The following steps are taken to
perform dequeue operation −
•Step 1 − Check if the queue is
empty.
•Step 2 − If the queue is empty,
produce underflow error and exit.
•Step 3 − If the queue is not
empty, access the data
where front is pointing.
•Step 4 − Increment front pointer
to point to the next available data
element.
•Step 5 − Return success.
25. Just like a stack, we can implementing a
queue in two ways:
Using an array
Using a linked list
25
26. Using an array to implement a queue is
significantly harder than using an array to
implement a stack. Why?
Unlike a stack, where we add and remove at the
same end, in a queue we add to one end and
remove from the other.
26
27. There are two options for implementing a
queue using an array:
Option 1:
Enqueue at data[0] and shift all of the rest of the
items in the array down to make room.
Dequeue from data[numItems-1]
27
28. Option 2
Enqueue at data[rear+1]
Dequeue at data[front]
The rear variable always contains the index of the last
item in the queue.
The front variable always contains the index of the
first item in the queue.
When we reach the end of the array, wrap around to
the front again.
28
30. // option 2 sketch of getFront
Object getFront() {
answer = data[front];
front = (front + 1) mod size;
manyItems--;
return answer
}
30
31. Linked List can be defined as collection of objects
called nodes that are randomly stored in the
memory.
A node contains two fields i.e. data stored at that
particular address and the pointer which contains
the address of the next node in the memory.
The last node of the list contains pointer to the
null.
32. Singly linked list or One way chain
Singly linked list can be defined as the collection of ordered
set of elements. The number of elements may vary according to
need of the program. A node in the singly linked list consist of two
parts: data part and link part. Data part of the node stores actual
information that is to be represented by the node while the link part
of the node stores the address of its immediate successor.
33. There are various operations which can be performed on
singly linked list. A list of all such operations is given below.
Node Creation
1. struct node
2. {
3. int data;
4. struct node *next;
5. };
6. struct node *head, *ptr;
7. ptr = (struct node *)malloc(sizeof(struct node *));
34. SN Operation Description
1 Insertion at beginning It involves inserting any element at the front of the list. We
just need to a few link adjustments to make the new node
as the head of the list.
2 Insertion at end of the list It involves insertion at the last of the linked list. The new
node can be inserted as the only node in the list or it can
be inserted as the last one. Different logics are
implemented in each scenario.
3 Insertion after specified node It involves insertion after the specified node of the linked
list. We need to skip the desired number of nodes in order
to reach the node after which the new node will be
inserted. .
Insertion
The insertion into a singly linked list can be performed at different positions. Based on
the position of the new node being inserted, the insertion is categorized into the
following categories.
35. SN Operation Description
1 Deletion at beginning It involves deletion of a node from the beginning of the list. This is the
simplest operation among all. It just need a few adjustments in the
node pointers.
2 Deletion at the end of
the list
It involves deleting the last node of the list. The list can either be empty
or full. Different logic is implemented for the different scenarios.
3 Deletion after specified
node
It involves deleting the node after the specified node in the list. we
need to skip the desired number of nodes to reach the node after
which the node will be deleted. This requires traversing through the list.
4 Traversing In traversing, we simply visit each node of the list at least once in order
to perform some specific operation on it, for example, printing data part
of each node present in the list.
5 Searching In searching, we match each element of the list with the given element.
If the element is found on any of the location then location of that
element is returned otherwise null is returned. .
The Deletion of a node from a singly linked list can be performed at different
positions. Based on the position of the node being deleted, the operation is
categorized into the following categories.
36. Doubly linked list
Doubly linked list is a complex type of linked list in which a node contains a
pointer to the previous as well as the next node in the sequence.
Therefore, in a doubly linked list, a node consists of three parts: node data,
pointer to the next node in sequence (next pointer) , pointer to the previous
node (previous pointer). A sample node in a doubly linked list is shown in
the figure.
37. A doubly linked list containing three nodes having numbers from 1
to 3 in their data part, is shown in the following image.
In C, structure of a node in
doubly linked list can be given
as :
struct node
{
struct node *prev;
int data;
struct node *next;
}
40. SN Operation Description
1 Insertion at beginning Adding the node into the linked list at
beginning.
2 Insertion at end Adding the node into the linked list to the end.
3 Insertion after specified
node
Adding the node into the linked list after the
specified node.
4 Deletion at beginning Removing the node from beginning of the list
5 Deletion at the end Removing the node from end of the list.
6 Deletion of the node
having given data
Removing the node which is present just after
the node containing the given data.
7 Searching Comparing each node data with the item to be
searched and return the location of the item in
the list if the item found else return null.
8 Traversing Visiting each node of the list at least once in
order to perform some specific operation like
searching, sorting, display, etc.
41.
42.
43.
44. Root − Node at the top of the tree is called root.
Parent − Any node except root node has one edge upward to a node
called parent.
Child − Node below a given node connected by its edge downward is
called its child node.
Sibling – Child of same node are called siblings
Leaf − Node which does not have any child node is called leaf node.
Sub tree − Sub tree represents descendants of a node.
Levels − Level of a node represents the generation of a node. If root
node is at level 0, then its next child node is at level 1, its grandchild is
at level 2 and so on.
keys − Key represents a value of a node based on which a search
operation is to be carried out for a node.
45. Degreeof anode:
• Thedegreeof anode isthe number of children of that node
Degreeof aTree:
• Thedegree of atree isthe maximumdegreeof nodesin agiventree
Path:
• It isthe sequence of consecutive edgesfrom sourcenode to destination node.
Heightof anode:
• Theheight of anode isthe maxpath length form that node to aleaf node.
Heightof atree:
• Theheight of atree isthe height ofthe root
Depthof atree:
• Depth of atree isthe maxlevelof anyleaf in the tree
48. Directory structure of a file store
Structure of an arithmetic expressions
Used in almost every 3D video game to
determine what objects need to be
rendered.
Used in almost every high-bandwidth
router for storing router-tables.
used in compression algorithms, such as
those used by the .jpeg and .mp3 file-
formats.
49. A binary tree, is a tree in which no node can
have more than two children.
Consider a binary tree T, here ‘A’ is the root
node of the binary tree T.
‘B’ is the left child of ‘A’ and ‘C’ is the right
child of ‘A’
• i.e A is a father of B and C.
• The node B and C are called siblings.
Nodes D,H,I,F,J are leaf node