Documente Academic
Documente Profesional
Documente Cultură
Motivation
☛ A “List” is a useful structure to hold a collection of
data.
■ Currently, we use arrays for lists
☛ Examples:
List of ten students marks
int studentMarks[10];
Data
20 45 75 85
0 1 2 array
Link
Link
Data 45 linked list
85
20
75
Data Link
20 45 75 85
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:
Data Link
20 45 75 85
Linked Lists: Basic Ideas
☛ The list can grow and shrink
20 45
addEnd(75), addEnd(85)
20 45 75 85
75
Linked Lists: Operations
☛ Original linked list of integers:
20 45 75 85
60
☛ Deletion (in the middle)
20 45 75 85
deleted item
Definition of linked list type:
struct Node{
int data;
Node* next;
};
We can also:
☛ 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.
Passing a Linked List
to a Function
It is roughly the same as for an array!!!
head = NULL;
Head
Node* newPtr; 20
Head
newPtr = new Node;
newPtr->data = 20; newPtr
newPtr->next = NULL;
head = newPtr;
Inserting a Node
at the Beginning
newPtr = new Node;
newPtr->data = 13;
newPtr->next = Head;
head = newPtr;
20
Head
13
newPtr
Keep going …
Head
50 40 13 20
newPtr
Adding an element to the head:
NodePtr&
newPtr->data = newdata;
newPtr->next = Head;
head = newPtr;
}
Also written (more functionally, better!) as:
newPtr->data = newdata;
newPtr->next = Head;
return newPtr;
}
p = head;
head = head->next;
delete p;
p
void deleteHead(Node*& head){
if(head != NULL){
NodePtr p = head;
head = head->next;
delete p;
}
}
As a function:
Node* deleteHead(Node* head){
if(head != NULL){
NodePtr p = head;
head = head->next;
delete p;
}
return head;
}
Displaying a Linked List
p = head;
head
20 45
p = p->next;
head
20 45
p
A linked list is displayed by walking through its nodes one by one,
and displaying their data fields (similar to an array!).
For an array:
void displayArray(int data[], int size) {
int n=0;
while ( n<size ) {
cout << data[i] << endl;
n++;
}
}
Searching for a node (look at array
searching first!)
//return the pointer of the node that has data=item
//return NULL if item does not exist
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;
}
if(newhead != NULL){
Node* p = newhead;
newhead = newhead->next;
delete p;
}
return newhead;
}
More operation:
adding to the end
☛ Original linked list of integers:
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!
Add to the end:
void addEnd(NodePtr& head, int newdata){
NodePtr newPtr = new Node;
newPtr->data = newdata;
newPtr->next = NULL;
last->next = newPtr;
}
else // deal with the case of empty list
head = newPtr;
}
last->next = newPtr;
}
else // deal with the case of empty list
head = newPtr;
return head;
}
Implementation of a
Sorted Linked List
Inserting a Node
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;
Head
20 45 75 ...
prev 33 cur
newPtr
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
It’s a kind of search algo,
prev = NULL;
cur = head;
found=false;
while( (cur!=NULL) && (!found) ) {
if (newValue > cur->data) {
prev=cur;
cur=cur->next;
}
else found = true;
}
prev = NULL;
cur = head;
while( (cur!=NULL) && (newValue>cur->data) ) {
prev=cur;
cur=cur->next;
}
pre = NULL;
cur = head;
while( (cur != NULL) && (item>cur->data)){
pre = cur;
cur = cur->next;
}
return head;
}
// not recommended void type function
void 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;
}
prev cur
Delete an element in a sorted linked list:
return head;
}
// in a void function, not recommended
delete cur;
General case
}
}