Module 2 - Linear Data Structure (1)
Module 2 - Linear Data Structure (1)
• String Reversal
• Balanced Parenthesis
• UNDO/REDO
• Infix, Prefix and Postfix Evaluation
• Depth First Search (Module 5)
String Reversal
Balanced Parenthesis
Undo/ Redo
Infix/Postfix/Prefix Evaluation
Infix Evaluation
Infix to Postfix
Infix to Prefix
Postfix to Infix
Prefix to Infix
Infix to Prefix
1.(A+B)*(C/D)
2. A*(B*C+D*E)+F
3. (A+B)*C+(D-E)/F+G
4. + + A * B C D
Postfix to Infix
1. abc+de/*-
2. a = 2 3 + 4 5 6 – – * //find ‘a’
value
3. b = 5 3 * 9 + 6 / 8 4 / + //find ‘b’
value
Prefix to Infix
1. a = - + 5 / 10 5 5 //find ‘a’
value
2. b = + 9 * 3 / 8 4 //find ‘b’
value
3. b = 5 3 * 9 + 6 / 8 4 / + //find ‘b’
value
Queues
Stack vs Queue
Stack vs Queue
Queue
35
Queues
• Queue: a collection whose elements are added
at one end (the rear or tail of the queue) and
removed from the other end (the front or
head of the queue)
• A queue is a FIFO (first in, first out) data
structure
• Any waiting line is a queue:
– The check-out line at a grocery store
– The cars at a stop light
– An assembly line
6-36
Conceptual View of a Queue
Adding an element
Front of queue
6-37
Conceptual View of a Queue
Removing an element
Element is removed
from the front of the
queue
6-38
Operations on a Queue
Operation Description
Front BBB 2
Rear
Rear AAA 1
Front
09/10/08 40
Algorithms for Insert and Delete Operations in Linear Queue
filled..*/
4.09/10/08
Return. 41
Queues
class Queue {
int front, rear, capacity;
int queue[];
Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
} 6-42
Queues
// function to insert an element // at the
rear of the queue
static void queueEnqueue(int data)
{ // check queue is full or not
if (capacity == rear) {
System.out.printf("\nQueue is
full\n");
return; }
// insert element at the rear
else {
queue[rear] = data; 6-43
// function to delete an element
// from the front of the queue
static void queueDequeue()
{
// if queue is empty
if (front == rear) {
System.out.printf("\nQueue is
empty\n");
return;
}
09/10/08 Shaily Kabir, Dept. of CSE, DU
// shift all the elements from index 2 till rear
// to the right by one
else {
for (int i = 0; i < rear - 1; i++)
{
queue[i] = queue[i + 1];
}
// store 0 at rear indicating there's no
element
if (rear < capacity)
queue[rear] = 0;
// print queue elements
static void queueDisplay()
{
int i;
if (front == rear) {
System.out.printf("\nQueue
is Empty\n"); return; }
// traverse front to rear and print elements
for (i = front; i < rear; i++) {
System.out.printf(" %d <-- ",
queue[i]);
} return; }
// print front of queue
static void queueFront()
{
if (front == rear) {
System.out.printf("\nQueue
is Empty\n");
return;
}
System.out.printf("\nFront
Element is: %d", queue[front]);
return;
}}
public class StaticQueueinjava {
// Driver code
public static void main(String[] args)
{
// Create a queue of capacity 4
Queue q = new Queue(4);
10 50 30 40 20
1 2 3 4 5 6 7
(2) Delete Front Element. Now Rear = 5 and Front = 2
x 50 30 40 20
1 2 3 4 5 6 7
(3) Delete Front Element. Now Rear = 5 and Front = 3
x x 30 40 20
1 2 3 4 5 6 7
(4) Insert 60. Now Rear = 6 and Front = 3
x x x x 20 60 70
1 2 3 4 5 6 7
09/10/08 50
Linked lists
Example:
struct node {
int data; // data
struct node *next; // pointer to next element
};
Simple Linked List
START
1 2 3 4 5 6 7 X
• In the above linked list, every node contains two parts - one
integer and the other a pointer to the next node.
• The data part of the node which contains data may include a
simple data type, an array or a structure.
• The pointer part of the node contains a pointer to the next node
(or address of the next node in sequence).
• The last node will have no next node connected to it, so it will
store a NULL value.
• A linked list is defined by the pointer pointing to the first node,
e.g START
Linked List Operations
1. Create a node and linked list
2. Traversal, e.g. display all elements
3. Search node
4. Insert node
at beginning, at end, after/before a node
5. Delete node
at beginning, at end, after/before a node
6. Sort
How to create nodes
A node is a structure data type, can be created in
two methods, statically and dynamically.
• Static method
– use array of structures
– declared as globally outside functions
– declared locally within a function
1 2 3 4 5 6 7 X
Example:
struct node {
int data;
struct node *next;
};
Traversal Singly Linked Lists
ALGORITHM FOR TRAVERSING A LINKED LIST
Step 1:
[INITIALIZE] SET PTR = START
Step 2:
Repeat Steps 3 and 4 while PTR != NULL
Step 3:
Apply Process to PTR->DATA
Step 4:
SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
Searching a Linked List
ALGORITHM TO SEARCH A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Step 3 while PTR != NULL
Step 3: IF VAL = PTR->DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR->NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL // not found
Step 5: EXIT // found, output POS
START
9 1 7 3 4 2 6 5 X
START
See example
Inserting a Node at the End
1 7 3 4 2 6 5 X
START, PTR
1 7 3 4 2 6 5 9 X
START
START
7 3 4 2 6 5 X
START
Deleting the Last Node
1 7 3 4 2 6 5 X
1 7 3 4 2 6 X 5 X
PREPTR PTR
START
1 7 3 4 2 6 5 X
1 7 3 4 2 6 5 X
1 7 3 4 2 6 5 X
START
1 7 3 4 6 5 X
Deleting the Node After a Given Node
ALGORITHM TO DELETE THE NODE AFTER A GIVEN NODE FROM THE
LINKED LIST
START
1 2 3 4 5 6 7
Circular Linked List
Algorithm to insert a new node in the beginning of
circular the linked list
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5
PTR
START
9 1 7 3 4 2 6 5
START
Circular Linked List
Algorithm to insert a new node at the end of the circular
linked list
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5 9
START PTR
Circular Linked List
Algorithm to insert a new node after a node that has
value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Step 8 and 9 while PREPTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5
PTR
START
7 3 4 2 6 5
START
Circular Linked List
Algorithm to delete the last node of the circular linked
list
1 7 3 4 2 6 5
1 7 3 4 2 6 5
PREPTR
PTR
START
1 7 3 4 2 6
START
Circular Linked List
Algorithm to delete the node after a given node from the circular linked list
1 7 3 4 2 6 5
1 7 3 4 2 6 5
1 7 3 4 6 5
START
Doubly Linked List
A doubly linked list or a two way linked list is a more complex
type of linked list which contains a pointer to the next as well
as previous node in the sequence. Therefore, it consists of
three parts and not just two. The three parts are data, a
pointer to the next node and a pointer to the previous node
START
X 3 4 5 X
1 2
Doubly Linked List
• In C language, the structure of a doubly linked list is given as,
struct node
{ struct node *prev;
int data;
struct node *next;
};
• The prev field of the first node and the next field of the last
node will contain NULL. The prev field is used to store the
address of the preceding node. This would enable to
traverse the list in the backward direction as well.
Doubly Linked List
Algorithm to insert a new node in the beginning of the
doubly linked list
X 9 1 7 3 4 2 X
START
Doubly Linked List
Algorithm to insert a new node after a node that has
value NUM
X 1 7 3 4 2 X
START, PTR
X 1 7 3 4 2 X
START PTR
X 1 7 3 9 4 2 X
START
Doubly Linked List
Algorithm to delete the first node from the doubly linked
list
X 1 7 3 4 2 X
START, PTR
7 3 4 2 X
Doubly Linked List
Algorithm to delete the last node of the doubly linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 7
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 and 5 while PTR->NEXT != NULL
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET PTR->PREV->NEXT = NULL
Step 6: FREE PTR
Step 7: EXIT
X 1 3 5 7 8 9 X
1
START, PTR
X 1 3 5 7 8 9 X
1
START PTR
X 1 3 5 7 8 X
START
Doubly Linked List
Algorithm to delete the node after a given node from the
doubly linked list
X 1 3 4 7 8 9 X
1
START, PTR
X 1 3 4 7 8 9 X
1
START
PTR
X 1 3 4 8 9 X
START
Circular Doubly Linked List
• A circular doubly linked list or a circular two way linked list is a
more complex type of linked list which contains a pointer to
the next as well as previous node in the sequence.
START
1 1 2 3 4
Circular Doubly Linked List
Algorithm to insert a new node in the beginning of the circular doubly linked list
1 7 3 4 2
START
9 1 7 3 4 2
START
Circular Doubly Linked List
Algorithm to insert a new node at the end of the circular
doubly linked list
1 7 3 4 2
START, PTR
1 7 3 4 2
PTR
START 9
1 7 3 9 4 2
START
Circular Doubly Linked List
Algorithm to delete the first node from the circular doubly
linked list
3 5 7 8 9
1
START
Circular Doubly Linked List
Algorithm to delete the last node of the circular doubly
linked list
X 1 3 5 7 8
START
Circular Doubly Linked List
Algorithm to delete the node after a given node from the
circular doubly linked list
1 3 5 7 8 9
1
START
1 3 5 7 8 9
1
START PTR
1 3 4 8 9
START
Circular Doubly Linked List
A header linked list is a special type of linked list which
contains a header node at the beginning of the list. So, in a
header linked list START will not point to the first node of the
list but START will contain the address of the header node.
There are basically two variants of a header linked list-
Header Node
1 2 3 4 5 6 X
START
Header Node
1 2 3 4 5 6
START
Circular Doubly Linked List
Algorithm to traverse a Circular Header Linked List