Chapter 3 - Linked Lists (Autosaved)
Chapter 3 - Linked Lists (Autosaved)
Algorithms
Instructor: Yonas B
Gondar University
Chapter Three: Linked Lists
This chapter covers:
Singly linked lists
Doubly linked lists
2
Structures
Structures are aggregate data types built using
elements of primitive data types.
Structures are defined using the struct keyword:
E.g. struct Time{
int hour;
int minute;
int second;
};
Structure variables are declared like variables of
other types.
Syntax: struct <structure tag> <variable name>;
E.g. struct Time timeObject,
struct Time *timeptr;
Singly Linked Lists
Linked lists are the most basic self-
referential structures. Linked lists allow you
to have a chain of structs with related data.
Array vs. Linked lists
Arrays are simple and fast but we must specify
their size at construction time. This has its own
drawbacks. If you construct an array with space
for n, tomorrow you may need n+1.Here comes a
need for a more flexible system.
Advantages of Linked Lists
Flexible space use by dynamically allocating space
for each element as needed. This implies that one
need not know the size of the list in advance.
Memory is efficiently utilized.
Creating Linked Lists in C++
A linked list is a data structure which is built
from structures and pointers.
It forms a chain of "nodes" with pointers
representing the links of the chain and
holding the entire thing together.
A linked list is made up of a chain of nodes.
Each node contains:
the data item, and
a pointer to the next node
5
…cont
Graphically,
7
Definition and
Operations on Data
Structures
i) Definition of Data Structures
A) Array:
struct Node
{
char name[15];
int age;
float height;
};
Node person[1000];
8
...
11
…
Then we will fill details of the person
cout << "Please enter the name of the person: ";
cin >> temp->name;
cout << "Please enter the age of the person : ";
cin >> temp->age;
cout << "Please enter the height of the person: ”;
cin >> temp->height;
temp->nxt = NULL;
The last line sets the pointer from this node to the
next to NULL, indicating that this node, when it is
inserted in the list, will be the last node.
12
Insert at the beginning
Void add_node_at_first()
{
if(start_ptr==NULL)
start_ptr=temp;
else
{
temp->next=start_ptr;
start_ptr=temp;
}
}
iii) The full code for adding a node at the end of the list is
shown below, in its own little function:
14
…
Graphically
15
iv) Traversing (Displaying the
list of nodes)
Array:
for (i=0;i<=lastperson;i++)
{
cout<<”Person Name:” <<person[i].
name;
cout<<”Book
Reserved :”<<person[i].age;
cout<<” Borrowed :”<< person[i].
height;
}
16
…
Single Linked List:
Having added one or more nodes, we
need to display the list of nodes on the
screen.
This is comparatively easy to do. Here is
the method:
Set a temporary pointer to point to the same
thing as the start pointer.
If the pointer points to NULL, display the
message "End of list" and stop.
Otherwise, display the details of the node
pointed to by the start pointer.
Make the temporary pointer point to the
same thing as the nxt pointer of the node it
is currently indicating.
Jump back to step 2. 17
…
temp = start_ptr;
do {
if (temp == NULL)
cout << "End of list" << endl;
else {
// Display details for what temp points to
cout << "Name : " << temp->name <<
endl;
cout << "Age : " << temp->age << endl;
cout << "Height : " << temp->height <<
endl;
cout << endl; // Blank line
// Move to next node (if present)
temp = temp->nxt;
}
}
while (temp != NULL); 18
v) Deleting a node from the list
When it comes to deleting nodes, we
have three choices:
Delete a node from the start of the list,
delete one from the end of the list, or
delete one from somewhere in the middle.
20
…
Now that the first node has been safely
tagged (so that we can refer to it even when
the start pointer has been reassigned), we
can move the start pointer to the next node
in the chain:
start_ptr = start_ptr->nxt; // Second node in
chain.
21
…
.
22
Here is the function that deletes a
node from the start:
void delete_start_node()
{
node *temp;
temp = start_ptr;
start_ptr = start_ptr->nxt;
delete temp;
}
23
Deleting a node from the end
1. Look at the start pointer. If it is NULL, then the
list is empty, so print out a "No nodes to delete"
message.
2. Make temp1 point to whatever the start pointer
is pointing to.
3. If the nxt pointer of what temp1 indicates is
NULL, then we've found the last node of the
list, so jump to step 7.
4. Make another pointer, temp2, point to the
current node in the list.
5. Make temp1 point to the next item in the list.
6. Go to step 3.
7. If you get this far, then the temporary pointer,
temp1, should point to the last item in the list
and the other temporary pointer, temp2, should
point to the last-but-one item.
8. Delete the node pointed to by temp1.
9. Mark the nxt pointer of the node24pointed to by
Suppose we want to delete the last
node from this list:
25
Let's get straight on with step2 - set the
pointer temp1 to the same as the start
pointer:
26
The nxt pointer from this node isn't NULL, so we
haven't found the end node. Instead, we set the
pointer temp2 to the same node as temp1
27
and then move temp1 to the next node in the list:
28
Going back to step 3, we see that temp1 still doesn't
point to the last node in the list, so we make temp2
point to what temp1 points to
29
and temp1 is made to point to the next node along:
30
Eventually, this goes on until temp1 really is
pointing to the last node in the list, with temp2
pointing to the penultimate node:
31
Now we have reached step 8. The next thing to do is
to delete the node pointed to by temp1
32
and set the nxt pointer of what temp2 indicates to
NULL:
33
The code to delete last node.
void delete_end_node()
{ node *temp1, *temp2;
if (start_ptr == NULL) cout << "The list is
empty!" << endl;
Else
{ temp1 = start_ptr;
if (temp1->nxt == NULL)
{ delete temp1;
start_ptr = NULL;
} Else
{ while (temp1->nxt != NULL)
{ temp2 = temp1;
temp1 = temp1->nxt;
}
delete temp1;
temp2->nxt = NULL;
}
} 34
}
Navigating through the list
Move on:
if (current->nxt == NULL)
cout << "You are at the end of the list." <<
endl;
Else
current = current->nxt;
35
Move Back:
if (current == start_ptr)
cout << "You are at the start of the list" <<
endl;
else
{
node *previous; // Declare the pointer
previous = start_ptr;
while (previous->nxt != current)
{
previous = previous->nxt;
}
current = previous;
} 36
…cont
Moving the current pointer back one step is
a little harder. This is because we have no
way of moving back a step automatically
from the current node. The only way to find
the node before the current one is to start at
the beginning, work our way through and
stop when we find the node before the one
we are considering at the moment.
Deleting from the center of
the list
First navigate to the list (use above code)
if (current->nxt == NULL)
cout << "There is no node after current" <<
endl;
else
{ node *temp;
temp = current->nxt;
current->nxt = temp->nxt; // Could be NULL
delete temp;
}
38
Here is the code to add a node
after the current one
if (current->nxt == NULL)
add_node_at_end();
else {
node *temp;
new temp;
get_details(temp);
// Make the new node point to the same thing
as the current node
temp->nxt = current->nxt;
// Make the current node point to the new link
in the chain
current->nxt = temp; }
39
Add the node with
( Samrawit, 32,1.64) next
to Current – Quiz 5%
Start_ptr current
Eden Hadera
21 22
1.45 1.75
40
Doubly Linked List
A doubly linked list is one where there are
links from each node in both directions.
41
…cont
Definitions
struct Book
{
int BookId;
bool BookRes;
bool BookBor;
Book *next, *prev;
};
Book *start_ptr=NULL; //first node
43
Storing and accessing
Data files
(*Bookptr).BookID=3;
(*Bookptr).BookRes=true;
(*Bookptr).BookBor =false;
(*Bookptr).next=NULL;
(*Bookptr).prev=NULL;
44
Adding a node to the end
of a list
Book *NewBookptr, *end_ptr= NULL;
NewBookptr= new Book;
NewBookptr->BookId=BI; startr_ptr end_ptr
NewBookptr->BookRes=BR;
NewBookptr->BookBor=BB;
NewBookptr->next=NULL
if(end_ptr==NULL) Data1 Data2 Data3
Next Next Next
{
prev prev
NewBookptr->prev=NULL; prev
Start_ptr=NewBookptr;
end_ptr= NewBookptr;
}
else
{
NewBookptr->prev=end_ptr;
end_ptr->Next=NewBookptr;
End_ptr=NewBookptr;
} 45
Deleting a node
if(LibBookptr!=NULL)
if(currBookptr==LibBookptr) //first node
{ LibBookptr=LibBookptr->next;
LibBookptr->prev=NULL;
}else if(currBookptr->next==NULL);// last
node
PrevBookptr->next=NULL;
else //middle node
{ PrevBookptr->Next=currBookptr->next;
nextBookptr->prev=PrevBookptr;
}
delete currentBookptr;
46
Inserting a node
prevbookptr->next=currBookptr;
currBookptr->prev=prevbookptr;
currBookptr->next=nextBookptr;
nextBookptr->prev=currBookptr;
1 2 4 5
NextBookptr
PrvBookptr
3 47
currBookptr