Professional Documents
Culture Documents
DSA Lec 3
DSA Lec 3
Linked List
object next
head
2 6 8 7 1 size=5
current
Linked List
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));
#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
AL 13
Assigning Values to List
AL 14
Contd….
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
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
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
AL 30
Contd…
AL 31
2. Insert at the End
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
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
AL 45
4. [Search is unsuccessful.]
Set LOC = NULL.
5. Exit.
AL 46
LIST is sorted
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
following steps.
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