Stacks:: Programming in C++ and Data Structures
Stacks:: Programming in C++ and Data Structures
1. Stacks:
A stack is an ordered collection of homogeneous data element. In which all insertions and
deletions are made at one end called the TOP.
Stacks are sometimes referred to as Last in First out (LIFO) list. Because last inserted
element is first deleted from the stack.
Representation of Stack:
We can represent array in two ways. They are
1. Array representation
2. Linked list representation
Array representation:
The insertion and deletion operations in the case of a stack are specially termed PUSH
and POP. An element in the stack is termed an ITEM. The maximum number of elements that a
stack can accommodate is termed SIZE. The condition TOP=0 and TOP=NULL will indicate
that the stack is empty and stack is full respectively.
Push Pop
Item 1 Top
Item 2
Bottom Item 3
Page 1
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
top
Push 6
8
8
4 4
Fig: Stack Push Operation.
Algorithm push :
Here STACK is a stack, TOP is a pointer point the top element, SIZE specifies maximum
capacity of the stack and ITEM is an element to be added into the stack. The below algorithm
adds the element ITEM into the stack.
2. Pop operation:
Pop removes the item at the top of the stack.
Returns that item to the user.
Next older item in the stack becomes the top.
Page 2
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
If the pop is called after the removal of last item, then it is in an underflow state.
Item/element
6
stack stack
Pop
6
8 top 8
4 4
Algorithm Pop :
Here STACK is a stack; TOP is a pointer point the top element and ITEM is a variable used to
hold the removed element from the stack. The following algorithm will remove the top element
of the stack and stored it in the variable ITEM.
Expression:
Expression is a string of operands or operators. In general there are three types of
expression:
a. Infix Expression
b. Prefix Expression
c. Postfix Expression.
a. Infix Expression:
Page 3
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
Example: (A + B) * C
Example: +AB, * EF
Example: AB + , EF *
Page 4
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
Algorithm:
POLISH(Q,P)
Suppose Q is an arithmetic expression written in infix notation. This finds equal postfix
expression P.
1. Get the expression through Q.
2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q until the STACK is
empty.
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it into STACK.
5. if an operator is encountered, then:
a. repeatedly POP from STACK and add to P each operator.
b. Otherwise push the operator being read on to the stack.
Endif (step 5)
6. If a right parenthesis is encountered , then:
a. Repeatedly POP from STACK and add to P each operator until a left parenthesis
is encountered.
b. Remove the left parenthesis (do not add the left parenthesis to P)
Endif (step 6)
End of step 2 loop
Page 5
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
7. Exit
Example:
(( A – ( B + C )) * D) $ ( E + F )
2. Recursion:
Recursion
2. Queues:
A queue is a ordered collection of homogeneous data element. In which data can be
inserted at one end, called the REAR, and deleted from the other end, called the FRONT.
A queue is a First in First out (FIFO) structure.
A queue is also a liner data structure like an array. In queue insertion is called as
ENQUEUE and deletion is called as DEQUEUE.
Page 6
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
(deletion)
Dequeue Front rear
Fig: model of queue
1. Enqueue operation
2. Dequeue operation
1. Enqueue Operation:
Inserting an element at the REAR position of the queue is known as ENQUEUE.
After the data have been inserted into the queue, the new element becomes the
rear of the queue.
The only potential problem with enqueue is running out of space for the data.
An error is occurs if the queue is full
item
6
9 7 4 9 7 4 6
ENQUEUE
Algorithm Enqueue:
Page 7
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
5. Else
6. If(REAR = 0) and (FRONT = 0) then // queue is empty
7. FRONT = 1
8. Endif
9. REAR = REAR + 1
10. QUEUE(REAR) = ITEM //inserting new ITEM in the QUEUE
11. Endif
12. Stop
2. Dequeue:
Deleting an element from the FRONT position of the queue is known as
DEQUEUE
The item at the front of the queue is returned to the user and removed from the
queue.
An error is occurs if the queue is empty.
item
9
9 7 4 6 7 4 6
DEQUEUE
Algorithm Dequeue:
DEQUEUE(QUEUE, FRONT,ITEM)
1. BEGIN
2. If (FRONT = 0) then
3. Print “queue is empty”
4. Exit
5. Else
6. ITEM = QUEUE(FRONT) //Get the element
7. If (FRONT = REAR) // When the queue contains a single element
8. REAR = 0 // The queue becomes empty
9. FRONT = 0
10. Else
11. FRONT = FRONT + 1
12. EndIf
Page 8
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
13. EndIf
14. Stop
3.Circular Queue:
Locations of queue are viewed in a circular form. The first location is viewed after the
last one. Overflow occurs when all the location are filled.
A queue represented using an array when the REAR pointer reaches the END, insertion
will be denied even if space is available at the FRONT.
This problem is avoid using CIRCULAR QUEUE.
rear
front
1. Enqueue operation
2. Dequeue operation
1. Enqueue Operation:
Page 9
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
3 5
front rear
Algorithm Enqueue:
1. BEGIN
2. If(FRONT = 0) then //when the queue is empty
3. FRONT = 1
4. REAR = 1
5. CQ[REAR] = ITEM
6. Else //queue is not empty
7. next = (REAR mod LENGTH ) +1
8. if(next != FRONT ) then // if the queue is not full
9. REAR = next
10. CQ[REAR] = ITEM
11. Else
12. Print “ queue is full “
13. EndIf
14. EndIf
15. END
2. Dequeue:
Deleting an element from the FRONT position of the queue is known as
DEQUEUE
The item at the front of the queue is returned to the user and removed from the
queue.
An error is occurs if the queue is empty.
Page 10
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
rear
8
3
front
Algorithm Dequeue:
1. BEGIN
2. If ( FRONT = 0 ) then
3. Print “queue empty”
4. Exit
5. Else
6. ITEM = CQ[FRONT] //item deletex
7. If ( FRONT = REAR ) then // if the queue contains single item
8. FRONT = 0
9. REAR = 0
10. Else
11. FRONT = ( FRONT mod LENGTH ) + 1
12. EndIf
13. EndIf
14. END
4.Linked List:
A linked list is an ordered collection of finite, homogeneous data elements called Nodes.
where the linear order is maintained by means of links or pointers.
A consist of two fields: DATA (to store the actual information) and LINK (to point to the
next node (i.e) contains the address of next node).
Link
Page 11
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
A list is a Dynamic Data Structure. the pointer of the last node contains a special value,
called the Null pointer. This Null pointer indicates the End of List.
In a singly linked list each node contains only one link which points to the subsequent
node in the list.
In traversing a single linked list ,we visit every node in the list staring from the first node to
the last node.
Algorithm: Traverse_SL
Steps:
1. Ptr = HEADER -> LINK //ptr is to store the pointer to the current node.
2. While (ptr != NULL) do //continue till the last node.
3. Process(ptr) //perform process on the current node.
4. EndWhile
Page 12
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
5. Stop
Steps:
1. If(AVAIL = NULL) //AVAIL is the pointer to the pool of free storage
2. Return (NULL)
3. Print “Insufficient memory :unable to allocate memory”
4. Else //sufficient memory is available.
5. Ptr = AVAIL //start from the location ,where AVAIL points.
6. While ( SizeOf (ptr) != SizeOf (NODE )) and ( ptr -> LINK != NULL ) do
//Till the desired block is found or the search reach the end of the pool
Page 13
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
Input : HEADER is the pointer to the header node and X is the data of the node to be inserted.
Output: A single Linked list with a newly inserted node at the front of the list.
Data structure: A single Linked list whose address of the starting node is known from the
HEADER.
Steps:
1. New = GetNode(NODE) //Get a memory block of type NODE and store its
pointer in new
2. If( new = NULL) then //Memory manager returns NULL on searching the
memory block
Before insertion:
header
1000
After insertion:
500
Input : HEADER is the pointer to the header node and X is the data of the node to be inserted.
Output: A single Linked list with a newly inserted node at the end of the list.
Data structure: A single Linked list whose address of the starting node is known from the
HEADER.
Steps:
1. New = GetNode(NODE) //Get a memory block of type NODE and store its
pointer in new
2. If( new = NULL) then //Unable to allocate memory for a node
Page 15
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
9. Endwhile
10. ptr->LINK = new //change the linked field of last node
11. new->DATA = X //copy the content X into the new node
12. Endif
13. stop
Before inserting:
1000
After Insertion:
New node
1000
Input : HEADER is the pointer to the header node , X is the data of the node to be inserted. KEY
being the data of the key node after which the node has to be inserted.
Output: A single Linked list with a newly inserted node having data X after the node with data
KEY.
Data structure: A single Linked list whose address of the starting node is known from the
HEADER.
Steps:
1. New = GetNode(NODE) //Get a memory block of type NODE and store its
2. pointer in new
Page 16
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
Before inserting:
1000
After Insertion
1000
Steps:
1. ptr = HEADER // pointer to the first node
2. If( ptr = NULL) then //if the list is empty
6. Ptr1= ptr -> LINK //ptr1 is the pointer to the second node, if any
Before Deleting:
1000
After Deleting:
2000
12 3000 15 NULL
2000 3000
Steps:
1. ptr = HEADER // pointer to the first node
2. If( ptr->LINK = NULL) then //move from the header node
9. End While
10. ptr->LINK = NULL //last but one node becomes the last node
11. ReturnNode(ptr) //Deleted node
12. EndIf
Page 19
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
13. Stop
Before Deleting:
1000
After Deleting
1000
5 2000 12 NULL
1000 2000
Input : HEADER is the pointer to the header node. KEY is the data content of the node to be
deleted
Output: A single Linked list except the node with the data content as KEY
Data structure: A single Linked list whose address of the starting node is known from the
HEADER.
Steps:
1. Ptr1 = HEADER // start from the header node
Page 20
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
Before Deleting:
1000
After Deleting
1000
5 3000 15 NULL
1000 3000
Representation of a polynomial
Page 21
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
Polynomial Addition
1. Polynomial Representation:
General form :
Where
ei – Exponent.
Considering the singly linked list representation , a node should have three
fields:
Example:
4 3 10 1 5 0 NULL
Page 22
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
2. Polynomial Addition
4 3 3 1 5 0 NULL
Page 23
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
6 2 9 1 10 0 NULL
4 3 6 2 12 1 15 0 NULL
Steps:
1. ptr = HEADER // pointer to the first node
2. new = GetNode(NODE)
3. If( new = NULL) then
4. Print “memory underflow: No insertion”
Page 24
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
5. Else
6. New -> LLINK = HEADER //newly inserted node points the header
7. HEADER -> RLINK = new // header now points to the new node
8. New -> RLINK = ptr
9. Ptr -> LLINK = new
10. New -> DATA = x // copy the data into new node
11. EndIf
12. Stop
Before inserting:
1000
500
Page 25
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
Output: A double linked list enriched with a node containing data X at the end of the list
Data structure: A Double linked list structure whose pointer to the header node is the HEADER.
Steps:
1. ptr = HEADER // pointer to the first node
2. while ( ptr -> RLINK != NULL ) do // move to the last node
3. ptr = ptr -> RLINK
4. EndWhile
5. new = GetNode(NODE)
6. If( new = NULL) then
7. Print “memory underflow: No insertion”
8. Else
9. New -> LLINK = ptr //change the pointer1
10. ptr -> RLINK = new // change the pointer2
11. New -> RLINK = NULL // make the new node as the last node
12. New -> DATA = x // copy the data into new node
13. EndIf
14. Stop
Before inserting:
1000
After inserting:
1000
Input : X is the data content of the node to be inserted, and KEY the data content of the node
after which the node is to be inserted.
Output: A double linked list enriched with a node containing data X after the node with data
KEY, if KEY is not present in the list then it is inserted at the end
Data structure: A Double linked list structure whose pointer to the header node is the HEADER.
Steps:
1. new = GetNode(NODE)
2. If( new = NULL) then
3. Print “memory underflow: No insertion”
4. Exit
5. Else
6. ptr = HEADER // pointer to the first node
7. while ( ptr ->DATA != KEY ) and ( ptr -> RLINK != NULL ) do // move to the last
node
8. ptr = ptr -> RLINK
9. EndWhile
10. If( ptr -> RLINK = NULL ) then // if the not found in the list
11. Printf “ KEY is not available in the list “
12. Exit
13. Else
14. Ptr 1 = ptr -> RLINK // next node after the KEY node
15. New -> LLINK = ptr //change the pointer
16. New -> RLINK = ptr 1 //change the pointer
17. ptr -> RLINK = new // change the pointer
18. ptr 1-> LLINK = new
19. Ptr = new
20. New -> DATA = X // copy the data into new node
21. EndIf
Page 27
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
22. EndIf
23. Stop
before insertion:
1000
2000 10 3000
1000
Steps:
1. ptr = HEADER ->RLINK // pointer to the first node
2. If( ptr = NULL) then
3. Print “list is empty: No deletion is made”
4. Exit
5. Else
Page 28
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
12. Endif
13. Stop
Steps:
1. ptr = HEADER // pointer to the first node
2. While( ptr -> RLINK ! = NULL) do // move to the last node
3. Ptr = ptr ->RLINK
4. EndWhile
5. If( ptr = HEADER ) then // if the list is empty
6. Printf “ list is empty ;no deletion is made”
7. Exit
8. Else
9. Ptr 1 = ptr ->LLINK //pointer to the last but one node
10. Ptr 1 -> RLINK = NULL // change the pointer
11. RetuenNode( ptr ) //return the node
12. Endif
13. Stop
Input : Doubly linked list with data, KEY the data content of the key node to be deleted
Output: A Doubly linked list without a node having data content KEY
Data structure: A Double linked list structure whose pointer to the header node is the HEADER.
Steps:
1. ptr = HEADER->RLINK //move to the first node
2. if ( ptr = NULL) then
3. printf “ list is empty : no deletion is made”
4. Exit
5. Endif
6. While (ptr ->DATA != KEY) and ( ptr -> RLINK ! = NULL) do // move to the node
7. Ptr = ptr ->RLINK
8. EndWhile
Page 29
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]
Linked list is dynamic data structure, the size of a list can grow or shrink during the
program execution. So, maximum size need not be known in advance.
The linked list does not waste memory.
It is not necessary to specify the size of the list, as in the case of arrays.
Linked list provides the flexibility in allowing the items to be rearranged.
A singly linked list allows traversal of the list in only one direction.
Deleting a node from a list requires keeping track of the previous node (i.e) the node
whose link points to the node to be deleted.
If the link in any node gets corrupted , the remaining nodes of the list become unusable.
Page 30