0% found this document useful (0 votes)
54 views30 pages

Stacks:: Programming in C++ and Data Structures

1. Stacks are ordered collections where insertion and deletion occur at one end, called the top. Elements are added and removed in Last In First Out order. 2. Stacks have two common representations - arrays and linked lists. Array representation uses Push and Pop operations to add/remove elements from the top. Linked list representation uses a head pointer to track the top element. 3. Common stack operations are Push, which adds an element to the top, and Pop, which removes the top element. Stacks have applications in expression evaluation, recursion, and infix to postfix conversion.

Uploaded by

char les
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views30 pages

Stacks:: Programming in C++ and Data Structures

1. Stacks are ordered collections where insertion and deletion occur at one end, called the top. Elements are added and removed in Last In First Out order. 2. Stacks have two common representations - arrays and linked lists. Array representation uses Push and Pop operations to add/remove elements from the top. Linked list representation uses a head pointer to track the top element. 3. Common stack operations are Push, which adds an element to the top, and Pop, which removes the top element. Stacks have applications in expression evaluation, recursion, and infix to postfix conversion.

Uploaded by

char les
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 30

UNIT - 4 [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

Fig: Schematic Diagram of a Stack.

Linked List Representation:


In several applications, size of the stack may vary during program execution. So single
linked list is convenient to represents any stack. Here, the DATA field is for the ITEM, and
LINK filed is as usual to point to the next item in the list.
STACK_HEAD TOP

ITEMn …. … ITEM2 ITEM1

Page 1
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]

1.1 Basic Operations:


1. Push – Adds or Inserts an element / item at the top of the stack.
2. Pop – Deletes or removes the top element / item from the stack.
3. TOP: Top is a pointer in a stack point the top element on the stack.
1. Push Operation:
 Push inserts an item at the top of the stack.
 After the push, the new item becomes the top.
 We must ensure that there is a space for the new item before insertion.
 If there is not enough space, then the stack is in an overflow state and the item
cannot be added into the stack.
item/
element stack
6
Stack

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.

PUSH (STACK, ITEM, TOP, SIZE)


1. BEGIN
2. If TOP = SIZE then
3. Print “stack is full”
4. Else
5. TOP = TOP +1
6. STACK[TOP]=ITEM (inserting new ITEM in the TOP of the stack)
7. END

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

Fig: Stack Pop Operation.

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.

POP (STACK, TOP, ITEM)


1. BEGIN
2. If TOP = 0 then
3. Print “stack is empty”
4. Else
5. ITEM = A[TOP] (removing an ITEM from the STACK).
6. TOP = TOP - 1
7. return ITEM
8. End

1.2 Applications of stack.


1. Infix to postfix conversion
2. Recursion

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]

In arithmetic operations, the operators symbol is placed in between its two


operands. This is called infix notation.

Example: (A + B) * C

b. Prefix (or) polish notation:


In which the operator symbol is placed before its two operands.

Example: +AB, * EF

c. Postfix (or) polish notation:


The operator symbol is placed after its operands.

Example: AB + , EF *

Evaluation of Postfix expression:


Suppose P is an arithmetic expression written in postfix notation. The following
algorithm can evaluate the postfix expression and gives the result with the help of the stack.
Algorithm:
Step 1: Add a right parenthesis “)” at the end of P. [This acts as an end pointer]
Step 2: Scan P from left to right and repeat step3 and step4 for each element of P until “)” is
encounter.
Step 3: If an operand is encountered, put it on STACK.
Step 4: If an operator is encountered, then
a) Remove the two top element of STACK, where A is the top element and B is the
next-to-top element.
b) Evaluate B @ A.
c) Place the result of step (b) back on to the STACK.
[End of If structure]
[End of Step 2 Loop]
Step 5: Set VALUE equal to the top element on STACK.
Step 6: Exit
Example:
Consider the following arithmetic expression P written in postfix notation:
P : 5, 6, 2, +, *, 12, 4, /, -

Page 4
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]

Execution of the above expression evaluation is as follows


Symbol Scanned STACK
(1) 5 5
(2) 6 5, 6
(3) 2 5, 6, 2
(4) + 5, 8
(5) * 40
(6) 12 40, 12
(7) 4 40, 12, 4
(8) / 40, 3
(9) - 37
Finally the result 37 will be stored in a variable VALUE.

1. Infix to postfix conversion:


Transforming the Infix expressions into Postfix Expressions.

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 )

SYMBOL POSTFIX STACK


( (
( ((
A A ((
- A ((-
( A ((-(
B AB ((-(
+ AB ((-(+
C ABC ((-(+
) ABC+ ((-
) ABC+ - (
* ABC+ - (*
D ABC+ - D (*
) ABC + - D*
$ ABC + - D* $
( ABC + - D* $(
E ABC + - D * E $(
+ ABC + - D * E $(+
F ABC + - D * E F $(+
) ABC + - D * E F+ $
ABC + - 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]

Representation of Queue using Array:


Enqueue (insertion)

(deletion)
Dequeue Front rear
Fig: model of queue

Queue is empty : FRONT = 0 (and/or) REAR = 0


Queue is full : REAR = N , FRONT = 1
Queue contains elements: FRONT < = REAR

2.1 Basic operations on queue:


There are two basic queue operations. They are:

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

Front rear operation front rear

Fig: Queue Enqueue Operation

Algorithm Enqueue:

ENQUEUE(QUEUE, REAR, ITEM)


1. BEGIN
2. If(REAR = N) then
3. Print “ queue is full”
4. Exit

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

front rear operation front rear

fig: Queue Dequeue Operation.

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

2.2 Applications of Queue:


 Reservation Counter:
All the reservation counters where the people collect their tickets on the First
come First served basis.
 Resource sharing in a computer system:
In computer centre , where the resources are limited compared to the demand,
users must sign a waiting register. The user who has been waiting for a terminal
for the longest period of time gets hold of the resource first, and so on.

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

fig: Circular Queue.

Circular Queue is Empty FRONT =0 and REAR = 0

Circular Queue is Full FRONT = (REAR MOD LENGTH) +1.

3.1 Basic operations on Circular queue:

There are two basic Circular queue operations. They are:

1. Enqueue operation
2. Dequeue operation

1. Enqueue Operation:

Page 9
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]

 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.

3 5

front rear

Fig: Circular Queue Enqueue Operation

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

fig: Circular Queue Dequeue Operation.

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 to the next node


data

Link

Fig: node: an element in a linked list.

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.

It can be classified into three groups:

 Singly Linked List


 Doubly Linked List
 Circular linked list

4.1 Singly Linked List:

In a singly linked list each node contains only one link which points to the subsequent
node in the list.

4.1.1 Operations on a Singly Linked List:

The operations possible on a singly linked list are listed below:

 Traversing the list


 Inserting a node into the list
 Deleting a node from the list

Traversing 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

Input: HEADER is the pointer to the header node.


Output: According to the Process( )
Data Structure: A singly Linked list whose address of the starting node is known from the
HEADER.

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

Inserting a node into a single Linked List:

There are various position where a node can be inserted:


1. Inserting at the front (as a first element).
2. Inserting at the End (as a Last element)
3. Inserting at any other position.
Procedure GetNode:
Input: NODE is the type of data for which a memory has to be allocated.
Output: Return a message if the allocations fails else the pointer to the memory block
allocated.

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

7. Ptr1 = ptr //To keep the track of the previous block


8. Ptr = ptr -> LINK //move to the next block.
9. EndWhile
10. If(SizeOf(ptr) = SizeOf(NODE)) //memory block of right size is found.
11. Ptr1 -> LINK = ptr -> LINK
12. Return (ptr)
13. Else
14. Print “the memory block is too large to fit”
15. Return(NULL)
16. Endif
17. Endif
18. Stop

Page 13
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]

1. Inserting a node at the front of a single linked list:

The Algorithm Insertfront-SL :

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

3. Print”memory underflow: No insertion”


4. Exit //Quit the program
5. Else //memory is available and get a node from memory bank

6. New -> LINK = HEADER -> LINK //change of pointer 1

7. New -> DATA = X //copy the X to the newly availed node

8. HEADER -> LINK = new //change of pointer 2


9. EndIf
10. Stop

Before insertion:

header

1000

5 2000 12 3000 15 NULL Page 14

1000 2000 3000


UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]

After insertion:

header new node

500

10 1000 5 2000 12 3000 15 NULL


500 1000 2000 3000

2. Inserting node at the End of a Single Linked List:

The Algorithm InsertEnd-SL :

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

3. Print ”memory insufficient: insertion not possible”


4. Exit //Quit the program
5. Else //move to the end of the given list and then insert

6. ptr =HEADER //start from the HEADER node

7. While(ptr->LINK != NULL) do //move to the end


8. ptr=ptr->LINK //change of pointer to next 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

5 2000 12 3000 15 NULL

1000 2000 3000

After Insertion:

New node

1000

5 2000 12 3000 15 4000 20 NULL

1000 2000 3000 4000


3. Inserting node at the middle of a Single Linked List:

The Algorithm InsertAny-SL :

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]

3. If( new = NULL) then //Unable to allocate memory for a node

4. Print ”memory insufficient: insertion not possible”


5. Exit //Quit the program
6. Else //move to the end of the given list and then insert

7. ptr =HEADER //start from the HEADER node


8. While(ptr->DATA != KEY) and (ptr->LINK != NULL) do //move to the
node having data as KEY
9. ptr=ptr->LINK //change of pointer to next node
10. Endwhile
11. If(ptr->LINK = NULL) then //search fails to find the KEY.
12. Printf “key is not available in the list”
13. Exit
14. Else
15. New->LINK = ptr->LINK //change the pointer 1
16. new->DATA = X //copy the content X into the new node
17. ptr->LINK = new //change the pointer 2
18. EndIf
19. Endif
20. stop

Before inserting:

1000

5 2000 12 3000 15 NULL

1000 2000 3000

After Insertion

1000

5 2000 12 2500 20 3000 15 NULL


Page 17
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]

1000 2000 2500 3000

1. Deleting a node at the front of a single linked list:

The Algorithm DeletingFront-SL :

Input : HEADER is the pointer to the header node.


Output: A single Linked list after eliminating the 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. ptr = HEADER // pointer to the first node
2. If( ptr = NULL) then //if the list is empty

3. Print” the list is empty : no deletion”


4. Exit //Quit the program
5. Else //the list is not empty

6. Ptr1= ptr -> LINK //ptr1 is the pointer to the second node, if any

7. HEADER->LINK = ptr1 //next node becomes the first node


8. ReturnNode(ptr) //Deleted node
9. EndIf
10. Stop

Before Deleting:

1000

5 2000 12 3000 15 NULL


Page 18
1000 2000 3000
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]

After Deleting:

2000

12 3000 15 NULL

2000 3000

2. Deleting a node at the End of a single linked list:

The Algorithm DeletingEnd-SL :

Input : HEADER is the pointer to the header node.


Output: A single Linked list after eliminating the 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. ptr = HEADER // pointer to the first node
2. If( ptr->LINK = NULL) then //move from the header node

3. Print” the list is empty : no deletion”


4. Exit //Quit the program
5. Else //the list is not empty

6. While( ptr -> LINK != NULL) do //go to the last node

7. Ptr1 = ptr // to store the previous pointer

8. Ptr = ptr->LINK // move to the next

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

5 2000 12 3000 15 NULL

1000 2000 3000

After Deleting

1000

5 2000 12 NULL

1000 2000

3. Deleting a node at the Middle of a single linked list:

The Algorithm DeletingMiddle-SL :

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]

2. Ptr = ptr1->LINK //this points first node , if any


3. while( ptr != NULL) do
4. if (ptr -> DATA != KEY ) then //if not found KEY
5. ptr1 = ptr // keep a track of the pointer of the previous node
6. ptr = ptr ->LINK // move to the next
7. Else

8. Ptr1 -> LINK = ptr -> LINK


9. ReturnNode(ptr) //Deleted node
10. Exit
11. EndIf
12. EndWhile
13. If(ptr = NULL) then //node not available
14. EndIf
15. Stop

Before Deleting:

1000

5 2000 12 3000 15 NULL


1000 2000 3000

After Deleting

1000

5 3000 15 NULL

1000 3000

Application of Singly Linked List:

 Representation of a polynomial

Page 21
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]

 Polynomial Addition

1. Polynomial Representation:

An important application of linked lists is to represent polynomials and their


manipulations.

The main advantage of a linked list for polynomial representation is that it


can accommodate a number of polynomials of growing sizes so that their
combined size does not exceed the total memory available.

General form :

P(X) = anxen + an-1xen-1+ …..+ a1xe1

Where

aixei - is a term in the polynomial so that

ai - non – zero Coefficient

ei – Exponent.

COEFF EXP LINK

Considering the singly linked list representation , a node should have three
fields:

COEFF – store the coefficient an

EXP - store the Exponent ei

LINK – store the pointer to the next node

Example:

Singly linked list representation of the polynomial

P(x) = 4x3 +10x + 5 it would be stored like this.

4 3 10 1 5 0 NULL

Page 22
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]

2. Polynomial Addition

In order to add two polynomials, say P and Q to get a resultant polynomial


R. we have to compare their starting at their first node s and moving towards the
end one by one.

Procedure to polynomial Addition:

Step 1: scan P and Q one by one.

Step 2: compare exponent of P node with Q node.

a. If one node has higher exponent than another node higher


exponent then that higher exponent is inserted in the
polynomial R.
And scan the next node in higher exponent node.
b. If exponent are same in both P and Q then add both Coefficient
and store result in the polynomial R.
And scan next node.

Step 3: Repeat same process until one list is finished.

Step 4: add the remaining nodes of another unfinished list into R.

Example: to represent the polynomial Addition:

P(x) = 4x3 +3x + 5

Q(x) = 6x2 +9x + 10

R(x) = P(x) + Q(x)

P(x) = 4x3 +3x + 5:

4 3 3 1 5 0 NULL

Page 23
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]

Q(x) = 6x2 +9x + 10

6 2 9 1 10 0 NULL

R(x) = P(x) + Q(x)

4 3 6 2 12 1 15 0 NULL

Doubly linked list:


A more sophisticated kind of linked list is a doubly linked list or Two Way linked list. Each
node has two links:

1. One points to the previous node

2. One points to the next node.

1. Inserting node at the front of the Doubly Linked List:

Input : X is the data content of the node to be inserted


Output: A double linked list enriched with a node in the front containing data X
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. 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

NULL 5 2000 1000 12 3000 2000 15 NULL

1000 2000 3000


After inserting:

500

NULL 2 1000 500 5 2000 1000 12 3000 2000 15 NULL


L

500 1000 2000 3000

2. Inserting node at the End of the Doubly Linked List:

Input : X is the data content of the node to be inserted

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

NULL 5 2000 1000 12 3000 2000 15 NULL

1000 2000 3000

After inserting:

1000

NULL 5 2000 1000 12 3000 2000 15 4000 3000


Page 2615 NULL
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]

1000 2000 3000 4000

3. Inserting node at the middle of the Doubly Linked List:

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

NULL 5 2000 1000 12 3000 2000 15 NULL

1000 2000 3000

After insertion: 500

2000 10 3000
1000

NULL 5 2000 1000 12 500 500 15 NULL

1000 2000 3000


1. Deleting node at the front of the Doubly Linked List:

Input : Doubly linked list with data


Output: A reduced Doubly linked list
Data structure: A Double linked list structure whose pointer to the header node is the HEADER.

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

6. Ptr 1 = ptr ->RLINK //point to the second node


7. HEADER -> RLINK = ptr 1 // change the pointer
8. If ( ptr 1 != NULL) // if the list contains a node after first node is deleted
9. Ptr 1-> LLINK = HEADER
10. EndIf
11. RetuenNode( ptr ) //return the node

Page 28
UNIT - 4 [PROGRAMMING IN C++ AND DATA STRUCTURES]

12. Endif
13. Stop

2. Deleting node at the End of the Doubly Linked List:

Input : Doubly linked list with data


Output: A reduced Doubly linked 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. 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

3. Deleting node at the Middle of the Doubly Linked List:

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]

9. If( ptr ->DATA = KEY then // if the node is found


10. Ptr 1 = ptr -> LLINK //track the predecessor node
11. Ptr 2 = ptr ->RLINK //track the successor node
12. Ptr 1 -> RLINK = ptr2 // change the pointer
13. If (ptr 2 != NULL ) then // if the deleted node is the last node
14. Ptr2 -> LLINK = ptr 1
15. EndIF
16. RetuenNode( ptr ) //return the node
17. Else
18. Print “ the node does not exist in the given list”
19. Endif
20. Stop

Advantages of Linked list:

 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.

Disadvantages of Linked list:

 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

You might also like