0% found this document useful (0 votes)
23 views22 pages

CS2x1 DSA Spring 2023 L5

The document discusses linked lists, their advantages over arrays, and basic linked list operations like creation, insertion, deletion, and traversal. Linked lists allow dynamic size, avoid swapping elements, and are linear for access but non-linear in memory. Common linked list types include singly linked lists, doubly linked lists, and circular linked lists.

Uploaded by

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

CS2x1 DSA Spring 2023 L5

The document discusses linked lists, their advantages over arrays, and basic linked list operations like creation, insertion, deletion, and traversal. Linked lists allow dynamic size, avoid swapping elements, and are linear for access but non-linear in memory. Common linked list types include singly linked lists, doubly linked lists, and circular linked lists.

Uploaded by

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

CS2x1:Data Structures and Algorithms

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]

(a) a b r (b) a b c (c) a b c


Exercise: Circular Queue (2)
Given a circular queue with size 7. What is the final value at index 3, after the
following code is executed:

[Note: the circular queue array index starts at 0 ]

for (int k = 1; k <= 7; k++){


EnQueue(k);
}
for (int i = 1; i <= 4; i++){
int delete; //to store the dequeued/deleted element
Dequeue();
delete=DeQueue();
EnQueue(delete);
}
(a) 2 (b) 4 (c) 6 (d) 7
DEQUE Double Ended QUEues

o Circular Queue (FIFO) Implementation


▪ Enqueuefront () DEQUE Data Structures
▪ Enqueuerear ()
▪ Dequefront () Enqueuefront Enqueuerare Deueuefront Deueuerare

▪ Dequerear () Enqueuefront Enqueuerear

▪ IsEmpty ()
Dequeuefront
Dequeuerear

Elements can be added/removed at both ends


Recap
Classification of Data Structures
Data Structures

Primitive Data Non-Primitive


Structures Data Structures

Integer Real Character Boolean Linear Data Non-Linear Data


Structures Structures

Arrays Stack Queues Linked List Tress Graphs

Linear Queue Circular Queue


Linked List
Advantages:
(i) Dynamic in Size: Increase or Decrease → maximum size need not
to be known in advance
(ii) No need of swapping if an element is Inserted or Deleted
(iii) Memory assignment during the execution/run time
(iv) Important ******: Linked Lists are linear for accessing, and non-linear
for strong in memory
(v) IS it Flexible in rearranging elements?
LISP: LISt Processor
Linked List 1958

Singly Doubly Circular


Linked List Linked List Linked List
Linked List: Notations

Assumptions: Everyone familiar with how to define structure with pointers

node node
struct node {
int data;
struct node *next; data next data next
}

• Node is a self-referential structure


• Nodes are logically adjacent, physically scattered
• Array is a physically adjacent and is contiguous.
Revisit: structures with pointers
What is the output of the below program?
#include <stdio.h>
int main()
{
int a[5] = {1,2,3,4,5};
int *ptr;
ptr = (int *)(&a+1);
printf("%d %d\n", *(a+1), *(ptr-1));
}
Linked List: Operations
Linked List Data Structures

Create Insert Delete IsListEmpty Traversal


*create();
• at beginning • at beginning • at beginning
• at end • at end • at end
• at the given position • at the given position • at the given position

insert_begin(); delete_begin(); display();


insert_end(); delete_end(); get_begin();
insert_pos(); delete_pos(); get_end();
get_pos();
Linked List: Best practice

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

(ii) Adding a node to an empty linked list

if (head == NULL) newnode 10


head = newnode
head
Linked List: Insert at the beginning or Insert at the head
data next struct node {
Steps:
newnode 10 int data;
(i) Creating a node with data
struct node *next;
struct node *newnode = malloc(sizeof(struct node));
newnode → data = 10;
}
newnode → next = NULL;
(ii) Adding a node to an empty linked list newnode 10
if (head == NULL)
head
head = newnode
(iii) Adding a node to the beginning of a linked list

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 = ?

• head → next → next → next → data =?

• head → next → next → next → next → data = ?


Linked List: traversal or display
Steps:
(i) Check if the linked list empty or not head struct node *head = NULL
if (head == NULL)
printf (“Linked List is Empty\n”);

(ii) List Traversal: Each node present in the list must be visited and display
the data value
head 10 15 20 30

1 struct node *traversal traversal


2 traversal = head;
3 while ( traversal != NULL)
display the element: traversal → data
traversal = traversal →next
Linked List: Insert at the end or Insert at the tail
data next struct node {
Steps: newnode 10 int data;
(i) Creating a node with data struct node *next;
}
struct node *newnode = malloc(sizeof(struct node));
newnode → data = 10;
newnode → next = NULL; data next
newnode 10
(ii) Adding a node to at the end of a linked list
tail = head; head 15 20 30
a) Traversal the list till the tail pointer
struct node *tail
tail
while ( tail → next != NULL)
tail = tail → next

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]

NEXT Class: 28/04/2023

You might also like