linked lists. comp104 lecture 33 / slide 2 linked lists: basic idea * data may be stored...
Post on 21-Dec-2015
215 Views
Preview:
TRANSCRIPT
COMP104 Lecture 33 / Slide 2
Linked Lists: Basic Idea
Data may be stored consecutively in a linked list. Each element of the linked list
Stores one piece of data Points to the next element or NULL.
Linked lists provide natural implementation.
A linked list of integers:
20 45 75 85
COMP104 Lecture 33 / Slide 3
Original linked list of integers:
Insertion by pointers:
Deletion by pointers:
Linked Lists: Operations
20 45 75 85
20 45 75 85
20 45 75 85
60
old value
deleted item
COMP104 Lecture 33 / Slide 4
Pointer Based Linked Lists Linked lists store data in increasing order:
Definitions:1. Defining a node (recursive datatype - use itself to define
itself!) class node {
public:int data;node* next;
}; // end classtypedef node* ptrType; // ptrType is a pointer to a node
20 45 75 85
COMP104 Lecture 33 / Slide 5
typedef typedef allows you to make new shortcuts to
existing types
typedef int FunnyInt;
FunnyInt k; // same as: int k;
typedef int* IntPtr;
IntPtr p;// same as: int *p;
p = new node;
*p = 100;
100p
COMP104 Lecture 33 / Slide 6
Pointer Based Linked Lists Definitions (cont’d):
2. To create/delete node
P = new node;delete P;
3. The fields in P can be accessed using:P->data = 100; and P->next = NULL;
Alternatively, we could also write:(*P).data = 100; and (*P).next =
NULL;
100p
COMP104 Lecture 33 / Slide 7
Accessing Linked Lists
We define a pointer
ptrType Head;
that points to the first node of the linked list. When the linked list is empty (empty list) then Head is NULL.
20 45 75 85Head
Head
COMP104 Lecture 33 / Slide 8
Traversing a Linked List
A linked list is displayed by visiting its nodes one by one, and displaying their data fields.
void DisplayList(ptrType Head) {ptrType current = Head;while(current != NULL){
cout << current->data << endl;current = current->next;
} }
COMP104 Lecture 33 / Slide 9
Traversing a Linked List
Alternatively, we can use for loop to traverse a linked list:
void DisplayList(ptrType Head){
ptrType cur;
for (cur = Head; cur != NULL; cur = cur->next) {
cout << cur->data << endl;
}
}
COMP104 Lecture 33 / Slide 10
Displaying a Linked List
current = Head;
current = current->next;
20 45Head
current
20 45Head
current
COMP104 Lecture 33 / Slide 11
(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 to be deleted.
(b) prev points to cur’s 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
...
COMP104 Lecture 33 / Slide 12
(to delete)
Deleting the Head Node
When the node to be deleted is the first node, there is no predecessor node.
Use:Head = Head->next;
instead of: prev->next = cur->next;
Head
cur
20 45 75 85 ...
COMP104 Lecture 33 / Slide 13
Inserting a Node To insert a new node into the list
1. (a) Create a new node using:
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 (the node being pointed at by prev) and *cur.
3. Connect the new node to the list by using:
(a) newPtr->next = cur;
(b) prev->next = newPtr;
COMP104 Lecture 33 / Slide 14
Inserting a Node
Head20 45 75 ...
3(b) prev->next = newPtr;
curprev 2. find prev and cur33
newPtr
1. newPtr = new node
3(a) newPtr->next = cur;
COMP104 Lecture 33 / Slide 15
Inserting a Node at the Beginning
When the new node is to be inserted at the beginning of the list there is no predecessor node. Use:
newPtr->next = Head; Head = newPtr;
instead of: newPtr->next = cur; prev->next = newPtr;
Head
newPtr
13
20 45 75 ...
cur
newPtr->next = Head;Head = newPtr;
COMP104 Lecture 33 / Slide 16
Finding prev and cur In previous slides we assumed that prev and cur were
known. How did we find them? 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;
}
COMP104 Lecture 33 / Slide 17
Finding prev and cur Insertion Example with newValue=60:
...Head
20 45 75
prev = NULL;cur = Head;while(cur!=NULL && newValue > cur->data){
prev = cur;cur = cur->next;
}
curprev curprev curprev
COMP104 Lecture 33 / Slide 18
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.
20 45 75 85Head
COMP104 Lecture 33 / Slide 19
Pointer Based Linked Lists
We now describe how to implement a Linked-List ADT (Abstract Data Type) using pointers.
Main Points: Contains a “Size” field and a “Head” pointing to the list.
Implemented as a Class. Uses “Deep Copy” in the copy-constructor to copy the entire
data structure. Destructor deallocates dynamic memory of object.
20 45 75 85Head
4Size
#include <iostream>using namespace std;
class listNode{ // a node on the listpublic:
int data; // a data item on the listlistNode *next; // pointer to
next node};
typedef listNode* ptrType; // pointer to node
class listClass {public:
// constructors and destructor:listClass();listClass(const listClass& L);~listClass();
// list operations:bool empty();int length();void add(int newdata);void del(int deldata);void print();
private:int Size; ptrType Head;ptrType new_and_check();
};
ptrType listClass::new_and_check() {// allocate a new node; returns only upon successful // memory allocation; else, exit
ptrType temp;temp = new listNode;if (temp == NULL) { // not successful
cerr << "Memory allocation error!" << endl;exit(1);
}return temp;
}
listClass::listClass(){Size = 0;Head = NULL;
}
listClass::~listClass(){while(Head!=NULL){
ptrType cur = Head;Head = Head->next;delete cur;
}}
bool listClass::empty(){if(Size==0)
return true;else
return false;}
int listClass::length(){return Size;
}
listClass::listClass(const listClass& L){
Size = L.Size;
if(L.Head==NULL){
Head = NULL; // empty list
return;
}
Head = new_and_check (); // copy first node
Head->data = L.Head->data;ptrType cur = Head; // new list pointer
ptrType orig = L.Head->next;
while(orig != NULL) {
cur->next = new_and_check();
cur = cur->next;
cur->data = orig->data;
orig = orig->next;
}
cur->next = NULL;
}
void listClass::add(int newdata){Size++;ptrType newPtr = new listNode;if(newPtr==NULL){
cerr << "Memory error" << endl;exit(1);
}newPtr->data = newdata;newPtr->next = NULL;ptrType prev, cur = Head;while(cur!=NULL && newdata > cur->data){
prev = cur;cur = cur->next;
}if(cur==Head){
newPtr->next = Head; Head = newPtr;
}else{
newPtr->next = cur;prev->next = newPtr;
}}
void listClass::del(int deldata){ptrType prev, cur = Head;while(cur!=NULL && deldata > cur->data){
prev = cur;cur = cur->next;
}if(cur==NULL || cur->data!=deldata){
cout << "Delete error: " << deldata << " not in list!" << endl;
return;}if(cur==Head)
Head = Head->next;else
prev->next = cur->next;
delete cur;Size--;
}
void listClass::print(){cout << "[ ";ptrType cur = Head;while(cur != NULL){
cout << cur->data << " ";cur = cur->next;
}
cout << "]" << endl;}
void main(){listClass L;L.add(30);L.print();listClass N(L);N.print();L.add(20);L.print();L.add(40);L.print();L.add(10);L.print();L.del(50);L.print();L.del(40);L.print();L.del(30);L.print();L.del(20);L.print();L.del(10);L.print();
}
top related