Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of...

40
Review on linked lists
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    0

Transcript of Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of...

Page 1: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Review on linked lists

Page 2: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

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: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Motivation list using static array

int myArray[1000]; int n;

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

list using dynamic arrayint* myArray; int n;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: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Array naturally represents a (ordered) list,

the link is implicit, consecutive and contiguous!

Now the link is explicit, any places!

20 45 75 85

Data

Link

20

45

75

85Data Link

20 45 75 85

Data Link

0 1 2 array

linked list

Page 5: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

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 6: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

The list can grow and shrink

Linked Lists: Basic Ideas

20 45 75 85

20 45

addEnd(75), addEnd(85)

deleteEnd(85), deleteHead(20), deleteHead(45)

75

Page 7: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Original linked list of integers:

Insertion (in the middle):

Deletion (in the middle)

Linked Lists: Operations

20 45 75 85

20 45 75 85

20 45 75 85

60

old value

deleted item

Page 8: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

struct Node{ int data;Node* next;

};

We can also:

typedef Node* NodePtr;

Definition of linked list type:

Page 9: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

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: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

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: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Representing and accessing linked lists

We define a pointer

Node* 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: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

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 (not good!)

or using a function to return a new pointer value

It is roughly the same as for an array!!!

Page 13: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Implementation of an (Unsorted) Linked List

Page 14: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Start the first node from scratch

Node* newPtr;

newPtr = new Node;newPtr->data = 20;newPtr->next = NULL; head = newPtr;

Head

newPtr

20

Headhead = NULL;

Page 15: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Inserting a Node at the Beginning

newPtr = new Node;

newPtr->data = 13;

newPtr->next = Head;

head = newPtr;

Head

newPtr

13

20

Page 16: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Keep going …

Head

newPtr

50 40 13 20

Page 17: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

void addHead(Node*& head, int newdata){

Node* newPtr = new Node;

newPtr->data = newdata;newPtr->next = Head;head = newPtr;

}

Adding an element to the head:

Call by reference, scaring!!!

NodePtr&

Page 18: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Node* addHead(Node* head, int newdata){

Node* newPtr = new Node;

newPtr->data = newdata;newPtr->next = Head;

return newPtr;}

Also written (more functionally, better!) as:

Compare it with ‘addHead’ with a dynamic array implementation

Page 19: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

(to delete)

Deleting the Head Node

Node* p;

p = head;

head = head->next;

delete p;

head

p

50 40 13 20

Page 20: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

void deleteHead(Node*& head){

if(head != NULL){

NodePtr p = head;

head = head->next;

delete p;

}

}

Node* deleteHead(Node* head){

if(head != NULL){

NodePtr p = head;

head = head->next;

delete p;

}

return head;

}

As a function:

Page 21: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Displaying a Linked List

p = head;

p = p->next;

20 45head

p

20 45head

p

Page 22: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

void displayList(Node* head){

NodePtr p;

p = head;

while(p != NULL){

cout << p->data << endl;

p = p->next;

} }

A linked list is displayed by walking through its nodes one by one,

and displaying their data fields (similar to an array!).

void displayArray(int data[], int size) { int n=0; while ( n<size ) {

cout << data[i] << endl; n++;

}

}

For an array:

Page 23: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

//return the pointer of the node that has data=item//return NULL if item does not exist

Node* searchNode(Node* head, int item){NodePtr p = head;

NodePtr result = NULL;bool found=false;while((p != NULL) && (!found)){

if(p->data == item) {found = true;result = p;}

p = p->next;}return result;

}

Searching for a node (look at array searching first!)

Page 24: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

void main() { const int size=8; int data[size] = { 10, 7, 9, 1, 17, 30, 5, 6 };

int value; cout << "Enter search element: ";

cin >> value; int n=0; int position=-1; bool found=false; while ( (n<size) && (!found) ) {

if(data[n] == value) { found=true; position=n;}

n++;}if(position==-1) cout << "Not found!!\n";else cout << "Found at: " << position << endl;

}

Remember array searching algorithm:

It is essentially the same!

Page 25: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Variations of linked lists

Unsorted linked lists

Sorted linked lists

Circular linked lists Doubly linked lists …

Page 26: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Further considerations for the unsorted lists:

Physical copy of list for operators like ‘delection’ and ‘addHead’

‘delete’ should be understood as a decomposition into a sub-list …

Page 27: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Node* deleteHead(Node* head){

// physically copy head into a new one, newhead

// so to keep the original list intact!

Node* newhead …

if(newhead != NULL){

Node* p = newhead;

newhead = newhead->next;

delete p;

}

return newhead;

}

Page 28: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

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 29: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

void addEnd(NodePtr& head, int newdata){NodePtr newPtr = new Node;newPtr->data = newdata;newPtr->next = NULL;

NodePtr last = head;if(last != NULL){ // general 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->nextLink a new object to empty list

Page 30: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

NodePtr addEnd(NodePtr head, int newdata){NodePtr newPtr = new Node;newPtr->data = newdata;newPtr->next = NULL;

NodePtr last = head;if(last != NULL){ // general non-empty list case

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

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

head = newPtr;

return head;}

Add to the end as a function:

Page 31: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Implementation of a

Sorted Linked List

Page 32: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Inserting a Node

Head

cur

20

33

45 75

prev

...

newPtr

1. (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 33: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

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 such that

prev->data < newValue <= cur->data

Page 34: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

prev = NULL;

cur = head;

found=false;

while( (cur!=NULL) && (!found) ) {

if (newValue > cur->data) {

prev=cur;

cur=cur->next;

}

else found = true;

}

Prev is necessary as we can’t go back!

It’s a kind of search algo,

Page 35: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

prev = NULL;

cur = head;

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

prev=cur;

cur=cur->next;

}

Logical AND (&&) is short-circuited, sequential, i.e. if the first part is false, the second part will not be executed.

Finally, it is equivalent to:

Page 36: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

//insert item into linked list according to ascending orderNode* insertNode(Node* head, int item){

NodePtr newp, cur, pre; newp = new Node;newp->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 listnewp->next = head;head = newp;

} else {pre->next = newp;new->next = cur;

}

return head;}

If the position happens to be the head

General case

Page 37: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

// not recommended void type functionvoid insertNode(NodePtr& head, int item){

NodePtr newp, cur, pre; newp = new Node;newp->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 listnewp->next = head;head = newp;

} else {pre->next = newp;new->next = cur;

}}

Page 38: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

(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 39: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

Node* deleteNode(Node* head, int item){NodePtr prev=NULL, cur = head;while( (cur!=NULL) && (item > cur->data)){

prev = cur;cur = cur->next;

}

if ( cur!==NULL && cur->data==item) {

if(cur==head)head = head->next;

elseprev->next = cur->next;

delete cur; }

return head;}

Delete an element in a sorted linked list:

If the element is at the head

General case

We can delete only if the element is present!

If (cur==NULL || cur->data!=item) Item is not in the list!

Get the location

Page 40: Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.

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

prev = cur;cur = cur->next;

}

if ( cur!==NULL && cur->data==item) {

if(cur==Head)Head = Head->next;

elseprev->next = cur->next;

delete cur; }}

// in a void function, not recommended

If the element is at the head

General case

We can delete only if the element is present!

If (cur==NULL || cur->data!=item) Item is not in the list!

Get the location