Linked List Lecture Notes PDF
Document Details
Uploaded by TruthfulCopernicium
Ibn Sina University for Medical Sciences
Tags
Summary
These lecture notes cover different types of linked lists, including singly and doubly linked lists. They explore the concept of linked lists, their usage in data manipulation and storage, along with insertion (first, last, middle) and deletion operations. The notes provide both descriptions and illustrative diagrams.
Full Transcript
Linked List Types of Linked List There are several types of linked list including: 1. Singly (Linear) Linked List 2. Doubly Linked List Doubly Linked Lists A doubly link list is a list in which each node is linked to both its successor a...
Linked List Types of Linked List There are several types of linked list including: 1. Singly (Linear) Linked List 2. Doubly Linked List Doubly Linked Lists A doubly link list is a list in which each node is linked to both its successor and its predecessor. Head Pointer Node A Node B Node C Node D 10 20 30 10 A graphical view of a doubly linked list Doubly linked list - Motivation Doubly linked lists are useful for playing video and sound files with “rewind” and “instant replay”. They are also useful for other linked data which require “rewind” and “fast forward” of the data Doubly Linked Lists In a Doubly Linked List each item points to both its predecessor and successor next points to the successor prev points to the predecessor Doubly Linked List – Node Definition struct Node{ int data; struct Node* next; struct Node* prev; }; struct Node* head = NULL; Size int size() { int count = 0; Node* curr = head; while(curr != NULL) { count++; curr = curr->next; } return count; } Doubly Linked List – insert at first NULL Head NULL prev data next prevdata next data next prev data next prev data next NULL danext newNode //Inserting at first void insertFirst(int data){ Node* newNode = new Node; newNode->data = data; newNode->next = NULL; newNode->prev = NULL; if(head == NULL) { head = newNode; } else { newNode->next = head; head -> prev = newNode; head = newNode; } } Doubly Linked List – insert at last NULL Head NULL prev data next prevdata next data next prev data next data next danext prev NULL newNode //Inserting at Last void insertLast(int data){ Node* newNode = new Node; newNode->data = data; newNode->next = NULL; newNode->prev = NULL; if(head == NULL) { head = newNode; } else { Node* curr = head; while(curr->next != NULL) curr = curr->next; curr->next = newNode; newNode ->prev = curr; } } Doubly Linked List – insert at middle Head data next prev data next data data next prev prev data next data next prev curr prev 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; newNode->prev = NULL; int curPos = 1; if(head == NULL) { head = newNode; } else { Node *curr = head; Node *prev = NULL; while(curPos != pos){ prev=curr; curr = curr->next; curPos++; } prev->next = newNode; newNode->prev = prev; newNode->next = curr; curr->prev = newNode; } } Doubly Linked List – delete Head data next prev data next datadatanext prev next prev data next data ext prev curr Deletion at position 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; head->prev = NULL; delete curr; } else{ Node *prev = NULL; while(curPos != pos){ prev=curr; curr = curr->next; curPos++; } prev->next = curr->next; if (curr ->next != NULL) curr ->next->prev = prev; delete curr; } } }