Docsity

Linked Lists: Insertion, Deletion, and Traversal, Cheat Sheet of Computer Networks

Partial preview of the text

Download Linked Lists: Insertion, Deletion, and Traversal and more Cheat Sheet Computer Networks in PDF only on Docsity!

DATA STRUCTURE & ALGORITHMS

Lecture 09: Data Structures - Linked Lists

Anum Tariq

(Lecturer)

Date: May 05, 2020

INSERTING A NEW NODE

  • (^) Node* InsertNode(int index, double x)
    • (^) Insert a node with data equal to x after the index’th elements.

(i.e., when index = 0, insert the node as the first element;

when index = 1, insert the node after the first element, and so on)

  • (^) If the insertion is successful, return the inserted node.

Otherwise, return NULL. (If index is < 0 or > length of the list, the insertion will fail.)

  • (^) Steps

1. Locate index’

th

element

2. Allocate memory for the new node

3. Point the new node to its successor

4. Point the new node’s predecessor to the new node

INSERTING A NEW NODE Node* List::InsertNode(int index, double x) { if (index < 0) return NULL; int currIndex = 1; Node* currNode = head; while (currNode && index > currIndex) { currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = new Node; newNode->data = x; if (index == 0) { newNode->next = head; head = newNode; } else { newNode->next = currNode->next; currNode->next = newNode; } return newNode; }

Try to locate

index’th node. If it

doesn’t exist,

return NULL.

INSERTING A NEW NODE Node* List::InsertNode(int index, double x) { if (index < 0) return NULL; int currIndex = 1; Node* currNode = head; while (currNode && index > currIndex) { currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = new Node; newNode->data = x; if (index == 0) { newNode->next = head; head = newNode; } else { newNode->next = currNode->next; currNode->next = newNode; } return newNode; }

Create a new node

FINDING A NODE

  • (^) int FindNode(double x)
    • (^) Search for a node with the value equal to x in the list.
    • (^) If such a node is found, return its position. Otherwise, return 0.

int List::FindNode(double x) {

Node* currNode = head;

int currIndex = 1;

while (currNode && currNode->data != x) {

currNode = currNode->next;

currIndex++;

if (currNode) return currIndex;

return 0;

DELETING A NODE

  • int DeleteNode(double x)
    • (^) Delete a node with the value equal to x from the list.
    • (^) If such a node is found, return its position. Otherwise, return 0.
  • (^) Steps
    • (^) Find the desirable node (similar to FindNode)
    • (^) Release the memory occupied by the found node
    • (^) Set the pointer of the predecessor of the found node to the successor of the

found node

  • (^) Like InsertNode, there are two special cases
    • (^) Delete first node
    • (^) Delete the node in middle or at the end of the list

DELETING A NODE int List::DeleteNode(double x) { Node prevNode = NULL; Node currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode) { if (prevNode) { prevNode->next = currNode->next; delete currNode; } else { head = currNode->next; delete currNode; } return currIndex; } return 0; }** prevNode currNode

DELETING A NODE int List::DeleteNode(double x) { Node prevNode = NULL; Node currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode) { if (prevNode) { prevNode->next = currNode->next; delete currNode; } else { head = currNode->next; delete currNode; } return currIndex; } return 0; }** head currNode

USIGN LIST

int main(void)
List list;
list.InsertNode(0, 7.0); // successful
list.InsertNode(1, 5.0); // successful
list.InsertNode(-1, 5.0); // unsuccessful
list.InsertNode(0, 6.0); // successful
list.InsertNode(8, 4.0); // unsuccessful
// print all the elements
list.DisplayList();
if(list.FindNode(5.0) > 0)
cout << "5.0 found" << endl;
else
cout << "5.0 not found" << endl;
if(list.FindNode(4.5) > 0)
cout << "4.5 found" << endl;
else
cout << "4.5 not found" << endl;
list.DeleteNode(7.0);
list.DisplayList();
return 0; }

Number of nodes in the list: 3

5.0 found

4.5 not found

Number of nodes in the list: 2

REFERENCE

1. Data Structures, A Pseudocode Approach with C, Richard F. Gilberg & Behrouz A,

Forouzan,

Chapter 3 Linked Lists

2. Data Structures And Problem Solving Using C++ 2nd Ed - Mark Weiss,

Chapter 7, section 7.6 Sequences and Linked Lists

3. Data Structures by Schaum Series,

Chapter 5 Linked Lists

4. Objects, Abstraction, Data Structures, and Design Using C++, Elliot-Koffman-

mpaul-wolfgang-john-wiley-sons-2006,

Chapter 4 Sequential Container, Section 4.5 Single-Linked Lists and Double-

Linked Lists