Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 56

Linked List

Linked List

 Create a structure called a Node.

object next

 The object field will hold the actual list element.


 The next field in the structure will hold the
starting location of the next node.
 Chain the nodes together to form a linked list.
Linked List

 Picture of our list (2, 6, 7, 8, 1) stored as a


linked list:

head

2 6 8 7 1 size=5

current
Linked List

Note some features of the list:


 Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
Linked List

Note some features of the list:


 Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
 The current here is a pointer, not an index.
Linked List

Note some features of the list:


 Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
 The current here is a pointer, not an index.
 The next field in the last node points to nothing.
We will place the memory address NULL which
is guaranteed to be inaccessible.
Linked List
 Actual picture in memory:
1051 6
1052 1063
current 1053 1063
1054 2
head 1055 1051
1056
2 6 8 7 1 1057 7
1058 1060
current 1059
1060 1
1061 0
head 1062 1054
1063 8
1064 1057
1065
New Node

8
AL 9
NODE

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

AL 10
Contd..
struct Node {
    int data;
    struct Node* next;
};
struct Node* head = NULL;
  head =malloc(sizeof(struct Node));

Node *tmp = new Node;


AL 11
Create List

#include <stdio.h>
#include <stdlib.h>

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

// Driver's code
int main()
{
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;

AL 12
allocate Locations to nodes

head = (struct Node*)malloc(sizeof(struct Node));


second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
•Three blocks have been allocated dynamically.
• We have pointers to these three blocks as head,
second and third.
• Data is random because we haven’t assigned
anything yet

AL 13
Assigning Values to List

• assign data in first node


• head->data = 1;
• Link first node with the second node
• head->next = second;
• data has been assigned to the data part of the first
block
• block pointed by the head
• And next pointer of first block points to second.
• So they both are linked.

AL 14
Contd….

• assign data to second node


• second->data = 2;
• Link second node with the third node
• second->next = third;
• data has been assigned to the data part of
the second block
• This block pointed by second).
• And next pointer of the second block points
to the third block.
• So all three blocks are linked.
AL 15
Contd…

• assign data to third node


• third->data = 3;
• third->next = NULL;
• data has been assigned to data part of third
block
• This block pointed by third
• And next pointer of the third block is made
NULL to indicate that the linked list is
terminated here.
AL 16
Algorithm
• Let LIST be a linked list in memory. This algorithm traverses
LIST applying an operation PROCESS to each element of LIST.
The variable PTR points to the node currently being
processed.
1. Set PTR=START [Initialize pointer PTR]
2. Repeat Step 3 and 4 while PTR ≠ NULL.
3. Apply PROCESS TO INFO[PTR].
4. Set PTR=LINK[PTR] [PTR now points to the next node]
[End of Step 2 Loop]
5. Exit.

AL 17
Program

#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node* next;
};
AL 18
Print the list
void printList(struct Node* n)
{
while (n != NULL) {
cout<<n->data;
n = n->next;
}
}

AL 19
Contd..
int main()
{
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
// allocate 3 nodes
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
AL 20
Contd…

head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;

AL 21
Contd..

printList(head);

return 0;
}
OURTPUT:
123

AL 22
Insert New Node At Start

AL 23
1. Insert at the beginning

•Allocate memory for new node


Store data
•Change next of new node to
point to head
•Change head to point to recently
created node
AL 24
Inserting at the Beginning of the
List
Suppose our linked list is not necessarily sorted and
there is no reason to insert a new node in any special
place in the list. Then the easiest place to insert the
node is at the beginning of the list. An algorithm that
does so follows.
INSFIRST(INFO,LINK,START,AVAIL,ITEM)
1.[OVERFLOW?]
If AVAIL=NULL then write OVERFLOW and Exit.
2. [Remove first node from AVAIL list.]
Set NEW=AVAIL and AVAIL=LINK[AVAIL]

AL 25
Contd…

3. Set INFO[NEW]=ITEM
[Copies new data into new node]
4. Set LINK[NEW]=START
[New node now points to the original first
node]
5. Set START=NEW
[Change START so it points to the new node ]
6. Exit.
26
Insert New Node

struct node *newNode;


newNode = malloc(sizeof(struct node));
newNode->data = 4;
newNode->next = head;
head = newNode;

AL 27
Add a node after a given node
Follow the steps to add a node after a given
node:
•Firstly, check if the given previous node is NULL
or not.
•Then, allocate a new node
•Assign the data to the new node
•And then make the next of new node as the next
of previous node.
•Finally, move the next of the previous node as a
new node.
AL 28
Add a node after a given node

AL 29
3. Insert at the Middle

struct node *newNode;


//newNode = new node;

newNode = malloc(sizeof(struct node));


newNode->data = 4;

struct node *temp = head;

AL 30
Contd…

for(int i=2; i < position; i++) {


if(temp->next != NULL) {
temp = temp->next;
}
}
newNode->next = temp->next;
temp->next = newNode;

AL 31
2. Insert at the End

•Allocate memory for new node


•Store data
•Traverse to last node
•Change next of last node to recently
created node

AL 32
Insertion at End

AL 33
Program
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = 4;
newNode->next = NULL;
struct node *temp = head;
while(temp->next != NULL)
{
temp = temp->next;}
temp->next = newNode;
AL 34
Delete from a Linked List

•Beginning
•End
•Middle

AL 35
Deletion Operation on the Linked
List

Suppose that the node with info 34


is to be deleted from the list.
AL 36
Contd..

p->link=p->link->link;
•It is clear that the node with info 34
is removed from the list.
•However, the memory is still
occupied by this node and this
memory is inaccessible.
•How can we delete?
AL 37
•To deallocate the memory, we need
a pointer to this node.
q=p->link;
p->link=q->link;
delete q;

AL 38
39
Deleting a node following a given
node
Let LIST be a linked list in memory. Suppose we are given the
location LOC of a node N in LIST. Furthermore suppose we
are given the location LOCP of the node preceding N or
when N is the first node we are given LOCP=NULL. The
following algorithm deletes N from the list.
DEL(INFO, LINK, START, AVAIL, LOC, LOCP)
1. If LOCP=NULL
Set START=LINK[START]
else
Set LINK[LOCP]=LINK[LOC]. [End of if structure]
2. Exit.
AL 40
Deleting the node with a Given ITEM of
Information
FINDB(INFO, LINK ,START, ITEM, LOC, LOCP)
The algorithm finds the location LOC of the first node N
which contains ITEM and the location LOCP of the node
preceding N. If ITEM does not appear in the list then the
procedure sets LOC=NULL and if ITEM appears as the first
node then it sets LOPC=NULL.
1. [List Empty?] If START=NULL then
Set LOC=NULL and LOCP=NULL and return.
[End of If Structure]

AL 41
Contd..
2. [ITEM is first node?] If INFO[START=ITEM] then
Set LOC=START and LOCP=NULL and return
[End of If Structure]
3. Set SAVE=START and PTR=LINK[START] [Initialize
Pointers.]
4. Repeat Step 5 and 6 while PTR ≠ NULL
5. If INFO[PTR]=ITEM then
Set LOC=PTR and LOCP=SAVE and return.
[End of If structure]
6. Set SAVE=PTR and PTR=LINK[PTR] [Update Pointers]

AL 42
Contd..

7. Set LOC=NULL.
8. Return.

AL 43
Searching a Linked List
• Let LIST be a linked list in memory and suppose a
specific ITEM of information is given.
• Two searching algorithms for finding the location
LOC of the node where ITEM first appears in
LIST.
• The first algorithm does not assume that LIST is
sorted.
• If ITEM is actually a key value and we are searching
through a file for the record containing ITEM then
ITEM can appear only once in LIST.
AL 44
List is unsorted

SEARCH (INFO, LINK, START, ITEM, LOC)


1. Set PTR= START
2. Repeat Step 3 while PTR ≠ NULL
3. If ITEM = INFO[PTR] then
Set LOC = PTR and Exit.
Else
Set PTR=LINK[PTR] [PTR now points to the next node]
[End of if structure]
[End of Step 2 loop]

AL 45
4. [Search is unsuccessful.]
Set LOC = NULL.
5. Exit.

AL 46
LIST is sorted

SRCHSL(INFO, LINK, START, ITEM, LOC)


1. Set PTR = START
2. Repeat Step 3 while PTR ≠ NULL
3. If ITEM <INFO[PTR] then
Set PTR = LINK[PTR] [PTR now points to the
next node].
Else if ITEM = INFO[PTR] then
Set LOC= PTR and Exit [Search is successful.]
AL 47
Contd..

Else
Set LOC =NULL and Exit. [ITEM now exceeds
INFO[PTR]]
[End of If structure]
[End of Step 2 loop]
4. Set LOC = NULL.
5. Exit.

AL 48
1) Delete from Beginning

To delete a node from linked list, we need to do

following steps.

1) Find previous node of the node to be deleted.

2) Change the next of previous node.

3) Free memory for the node to be deleted.

AL 49
Delete First Node

AL 50
Program
void deleteNode(struct node **head, int key)
{
struct node *temp;
if(*head->data == key)
{
temp = *head; //backup the head to free its
memory
*head = (*head)->next;
free(temp);
}
}

AL 51
2. For other nodes

AL 52
Code
struct node *current = *head;
while(current->next != NULL)
{
if(current->next->data == key)
{ temp = current->next;
current->next = current->next->next;
free(temp);
break; }
else
current = current->next;}

53
Delete Last Node

AL 54
Program
if(head != NULL) {

if(head->next == NULL)
{
head = NULL;
}

AL 55
Contd..

else {
while(temp->next->next != NULL)
temp = temp->next;
Node* lastNode = temp->next;
temp->next = NULL;
free(lastNode); }

AL 56

You might also like