0% found this document useful (0 votes)
28 views24 pages

Lecture 2 - Linked List

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

Lecture 2 - Linked List

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

Linked List

G Bharathi Mohan
Assistant Professor- CSE -AIE
Linked List
• The linked list is a linear data structure that contains a sequence of
elements such that each element links to its next element in the sequence.
• Each element in a linked list is called "Node".
• Single Linked List
• Single linked list is a sequence of elements in which every element has link
to its next element in the sequence.
• In a single linked list, the address of the first node is always stored in a
reference node known as "front" (Some times it is also known as "head").
• Always next part (reference part) of the last node must be NULL.
Linked List
Operations on Single Linked List

• The following operations are performed on a Single Linked List


• Insertion
• Deletion
• Display
• First, perform the following steps before implementing actual operations.
• Step 1 - Include all the header files which are used in the program.
• Step 2 - Declare all the user defined functions.
• Step 3 - Define a Node structure with two members data and next
• Step 4 - Define a Node pointer 'head' and set it to NULL.
• Step 5 - Implement the main method by displaying operations menu and make
suitable function calls in the main method to perform user selected operation.
Insertion
• In a single linked list, the insertion operation can be performed in three
ways.
• Inserting At Beginning of the list
• Inserting At End of the list
• Inserting At Specific location in the list
Inserting At Beginning of the list

1. Input DATA to be inserted


2. Create a NewNode
3. NewNode → DATA = DATA
4. If (START equal to NULL)
(a) NewNode → Link = NULL
5. Else
(a) NewNode → Link = START
6. START = NewNode
7. Exit
code
struct node {
int info; void AddAtBeg(int data)
struct node *link; {
}*start; struct node *tmp; tmp=(struct
node*)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp; }
/*En
code
• void Create_List(int data) • /*If list is empty*/
• { struct node *q,*tmp; • start=tmp;
• //Dynamic memory is been • else {
allocated for a node • /*Element inserted at the end*/
• tmp=(struct node*)malloc(sizeof • q=start;
(struct node));
• while(q->link!=NULL) q=q->link;
• tmp->info=data;
• q->link=tmp; }
• tmp->link=NULL;
• if(start==NULL)
Insert a Node at any specified position

• We will simply traverse till (position -1)th node and add


the newnode just after that node. Let us take an example.
• Suppose the list is 1 → 2 → 3, and we have to insert 4 at the 2nd position.
• Now, we will traverse (position -1) = (2-1) = 1 node and after traversing 1
node, we will be standing at 1.
• Now we will make 4 → next = 1 → next as we have to insert it after 1, and
finally, 1 → next = 4 to link the nodes.
• By doing the above steps, 4 will be added at that specific position, and our
resultant linked list will be: 1 → 4 → 2 → 3.
Insert a Node at any specified position

• If the position where we are asked to insert pos is smaller than 1 or greater
than the size of the list, it is invalid, and we will return.
• Else, we will make variable curr and make it point to the head of the list.
• Now we will run a for loop using curr to reach to the node at (pos-
1)th position:
• The for loop will be: for(int i=1;inext;}
• After the termination of the above loop, curr will be standing at the (position –
1)th node.
• As explained above, we will simply make newnode → next = curr → next and curr
→ next = newnode.
• If the pos was equal to 1, we will make the head point
to newnode as newnode will become the first node of the list.
Insertion – pos
void insertAtPosition(struct struct Node*
Node* head, int pos, int data) temp=getNode(data);
{ temp->next=curr->next;
if (pos < 1 || pos > size + 1) curr->next=temp;
printf( "Invalid position!\n" ); if(pos=1)
else { head=temp;
struct Node *curr=head; size++;
for(int i=1;i<pos-1;i+ }}
+) curr="curr-">next;
Insert a Node at the end

1. Input DATA to be inserted


2. Create a NewNode
3. NewNode → DATA = DATA
4. NewNode → Next = NULL
8. If (SATRT equal to NULL)
(a) START = NewNode
9. Else
(a) TEMP = START
(b) While (TEMP → Next not equal to NULL)
(i) TEMP = TEMP → Next
10. TEMP → Next = NewNode
11. Exit
Insertion and Deletion
Deletion

• In a single linked list, the • Deleting from Beginning of the list


• We can use the following steps to delete a node
deletion operation can be from beginning of the single linked list...
performed in three ways. • Step 1 - Check whether list is Empty (head == NULL)
• Step 2 - If it is Empty then, display 'List is Empty!!!
• Deleting from Beginning of the Deletion is not possible' and terminate the
list function.
• Step 3 - If it is Not Empty then, define a Node
• Deleting from End of the list pointer 'temp' and initialize with head.
• Step 4 - Check whether list is having only one node
• Deleting a Specific Node (temp → next == NULL)
• Step 5 - If it is TRUE then set head = NULL and
delete temp (Setting Empty list conditions)
• Step 6 - If it is FALSE then set head = temp → next,
and delete temp.
• Suppose START is the first position in linked list. Let DATA be the element to be
• deleted. TEMP, HOLD is a temporary pointer to hold the node address.
• 1. Input the DATA to be deleted

Deletion
• 2. if ((START → DATA) is equal to DATA)
• (a) TEMP = START
• (b) START = START → Next
• (c) Set free the node TEMP, which is deleted
• (d) Exit
Deletion
• 4. while ((HOLD → Next → Next) • 5. if ((HOLD → next → DATA) ==
not equal to NULL)) DATA)
• (a) if ((HOLD → NEXT → DATA) • (a) TEMP = HOLD → Next
equal to DATA)
• (b) Set free the node TEMP,
• (i) TEMP = HOLD → Next
which is deleted
• (ii) HOLD → Next = TEMP → Next
• (c) HOLD → Next = NULL
• (iii) Set free the node TEMP, which
is deleted • (d) Exit
• (iv) Exit • 6. Disply “DATA not found”
• (b) HOLD = HOLD → Next • 7. Exit
ALGORITHM FOR DISPLAY ALL NODES

• Suppose START is the address of the first node in the linked list. Following algorithm
• will visit all nodes from the START node to the end.
1. If (START is equal to NULL)
(a) Display “The list is Empty”
(b) Exit
2. Initialize TEMP = START
3. Repeat the step 4 and 5 until (TEMP == NULL )
4. Display “TEMP → DATA”
5. TEMP = TEMP → Next
6. Exit
ALGORITHM FOR SEARCHING A NODE
• Suppose START is the address of the first node in the linked list and DATA is the information to be
searched. After searching, if the DATA is found, POS will contain the corresponding position in the list.
1. Input the DATA to be searched
2. Initialize TEMP = START; POS =1;
3. Repeat the step 4, 5 and 6 until (TEMP is equal to NULL)
4. If (TEMP → DATA is equal to DATA)
(a) Display “The data is found at POS”
(b) Exit
5. TEMP = TEMP → Next
6. POS = POS+1
7. If (TEMP is equal to NULL)
(a) Display “The data is not found in the list”
8. Exit
Create linked list from a given array
• int main() { • struct Node {
int arr[] = { 1, 2, int data;
3, 4, 5 }, n = 5; Node* next;
Node* root = };
createLinkedList(arr, struct Node* newNode(int
n); data) {
Node* node = new Node;
printLinkedList(root); node->data = data;
return 0; node->next = NULL;
} return node;
}
Create linked list from a given array

void insertNewNode(Node** root, int data) {


void printLinkedList(Node*
Node* node = newNode(data);
root) {
Node* ptr;
if (*root == NULL) { while (root != NULL) {
*root = node;
}
cout << root->data
else {
<< " -> ";
ptr = *root; root = root->next;
while (ptr->next != NULL) {
ptr = ptr->next; }
}
cout << "NULL" << endl;
ptr->next = node;
}} }
Create linked list from a given array
Node* createLinkedList(int
arr[], int n) {
Node *root = NULL;
for (int i = 0; i < n; i+
+) {
insertNewNode(&root,
arr[i]);
}
return root;
•}
• Approach: To create an array of linked lists below are the main
requirements:
Array • An array of pointers.
implementation • For keeping the track of the above-created array of pointers then another
pointer is needed that points to the first pointer of the array. This pointer is
of list called pointer to pointer.

You might also like