Linked List Data Structure Lecture Notes PDF

Summary

These lecture notes cover the concepts and implementation details of linked lists in C++. The document includes explanations of different types of linked lists, common operations like insertion, deletion, and traversal, and visual representations to aid comprehension. It also emphasizes the advantages and disadvantages of linked lists in comparison to arrays.

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 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 insertFirst(data): void insertFirst(int data){ Begin create a new node Node* newNode = new Node; newnode -> data = data newNode->data = data; newnode -> next = null newNode->next = NULL; if head == null if(head == NULL) { head = newnode head = newNode; else } newnode->next = head else { head = newnode newNode->next = head; End 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 insertLast(data): void insertLast(int data){ Begin create a new node Node* newNode = new Node; newnode -> data = data newNode->data = data; newnode -> next = null newNode->next = NULL; if the list is empty, then if(head == NULL) { head = newnode head = newNode; else } curr = head else { forever: Node* curr = head; if curr -> next == null while(curr->next != NULL) break curr = curr->next; curr= curr->Next curr->next = newNode; curr - > next = newnode } End } 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 //Inserting at position insertAtPos(data, pos): void insertAtPos(int data, int pos){ Begin create a new node Node* newNode = new Node; newnode -> data = data newNode->data = data; newnode -> next = null newNode->next = NULL; curPos = 1 int curPos = 1; if the list is empty, then if(head == NULL) { head = newnode head = newNode; else } curr = head else { forever: Node *curr = head; if curPos == pos || curr -> next == null Node *prev; break while(curPos != pos && curr->next != NULL){ prev = curr prev=curr; curr= curr->Next curr = curr->next; curPos++ curPos++; prev -> next = newnode } newnode -> next = curr prev->next = newNode; End 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 deleteAtPos(pos): void deleteAtPos(int pos){ Begin Node* curr = head; curr = head int curPos = 1; curPos = 1 if (head !=NULL) if head is null, then { it is Underflow and return if(pos ==1){ else if pos ==1 head = curr->next; head = curr->Next delete curr; delete curr } else else{ forever: Node *prev; if curPos == pos || curr -> next == null while(curPos != pos && curr->next != NULL){ break prev=curr; prev = curr curr = curr->next; curr= curr->Next curPos++; curPos++ } prev->Next = curr->Next prev->next = curr->next; Delete curr delete curr; End } } } 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 search(value): int search(int value) Begin { pos = 1 int pos = 1; cur = head Node* curr = head; forever: while(curr != NULL) { if cur ->next == null if(curr->data == value) break return pos; cur = cur->next pos++; if curr -> data == value curr = curr->next; return pos } pos++ End 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. Advantages Linked lists are a dynamic data structure, allocating the needed memory while the program is running. Insertion and deletion node operations are easily implemented in a linked list. They can reduce access time and may expand in real time without memory overhead. Disadvantages They have a quite difficult to use more memory due to pointers requiring extra storage space. Nodes in a linked list must be read in order from the beginning as linked lists are inherently sequential access. Nodes may be stored in an incontiguous manner; increasing the time required to access individual elements within the list. Difficulties arise in linked lists when it comes to reverse traversing. For instance, singly linked lists are cumbersome to navigate backwards and while doubly linked lists are somewhat easier to read, memory is wasted in allocating space for a back pointer.

Use Quizgecko on...
Browser
Browser