Linked List Lecture Notes PDF

Summary

These lecture notes cover the concept of linked lists, focusing on their implementation in C++. Topics include various types of linked lists, operations such as insertion, deletion, and searching, and their applications. The notes also outline visual representations and implementation codes.

Full Transcript

A Pointer to Struct #include int main() { #include cout next == NULL curr = curr->next; break } cur = cur->n...

A Pointer to Struct #include int main() { #include cout next == NULL curr = curr->next; break } cur = cur->next } End Size int size() { int count = 0; Node* curr = head; while(curr != NULL) { count++; curr = curr->next; } return count; } Insertion Insertion operation is used to insert a new node in the linked list. Insertion is of three types. They are: Inserting at first Inserting at last Inserting at mid Insertion at first (Prepend) There are two steps to be followed to insert a node at the first position. They are: Make the next pointer of the new node point towards the first node of the list. Make the head pointer point towards this new node. *Visual Representation of Insertion at First head data next data next data next data next NULL Newnode //Inserting at first void insertFirst(int data){ Node* newNode = new Node; newNode->data = data; newNode->next = NULL; if(head == NULL) { head = newNode; } else { newNode->next = head; head = newNode; } } Insertion at last (Append) Inserting at the last position is a simple process. To insert at last we just simply need to make the next pointer of last node to the new node. *Visual Representation of Insertion at Last head data next data next data next data next NULL newnode //Inserting at Last void insertLast(int data){ Node* newNode = new Node; newNode->data = data; newNode->next = NULL; if(head == NULL) { head = newNode; } else { Node* curr = head; while(curr->next != NULL) curr = curr->next; curr->next = newNode; } } Insertion at position There are several steps to be followed to insert a node at specific position: Traverse the list until we reach the position where the new node is going to be inserted. Update the pointer ‘cur’ to point to the node at the target position. Update the pointer ‘prev’ to point to node located before the node pointed by ‘cur’. Update the next pointer of ‘prev’ to point to ‘newnode’. Update the next pointer of ‘newnode’ to point to ‘cur’. *Visual Representation of Insertion at Position head data next data next NULL data next prev curr data next Newnode Insertion at position void insertAtPos(int data, int pos){ if (pos == 1){ insertFirst(data); return; } else if (pos > size()){ insertLast(data); return; } Node* newNode = new Node; newNode->data = data; newNode->next = NULL; int curPos = 1; Node *curr = head; Node *prev = NULL; while(curPos != pos){ prev=curr; curr = curr->next; curPos++; } prev->next = newNode; newNode->next = curr; } Deletion Deletion is a simple process. At first, we need to find the previous and the next nodes of the target node we want to delete. Then, we update the next pointer of the previous node to point to the next node of the target node we want to delete. *Visual Representation of Deletion head data next data next data NULL next prev curr //Delete void deleteAtPos(int pos){ if (pos > size()) return; //do nothing Node* curr = head; int curPos = 1; if (head !=NULL) { if(pos ==1){ head = curr->next; delete curr; } else{ Node *prev = NULL; while(curPos != pos){ prev=curr; curr = curr->next; curPos++; } prev->next = curr->next; delete curr; } } } Searching To search in a linked list, we need to follow the steps given below: We need a pointer which is assigned with the address of head pointer. Then we start a loop until the last node of the linked list to search. //Searching int search(int value) { int pos = 1; Node* curr = head; while(curr != NULL) { if(curr->data == value) return pos; pos++; curr = curr->next; } return -1; } Uses of Linked List 1. Web-browsers A good example is web-browsers, where it creates a Linked history (traversal of a list) or press back button, the previous node's data is fetched. 2. Image viewer 3. Phonebook In phonebook the names are sorted in ascending order. If we want to add a new contact, then the memory for the new contact is created by linked list.

Use Quizgecko on...
Browser
Browser