CS2x1 DSA Spring 2023 L5
CS2x1 DSA Spring 2023 L5
Koteswararao Kondepu
[email protected]
Outline
• Exercise on Circular Queue
• Limitations of Stack and Queue
• Linked list data structures
• Linked list operations
• Linked list exceptions
• Linked list implementation
• Linked list applications
Recap Simple Queue: Limitations (1)
Head = Tail = -1
Enqueue(5)
Enqueue(10)
Enqueue(15)
Enqueue(20) Dequeue() Dequeue() Dequeue()
Q[0] Q[1] Q[2] Q[3] Q[0] Q[1] Q[2] Q[3] Q[0] Q[1] Q[2] Q[3] Q[0] Q[1] Q[2] Q[3]
5 10 15 20 10 15 20 15 20 20
5 5 10 5 10 15
Head=0 Head=1 Tail = 3 Head=2 Tail = 3
Tail = 2 Tail = 3 Head=3
Tail = 0 Tail = 3
Tail = 1
Real Queue: Limitations (2)
Head = Tail = -1
Enqueue(5)
Enqueue(10)
Enqueue(15)
Enqueue(20) Dequeue() Dequeue()
Q[0] Q[1] Q[2] Q[3] Q[0] Q[1] Q[2] Q[3] Q[0] Q[1] Q[2] Q[3] Q[0] Q[1] Q[2] Q[3]
5 10 15 20 10 15 20 10 15 20 15 20
5
Head=0 Head=0
Tail = 2 Tail = 3 Head=0 Tail = 3 10 Tail = 2
Tail = 0 Head=0 Tail = 1
Tail = 1
Recap
o Circular Queue (FIFO) Implementation
▪ EnQueue () Circular Queue Data Structures
▪ DeQueue ()
▪ IsQueueFull () EnQueue DeQueue IsQueueFull IsQueueEmpty Traversal
▪ IsQueueEmpty () 2
1
▪ PrintQueue ()
3
Head = Tail = -1 4
Exercise: Circular Queue (1)
The initial Circular Queue configurations are shown in Figure below.
2
1
q 3
p
0
r
4
What is the circular queue content after the following operations:
EnQueue (a), DeQueue(), EnQueue (b), DeQueue (), EnQueue (c),
DeQueue ()
Q[0] Q[1] Q[2] Q[3] Q[4] Q[0] Q[1] Q[2] Q[3] Q[4] Q[0] Q[1] Q[2] Q[3] Q[4]
▪ IsEmpty ()
Dequeuefront
Dequeuerear
node node
struct node {
int data;
struct node *next; data next data next
}
A L I S T
Head
Tail
Linked List: Creation struct node {
int data;
struct node *next;
}
temp
void create(){
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL){
printf("\n Out of Memory Space: \n");
exit(0);
}
printf("\n Enter the data value for the node:");
scanf("%d",&temp->info);
temp->next=NULL;
}
Linked List: Insert at the beginning or Insert at the head
data next struct node {
Steps: newnode int data;
10
(i) Creating a node with data struct node
struct node *newnode = malloc(sizeof(struct node)); *next;
newnode → data = 10; }
newnode → next = NULL; struct node *head = NULL
head
a) Update the next pointer of new node → the current head data next
newnode → next = head newnode 10
b) Update the head pointer to the new node
head = newnode head 15 20 30
Linked List: Example
512 1024 2048 4096
head 10 15 20 30
• head → data = ?
• head → next = ?
head 10 15 20 30
• head → next → next → data = ?
• c → next → data = ?
(ii) List Traversal: Each node present in the list must be visited and display
the data value
head 10 15 20 30
b) tail node pointer points to the new node tail → next = newnode
Linked List: Insert at the given position
Steps:
(i) Creating a node with data data next struct node {
newnode 25 int data;
struct node *newnode = malloc(sizeof(struct node)); struct node *next;
newnode → data = 10; }
newnode → next = NULL;
(ii) Adding a node to at the given position
newnode 25
a) Traversal the list till the position – 1
struct node *position head 10 15 20 30
position = head
i=0
position
while (i<pos)
position = position → next
i++;newnode → next to the position-node → next
b) Point
newnode → next = position → next
c) Point position-node → next to the newnode position → next = newnode
thank you!
email:
[email protected]