Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
329 views

Data Structures and Algorithms: Module - 3 Linked List

The document discusses linked lists, their advantages over arrays for operations like insertion and deletion, and provides examples of how to implement a linked list including creating and traversing a linked list as well as inserting nodes at different positions. It also compares arrays and linked lists, describing when each type of data structure is most suitable.

Uploaded by

bhumika.verma00
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
329 views

Data Structures and Algorithms: Module - 3 Linked List

The document discusses linked lists, their advantages over arrays for operations like insertion and deletion, and provides examples of how to implement a linked list including creating and traversing a linked list as well as inserting nodes at different positions. It also compares arrays and linked lists, describing when each type of data structure is most suitable.

Uploaded by

bhumika.verma00
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Data Structures and

Algorithms

Module -3
Linked List
Dr. R. Jothi, SCOPE
VIT Chennai
Outline
 Linked List data structure
 List over Array
 Applications

Dr. R. Jothi, VIT Chennai


Array Vs. Linked List

2000
4000
2004
2004
2008
2060
2012
3100
2016
2016
Linked List
 A linked list consists of a sequence of elements
(nodes) that are not necessarily physically adjacent
 Each node contains
 A piece of data
 Pointer to next node in the list
 Head node
 Pointer to the first node
 The last node contains NULL link

A B C

Head
Structure of a node in the List
//If data is integer //If data is a structure
struct nd { struct nd {
int data; struct item data;
struct nd *next; struct nd *next;
} node; } node;
Array Vs. Linked List
 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/deleting are easier
 Applications where sequential access is required.
 In situations where the number of elements cannot be predicted
beforehand, so no memory wastage

Dr. R. Jothi, VIT Chennai


Insert in Array at position k
0th 5 5 5
element 0 99 23
Insert 99 99
@3
5@1 0 @2 0 23
0 0 0
0 0 0
5th O(n) time
0 0 0
element

count = 0 count = 1 count = 2


5 5 5
99 99 88
88
@2 23 99 99
23 23 23
0 0 0
0 0 0
Insertion in Linked List
5 99 23

Item to be
tmp 88 inserted

5 99 23

curr
88
Pseudocode for insertion
struct node {
struct item data;
struct nd * next;
};

void insert(struct node *curr)


{
struct node * tmp;

tmp=(struct node *) malloc(sizeof(struct node));


tmp->next=curr->next;
curr->next=tmp;
} Dr. R. Jothi, VIT Chennai
Illustration: Deletion Spring 2012

Item to be deleted

A B C

tmp
curr

A B C
Pseudo-code for deletion Spring 2012

struct node {
struct item data;
struct nd * next;
};

void delete(struct node *curr)


{
struct node * tmp;
tmp=curr->next;
curr->next=tmp->next;
free(tmp);
}
Singly Linked List Dr. R. Jothi, VIT Chennai

 The list we have seen so far is singly linked list, as the nodes
of the has only one link to its successor
 If the nodes have two links one for predecessor and one for
successor, then it is called as doubly linked list. (Discussed in
next class)

head
A B C
Summary of Operations on Linked List
 Create
 head
 Insert
 Delete
 Search
 Traversing the list
Implementation of Singly Linked List : Example
 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;
Dr. R. Jothi, VIT Chennai
Creating a List
• 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
age

Dr. R. Jothi, VIT Chennai


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 formed.
head

A B C

Dr. R. Jothi, VIT Chennai


Function for creating a list
node *create_list()
{
int k, n;
node *p, *head;
printf ("\n How many elements to enter?");
scanf ("%d", &n);
for (k=0; k<n; k++)
{
if (k == 0) {
head = (node *) malloc(sizeof(node));
p = head;
}
else {
p->next = (node *) malloc(sizeof(node));
p = p->next;
}
scanf ("%d %s %d", &p->roll, p->name, &p->age);
}
p->next = NULL;
return (head);
}
Invoking create_list()

 To be called from main() function as:

node *head;
………
head = create_list();

Dr. R. Jothi, VIT Chennai


Traversing the List
 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.

Dr. R. Jothi, VIT Chennai


Function for traversing a list
void traverse (node *head)
{
int count = 1;
node *p;
p = head;
while (p != NULL)
{
printf ("\nNode %d: %d %s %d", count,
p->roll, p->name, p->age);
count++;
p = p->next;
}
printf ("\n");
}

//in main()
traverse(head);
Insertion in Linked List
 At beginning
 At end
 At middle (before/after some node)

Dr. R. Jothi, VIT Chennai


Insertion at beginning
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.

Dr. R. Jothi, VIT Chennai


Insertion at beginning
head A B C

head
A B C

X
Dr. R. Jothi, VIT Chennai
Insertion at End
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.

Dr. R. Jothi, VIT Chennai


Insertion at End
head A B C

head

A B C X

Dr. R. Jothi, VIT Chennai


Insertion at middle
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.

Dr. R. Jothi, VIT Chennai


Insertion in middle
head A B C

Item to be
tmp X inserted

head A B C

curr
X Order?
void insert (node **head)
Spring 2012
{ Programming and Data Structure
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;
}
else
Spring 2012
{ Programming and Data Structure
while ((p != NULL) && (p->roll != rno))
{
q = p;
p = p->next;
} The pointers
q and p
if (p == NULL) /* If rno not present always point
in the list, then add newnode at the end */ { to consecutive
q->next = new; nodes.
new->next = NULL;
}
else if (p->roll == rno)
/* In the middle */
{
q->next = new;
new->next = p;
}
}
}
Typedef: giving meaningful name for a datatype

typedef unsigned int age; typedef long int phone;


phone officephone, personalphone;
age minage, maxage

typedef struct student_info


{
int rollno;
char name[20];
} student;
student s1,s2;
Dr. R. Jothi, VIT Chennai
Deletion
 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.

Dr. R. Jothi, VIT Chennai


Spring 2012
node * delete (node *head) Programming and Data Structure

{
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);
}
else
Spring 2012
{ Programming and Data Structure
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);
}
}
return(head);
}
Exercises

 Implement Stack using linked list


 Implement Queue using linked list

Dr. R. Jothi, VIT Chennai


Stack using Linked List (1)

 Push and Pop always at head

top

Dr. R. Jothi, VIT Chennai


Stack using Linked List (2)
PUSH OPERATION

top

Dr. R. Jothi, VIT Chennai


Stack using Linked List (3)
POP OPERATION

top

Dr. R. Jothi, VIT Chennai


Creating Stack: Linked List Representation
struct lifo void create (stack *top)
{ {
int value; top = NULL;
struct lifo *next;
}; /* top points to NULL,
typedef struct lifo indicating empty
stack; stack */
}
stack *top;

Dr. R. Jothi, VIT Chennai


Push
stack * push (stack *top, int element)
{
stack *new;

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


if (new == NULL)
{
printf (“\n Stack is full”);
exit(-1);
}

new->value = element;
new->next = *top;
top = new;
return top;
}
Dr. R. Jothi, VIT Chennai
Pop
node * pop (stack *top)
{
int t;
stack *p;
if (*top == NULL)
{
printf (“\n Stack is empty”);
exit(-1);
}
else
{
t = top->value;
p = top;
top = top->next;
printf(“%d”,t);
free (p);
return top; Dr. R. Jothi, VIT Chennai
}
Queue using Linked List

 Dequeue always at beginning


 Enqueue always at end Rear

Front DELETION INSERTION


Dr. R. Jothi, VIT Chennai
Queue using Linked List

ENQUEUE

front rear

Dr. R. Jothi, VIT Chennai


Queue using Linked List

DEQUEUE

front rear

Dr. R. Jothi, VIT Chennai


Exercise
 Write pseudocode for enqueue() and dequeue() on Queue using
linked list implementation.

Dr. R. Jothi, VIT Chennai

You might also like