0% found this document useful (0 votes)
6 views

Lecture 3

Uploaded by

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

Lecture 3

Uploaded by

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

Linked List

05/11/24 Programming and Data Structure 1


Introduction
• A linked list is a data structure which can change
during execution.
– Successive elements are connected by pointers.
– Last element points to NULL.
– It can grow or shrink in size during execution of a
program.
– It can be made just as long as required.
– It does not waste memory space.
head

A B C

05/11/24 Programming and Data Structure 2


Contd.
• Keeping track of a linked list:
– Must know the pointer to the first element of the
list (called start, head, etc.).

• Linked lists provide flexibility in allowing the


items to be rearranged efficiently.
– Traverse a list
– Insert an element.
– Delete an element.
05/11/24 Programming and Data Structure 3
Traversing a linked List
Algorithm
PRINT(INFO,LINK,START)
This procedure prints the information at each node of the list
1.Set PTR:=START
2.Repeat Steps 3 and 4 while PTR≠NULL:
3. WRITE: INFO[PTR].
4. Set PTR:=LINK[PTR]. [Update pointer]
5.[End of Step 2 loop]
6.Return

05/11/24 Programming and Data Structure 4


Count Elements
Procedure: COUNT(INFO,LINK,START,NUM)
1.Set NUM:=0 [initialize counter]
2.Set PTR:=START [initialize pointer]
3.Repeat Steps 4 and 5 while PTR≠NULL:
4. Set NUM:=NUM+1 (increase NUM by 1)
5. Set PTR:=LINK[PTR]. [Update pointer]
6.[End of Step 3 loop]
7.Return

05/11/24 Programming and Data Structure 5


Example
• Hospital ward with 12 beds, 5 occupied. We
want an alphabetical listing of patients. Listing
may be given by pointer field(next).
Bed No. Patient Next ptr
#
1 Kundan 7
2
3 Debesh 1
4 Mani
Start
5 Alok 3
6
7 Lila 4

05/11/24 Programming and Data Structure 6


Searching in Linked List
Procedure SEARCH(INFO,LINK,START,ITEM,LOC)
1.Set PTR:=START
2.Repeat Step 3 while PTR≠NULL:
3. If ITEM=INFO[PTR], then
4. Set LOC:=PTR and Exit
5. Else PTR:=LINK[PTR]. [Update pointer]
[End of If]
[End of Step 2 loop]
6. Set LOC:=NULL [Unsuccessful]
7. Exit

05/11/24 Programming and Data Structure 7


Illustration: Insertion
A B C

Item to be
tmp X inserted

A B C

curr
X

05/11/24 Programming and Data Structure 8


Pseudo-code for insertion
typedef struct nd {
struct item data;
struct nd * next;
} node;

void insert(node *curr)


{
node * tmp;

tmp=(node *) malloc(sizeof(node));
tmp->next=curr->next;
curr->next=tmp;
}
05/11/24 Programming and Data Structure 9
Position of insertion
 Inserting at the beginning -> Time complexity: O(1)

 Inserting in between -> Time complexity: O(n)

 Inserting at the end -> Time complexity: O(n)

 Inserting after a given Node -> Time complexity: O(1)

05/11/24 Programming and Data Structure 10


Illustration: Deletion
Item to be deleted

A B C

tmp
curr

A B C

05/11/24 Programming and Data Structure 11


Pseudo-code for deletion
typedef struct nd {
struct item data;
struct nd * next;
} node;

void delete(node *curr)


{
node * tmp;
tmp=curr->next;
curr->next=tmp->next;
free(tmp);
}

05/11/24 Programming and Data Structure 12


In essence ...
• For insertion:
– A record is created holding the new item.
– The next pointer of the new record is set to link it to
the item which is to follow it in the list.
– The next pointer of the item which is to precede it
must be modified to point to the new item.
• For deletion:
– The next pointer of the item immediately preceding
the one to be deleted is altered, and made to point
to the item following the deleted item.

05/11/24 Programming and Data Structure 13


Position of node to be deleted
• Case 1: Deleting the first node.
• Case 2: Deleting the node at the index.
• Case 3: Deleting the last node.
• Case 4: Deleting the first node with a given
value.

05/11/24 Programming and Data Structure 14


Array versus Linked Lists
• Arrays are suitable for:
– Inserting/deleting an element at the end.
– Randomly accessing any element.
– Searching the list for a particular value.
• Linked lists are suitable for:
– Inserting an element.
– Deleting an element.
– Applications where sequential access is required.
– In situations where the number of elements cannot
be predicted beforehand.

05/11/24 Programming and Data Structure 15


Basic Operations on a List
• Creating a list
• Traversing the list
• Inserting an item in the list
• Deleting an item from the list
• Concatenating two lists into one

05/11/24 Programming and Data Structure 16


Conceptual Idea

Insert
List
implementation
Delete
and the
related functions
Traverse

05/11/24 Programming and Data Structure 17


Example: Working with linked list
• Consider the structure of a node as follows:

struct stud {
int roll;
char name[25];
int age;
struct stud *next;
};

/* A user-defined data type called “node” */


typedef struct stud node;
node *head;

05/11/24 Programming and Data Structure 18


How to begin?
• To start with, we have to create a node (the
first node), and make head point to it.
head = (node *)
malloc(sizeof(node));
head
roll

name next
age

05/11/24 Programming and Data Structure 19


Contd.
• If there are n number of nodes in the initial
linked list:
– Allocate n records, one by one.
– Read in the fields of the records.
– Modify the links of the records so that the chain is
head formed.

A B C

05/11/24 Programming and Data Structure 20


Create node
void create()
{ struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL) {
head = temp;
}
else{ struct node* ptr = head;
while(ptr->next!=NULL)
{
ptr = ptr->next;
}
ptr->next = temp; //inserting at end of List
}}
05/11/24 Programming and Data Structure 21
Traversing the List

05/11/24 Programming and Data Structure 22


What is to be done?
• Once the linked list has been constructed and
head points to the first node of the list,
– Follow the pointers.
– Display the contents of the nodes as they are
traversed.
– Stop when the next pointer points to NULL.

05/11/24 Programming and Data Structure 23


Display node
void display()
{ if(head==NULL)
{
printf("Linked List is Empty\n");
return;
}
printf("LinkedList: ");
struct node* ptr = head;
while(ptr!=NULL) // start from first node
{
printf("%d ",ptr->data);
ptr = ptr->next;
}
printf("\n");
}

05/11/24 Programming and Data Structure 24


Inserting a Node in a List

05/11/24 Programming and Data Structure 25


How to do?
• The problem is to insert a node before a
specified node.
– Specified means some value is given for the node
(called key).
– In this example, we consider it to be roll.
• Convention followed:
– If the value of roll is given as negative, the node
will be inserted at the end of the list.

05/11/24 Programming and Data Structure 26


Contd.
• When a node is added at the beginning,
– Only one next pointer needs to be modified.
• head is made to point to the new node.
• New node points to the previously first element.
• When a node is added at the end,
– Two next pointers need to be modified.
• Last node now points to the new node.
• New node points to NULL.
• When a node is added in the middle,
– Two next pointers need to be modified.
• Previous node now points to the new node.
• New node points to the next node.

05/11/24 Programming and Data Structure 27


void insert (node **head)
{
int k = 0, rno;
node *p, *q, *new;

new = (node *) malloc(sizeof(node));

printf ("\nData to be inserted: ");


scanf ("%d %s %d", &new->roll, new->name, &new->age);
printf ("\nInsert before roll (-ve for end):");
scanf ("%d", &rno);

p = *head;

if (p->roll == rno) /* At the beginning */


{
new->next = p;
*head = new;
}

05/11/24 Programming and Data Structure 28


else
{
while ((p != NULL) && (p->roll != rno))

{
q = p;
p = p->next;
} The pointers
q and p
if (p == NULL) /* At the end */ always point
{ to consecutive
q->next = new; nodes.
new->next = NULL;
}
else if (p->roll == rno)
/* In the middle */
{
q->next = new;
new->next = p;
}
}
}
05/11/24 Programming and Data Structure 29
Deleting a node from the list

05/11/24 Programming and Data Structure 30


What is to be done?
• Here also we are required to delete a
specified node.
– Say, the node whose roll field is given.
• Here also three conditions arise:
– Deleting the first node.
– Deleting the last node.
– Deleting an intermediate node.

05/11/24 Programming and Data Structure 31


void delete (node **head)
{
int rno;
node *p, *q;

printf ("\nDelete for roll :");


scanf ("%d", &rno);

p = *head;
if (p->roll == rno)
/* Delete the first element */
{
*head = p->next;
free (p);
}

05/11/24 Programming and Data Structure 32


else
{
while ((p != NULL) && (p->roll != rno))

{
q = p;
p = p->next;
}

if (p == NULL) /* Element not found */


printf ("\nNo match :: deletion failed");

else if (p->roll == rno)


/* Delete any other element */
{
q->next = p->next;
free (p);
}
}
}

05/11/24 Programming and Data Structure 33

You might also like