Docsity

Data Structures & Algorithms: Linked Lists, Schemes and Mind Maps of Computer Vision

Partial preview of the text

Download Data Structures & Algorithms: Linked Lists and more Schemes and Mind Maps Computer Vision in PDF only on Docsity!

DATA STRUCTURE & ALGORITHMS

Lecture 09: Data Structures - Linked Lists

Anum Tariq

(Lecturer)

Date: May 05, 2020

INTRODUCTION

Linked lists

  • Abstract data type (ADT)
  • (^) Basic operations of linked lists
  • Insert, find, delete, print, etc.

Variations of linked lists

  • Circular linked lists
  • Doubly linked lists

LINKED LISTS

A

B C

Head

A

data pointer

node

  • (^) A linked list is a series of connected nodes

Each node contains at least

A piece of data (any type)

  • (^) Pointer to the next node in the list
  • (^) Head : pointer to the first node
  • (^) The last node points to NULL

VARIATION OF LINKED LISTS

  • (^) Circular linked lists
    • (^) The last node points to the first node of the list
    • (^) How do we know when we have finished traversing the list? (Tip: check if the

pointer of the current node is equal to the head.)

A

Head

B C

ARRAY VERSUS LINKED LISTS

  • (^) Linked lists are more complex to code and manage than arrays, but they have some

distinct advantages.

  • (^) Dynamic : a linked list can easily grow and shrink in size.
    • (^) We don’t need to know how many nodes will be in the list. They are created

in memory as needed.

  • (^) In contrast, the size of a C++ array is fixed at compilation time.
  • (^) Easy and fast insertions and deletions
  • (^) To insert or delete an element in an array, we need to copy to temporary

variables to make room for new elements or close the gap caused by deleted

elements.

  • (^) With a linked list, no need to move other nodes. Only need to reset some

pointers.

LINKED LISTS

  • (^) Because each node of a linked list has two components, we need to declare each node as a class or

struct.

  • (^) The data type of each node depends on the specific application—that is, what kind of data is being

processed.

  • (^) However, the link component of each node is a pointer. The data type of this pointer variable is the

node type itself.

  • (^) For the given linked list, the definition of the node is as follows. (Suppose that the data type is int.)

Linked Lists: Some Properties

  • (^) Suppose that current is a pointer of the same type as the pointer head. Then, the statement:

current = head; copies the value of head into current

Linked Lists: Some Properties

Now consider the statement: current = current->link;

  • (^) This statement copies the value of current->link, which is 2800, into current.
  • (^) Therefore, after this statement executes, current points to the second node in the list. (When working

with linked lists, we typically use these types of statements to advance a pointer to the next node in the

list.)

TRAVERSING A LINKED LIST

  • (^) If we move head to the second node, the first node is lost (unless we save a

pointer to this node). If we keep advancing head to the next node, we will lose

all of the nodes of the list.

Therefore, we always want head to point to the first node. It now follows that

we must traverse the list using another pointer of the same type.

  • (^) Suppose that current is a pointer of the same type as head. The following code

traverses the list:

current = head;

while (current != NULL)

cout << current->info << " ";

current = current->link;

A SIMPLE LINKED LIST CLASS

  • (^) We use two classes: Node and List
  • (^) Declare Node class for the nodes
    • (^) data: double-type data in this example
    • (^) next: a pointer to the next node in the list

class Node {

public:

double data; // data

Node* next; // pointer to next

A SIMPLE LINKED LIST CLASS

  • (^) Operations of List
    • (^) IsEmpty: determine whether or not the list is empty
    • (^) InsertNode: insert a new node at a particular position
    • (^) FindNode: find a node with a given value

DeleteNode: delete a node with a given value

  • (^) DisplayList: print all the nodes in the list

PRINTING ALL ELEMENTS

  • void DisplayList(void)
    • Print the data of all the elements
    • (^) Print the number of the nodes in the list

void List::DisplayList()

{

int num = 0;

Node* currNode = head;

while (currNode != NULL)

{

cout << currNode->data << endl;

currNode = currNode->next;

num++;

}

cout << "Number of nodes in the list: " << num << endl;

}