data structure: linked list

30
Dr. Engr. Sami ur Rahman Assistant Professor Department of Computer Science University of Malakand Data Structure: Linked List

Upload: zlhna

Post on 15-Feb-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Data Structure: Linked List. Data Structure: Linked List. Data Structure: Linked List. Data Structure: Linked List. Data Structure: Linked List. Data Structure: Linked List. Data Structure: Linked List. Data Structure: Linked List. Data Structure: Linked List. Data Structure: Linked List. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Data Structure: Linked List

Dr. Engr. Sami ur RahmanAssistant Professor

Department of Computer ScienceUniversity of Malakand

Data Structure:Linked List

Page 2: Data Structure: Linked List

Dr. Engr. Sami ur RahmanAssistant Professor

Department of Computer ScienceUniversity of Malakand

Data Structure:Linked List

Page 3: Data Structure: Linked List

Dr. Engr. Sami ur RahmanAssistant Professor

Department of Computer ScienceUniversity of Malakand

Data Structure:Linked List

Page 4: Data Structure: Linked List

Dr. Engr. Sami ur RahmanAssistant Professor

Department of Computer ScienceUniversity of Malakand

Data Structure:Linked List

Page 5: Data Structure: Linked List

Dr. Engr. Sami ur RahmanAssistant Professor

Department of Computer ScienceUniversity of Malakand

Data Structure:Linked List

Page 6: Data Structure: Linked List

Dr. Engr. Sami ur RahmanAssistant Professor

Department of Computer ScienceUniversity of Malakand

Data Structure:Linked List

Page 7: Data Structure: Linked List

Dr. Engr. Sami ur RahmanAssistant Professor

Department of Computer ScienceUniversity of Malakand

Data Structure:Linked List

Page 8: Data Structure: Linked List

Dr. Engr. Sami ur RahmanAssistant Professor

Department of Computer ScienceUniversity of Malakand

Data Structure:Linked List

Page 9: Data Structure: Linked List

Dr. Engr. Sami ur RahmanAssistant Professor

Department of Computer ScienceUniversity of Malakand

Data Structure:Linked List

Page 10: Data Structure: Linked List

Dr. Engr. Sami ur RahmanAssistant Professor

Department of Computer ScienceUniversity of Malakand

Data Structure:Linked List

Page 11: Data Structure: Linked List

List Using Linked Memory

Various cells of memory are not allocated consecutively in memory.

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 11

Page 12: Data Structure: Linked List

List Using Linked Memory

Various cells of memory are not allocated consecutively in memory.

Not enough to store the elements of the list.

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 12

Page 13: Data Structure: Linked List

List Using Linked Memory

Various cells of memory are not allocated consecutively in memory.

Not enough to store the elements of the list. With arrays, the second element was right next

to the first element.

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 13

Page 14: Data Structure: Linked List

List Using Linked Memory

Various cells of memory are not allocated consecutively in memory.

Not enough to store the elements of the list. With arrays, the second element was right next

to the first element. Now the first element must explicitly tell us

where to look for the second element.

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 14

Page 15: Data Structure: Linked List

List Using Linked Memory

Various cells of memory are not allocated consecutively in memory.

Not enough to store the elements of the list. With arrays, the second element was right next

to the first element. Now the first element must explicitly tell us

where to look for the second element. Do this by holding the memory address of the

second element

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 15

Page 16: Data Structure: Linked List

Linked List

Create a structure called a Node.

object next

The object field will hold the actual list element. The next field in the structure will hold the

starting location of the next node. Chain the nodes together to form a linked list.

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 16

Page 17: Data Structure: Linked List

Linked List

Picture of our list (2, 6, 7, 8, 1) stored as a linked list:

2 6 8 7 1

head

current

size=5

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 17

Page 18: Data Structure: Linked List

Linked List

Note some features of the list: Need a head to point to the first node of the list.

Otherwise we won’t know where the start of the list is.

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 18

Page 19: Data Structure: Linked List

Linked List

Note some features of the list: Need a head to point to the first node of the list.

Otherwise we won’t know where the start of the list is.

The current here is a pointer, not an index.

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 19

Page 20: Data Structure: Linked List

Linked List

Note some features of the list: Need a head to point to the first node of the list.

Otherwise we won’t know where the start of the list is.

The current here is a pointer, not an index. The next field in the last node points to nothing.

We will place the memory address NULL which is guaranteed to be inaccessible.

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 20

Page 21: Data Structure: Linked List

Linked List : Actual picture in memory:

1051

1052

1055

1059

1060

1061

1062

1063

1064

1056

1057

1058

1053

1054 2

6

8

7

1

1051

1063

1057

1060

0

head 1054

1063current

2 6 8 7 1

head

current

1065

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 21

Page 22: Data Structure: Linked List

Linked List Operations

add(9): Create a new node in memory to hold ‘9’

Node* newNode = new Node(9);

9newNode

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 22

Page 23: Data Structure: Linked List

Linked List Operations

add(9): Create a new node in memory to hold ‘9’

Node* newNode = new Node(9);

Link the new node into the list

9newNode

2 6 8 7 1

head

current

size=5 6

9

newNode

1

3

2

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 23

Page 24: Data Structure: Linked List

C++ Code for Linked List

The Node class

class Node {public:

int get() { return object; };void set(int object) { this->object = object; };

Node *getNext() { return nextNode; };void setNext(Node *nextNode)

{ this->nextNode = nextNode; };private:

int object;Node *nextNode;

};

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 24

Page 25: Data Structure: Linked List

#include <stdlib.h>#include "Node.cpp"

class List {public:

// ConstructorList() {

headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;

};

C++ Code for Linked List

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 25

Page 26: Data Structure: Linked List

C++ Code for Linked List

void add(int addObject) {Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){

newNode->setNext(currentNode->getNext());

currentNode->setNext(newNode); lastCurrentNode = currentNode; currentNode = newNode;

} else {

newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;

currentNode = newNode; } size++; };University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 26

Page 27: Data Structure: Linked List

Building a Linked List

List.add(8); list.add(7); list.add(1);

2 6 7 1headNode

currentNode

size=5

lastcurrentNode

8

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 27

Page 28: Data Structure: Linked List

C++ Code for Linked List

int get() { if (currentNode != NULL) return currentNode->get();

};

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 28

Page 29: Data Structure: Linked List

C++ Code for Linked Listbool next() {

if (currentNode == NULL) return false;

lastCurrentNode = currentNode;currentNode = currentNode->getNext();if (currentNode == NULL || size == 0)

return false;else

return true;};

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 29

Page 30: Data Structure: Linked List

Questions

University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 30