programming linked lists. comp104 linked lists / slide 2 motivation * a “list” is a useful...

31
Programming Linked Lists

Post on 18-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

Programming

Linked Lists

Page 2: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 2

Motivation

A “List” is a useful structure to hold a collection of data. Currently, we use arrays for lists

Examples:

List of ten students marksint studentMarks[10];

List of temperatures for the last two weeksdouble temperature[14];

Page 3: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 3

Motivation

list using static array int myArray[10];

We have to decide in advance the size of the array (list) list using dynamic array

int n, *myArray;cin >> n;myArray = new int[n];

We allocate an array (list) of any specified size while theprogram is running

linked-list (dynamic size)size = ??The list is dynamic. It can grow and shrink to any size.

Page 4: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 4

Linked Lists: Basic Idea

A linked list is an ordered collection of data Each element of the linked list has

Some data A link to the next element

The link is used to chain the data

Example: A linked list of integers:

20 45 75 85

Data Link

Page 5: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 5

The list can grow and shrink

Linked Lists: Basic Ideas

20 45 75 85

20 45

add(75), add(85)

delete(85), delete(45), delete(20)

75

Page 6: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 6

Original linked list of integers:

Insertion:

Deletion

Linked Lists: Operations

20 45 75 85

20 45 75 85

20 45 75 85

60

old value

deleted item

Page 7: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

#include <iostream>using namespace std;

struct Node{ int data;Node *next;

};

typedef Node* NodePtr;

Page 8: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 8

typedef typedef allows you to make new shortcuts to existing types

typedef int WAH;WAH k; // same as: int k;

typedef int* WAHPTR;WAHPTR p; // same as: int *p;

typedef Node* NodePtr;NodePtr Head; // same as: Node* Head;

Page 9: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 9

Linked List Structure Node : Data + Link

Definitionstruct Node {

int data; //contains useful information

Node* next; //points to next element or NULL

};

Create a NodeNode* p;

p = new Node; //points to newly allocated memory

Delete a Nodedelete p;

Page 10: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 10

Linked List Structures

Access fields in a node(*p).data; //access the data field

(*p).next; //access the pointer field

Or it can be accessed this way

p->data //access the data field

p->next //access the pointer field

Page 11: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 11

Representing and accessing Linked Lists

We define a pointer

NodePtr Head;

that points to the first node of the linked list. When the linked list is empty then Head is NULL.

20 45 75 85Head

Page 12: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 12

Passing a Linked List to a Function

When passing a linked list to a function it should suffice to pass the value of Head. Using the value of Head the function can access the entire list.

Problem: If a function changes the beginning of a list by inserting or deleting a node, then Head will no longer point to the beginning of the list.

Solution: When passing Head always pass it by reference (or using a function to return a new pointer value)

20 45 75 85Head

Page 13: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 13

Manipulation of a Unsorted Linked List

Page 14: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 14 Start the first node from scratch

NodePtr newPtr;

newPtr = new Node;

newPtr->data = 20;

newPtr->next = NULL;

Head = newPtr;

Head

newPtr

20

HeadHead = NULL;

Page 15: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 15

Inserting a Node at the Beginning

newPtr = new Node;

newPtr->data = 13;

newPtr->next = Head;

Head = newPtr;

Head

newPtr

13

20

Page 16: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 16

Keep going …

Head

newPtr

50 40 13 20

Page 17: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

void addHead(NodePtr& Head, int newdata){

NodePtr newPtr = new Node;

newPtr->data = newdata;

newPtr->next = Head;

Head = newPtr;

}

Adding an element to the head:

Page 18: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 18

(to delete)

Deleting the Head Node

NodePtr cur;

cur = Head;

Head = Head->next;

delete cur;

Head

cur

50 40 13 20

Page 19: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

void delHead(NodePtr& Head){

if(Head != NULL){

NodePtr cur = Head;

Head = Head->next;

delete cur;

}

}

Page 20: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 20

Displaying a Linked List

cur = Head;

cur = cur->next;

20 45Head

cur

20 45Head

cur

Page 21: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 21

A linked list is displayed by walking through its nodes one by one, and displaying their data fields.

void DisplayList(NodePtr Head){

NodePtr cur;

cur = Head;

while(cur != NULL){

cout << cur->data << endl;

cur = cur->next;

} }

Page 22: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

//return the pointer of the node has data=item//return NULL if item does not existNodePtr searchNode(NodePtr Head, int item){NodePtr Cur = Head;

NodePtr Result = NULL;while(Cur != NULL){

if(Cur->data == item)Result = Cur;

Cur = Cur->next;}return Result;

}

Searching for a node

Page 23: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 23

Original linked list of integers:

Add to the end (insert at the end):

More operation: adding to the end

50 40 13 20

50 40 13 20 60

Last element

The key is how to locate the last element or node of the list!

Page 24: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

void addEnd(NodePtr& Head, int newdata){

NodePtr last = Head;NodePtr newPtr = new Node;newPtr->data = newdata;newPtr->next = NULL;if(Head != NULL){ // non-empty list case

while(last->next != NULL) last = last->next;

last->next = newPtr;}else // deal with the case of empty list

Head = newPtr;}

Add to the end:

Link new object to last->next

Link a new object to empty list

Page 25: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 25

Manipulation of a

Sorted Linked List

Page 26: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 26

Inserting a Node

Head

cur

20

33

45 75

prev

...

newPtr

Page 27: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 27

To insert a new node into the list1. (a) Create a new node using:

NodePtr newPtr = new node;(b) Fill in the data field correctly.

2. Find “prev” and “cur” such that the new node should be inserted between *prev and *cur.

3. Connect the new node to the list by using:(a) newPtr->next = cur;

(b) prev->next = newPtr;

Page 28: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 28

Finding prev and cur Suppose that we want to insert or delete a node with

data value newValue. Then the following code successfully finds prev and cur.

prev = NULL;

cur = Head;

while(cur!=NULL && newValue > cur->data){

prev = cur;

cur = cur->next;

}

Page 29: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

//insert item into linked list in ascending ordervoid insertNode(NodePtr& Head, int item){

NodePtr New, Cur, Pre; New = new Node;New->data = item;Pre = NULL;Cur = Head;while(Cur != NULL && item > Cur->data){

Pre = Cur;Cur = Cur->next;

}if(Pre == NULL){ //insert to head of linked list

New->next = Head;Head = New;

} else {Pre->next = New;New->next = Cur;

}}

Page 30: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

COMP104 Linked Lists / Slide 30

(to delete)

Deleting a Node To delete a node from the list

1. Locate the node to be deleted(a) cur points to the node.

(b) prev points to its predecessor

2. Disconnect node from list using: prev->next = cur->next;

3. Return deleted node to system: delete cur;

Head

cur

20 45 75 85

prev

...

Page 31: Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use

void deleteNode(NodePtr& Head, int item){NodePtr prev, cur = Head;while(cur!=NULL && item > cur->data){

prev = cur;cur = cur->next;

}

if(cur==NULL || cur->data!=item){cout << "Delete error: " << item

<< " not in list!" << endl;return;

}if(cur==Head)

Head = Head->next;else

prev->next = cur->next;

delete cur;}

Delete an element in a sorted linked list: