Data Structure and Linked List
Data Structure and Linked List
College of Informatics
Department of Computer Science
Data Structure and Algorithm Analysis
Chapter Three
Data Structure and Linked List
2
struct list{
char name[10];
int count;
list *next;
};
Self-Referentia Structure variables are declared like
variables of other types.
list *temp;
The Arrow operator (->): to access data members of
pointer variables pointing to the structure.
cout<<temp->count;
TIP: temp->count is the same as (*temp).count
The parentheses is required since (*) has lower precedence than
(.)
3
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.
4
A linked list is made up of a chain of nodes. Each node
contains:
the data item, and
a pointer to the next node
7
if the list is empty to start with, there's no problem - just
set the head pointer to point to this node (i.e. set it to the
same value as temp):
if (head == NULL)
head = temp;
if there are already nodes in the list. In this case, the secret
is to declare a second pointer, temp2, to step through the
list until it finds the last node.
8
temp2 = head;
// We know this is not NULL - list not empty!
while (temp2->next != NULL)
{
temp2 = temp2->next; // Move to next link in chain
}
The loop will terminate when temp2 points to the last
node in the chain, and it knows when this happened
because the next pointer in that node will point to NULL.
When it has found it, it sets the pointer from that last node
to point to the node we have just declared:
9
The link temp2->next in this diagram is the link joining
the last two nodes. The full code for adding a node at the
end of the list is shown below, in its own little function:
10
void add_node_at_end ( )
{
student *temp, *temp2; // Temporary pointers
// Reserve space for new node and fill it with data
temp = new student;
cout << "Please enter the name of the student: ";
cin >> temp->name;
cout << "Please enter the age of the student : ";
cin >> temp->age;
cout << "Please enter the gpa of the student : ";
cin >> temp->gpa;
temp->next = NULL;
11
// Set up link to this node
if (head == NULL)
head = temp;
else {
temp2 = head;
while (temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
}
12
Displaying the list of nodes
We need to display the list of nodes on the screen.
1. Set a temporary pointer to point to the same thing as the
start pointer.
2. If the pointer points to NULL, display the message
"End of list" and stop.
3. Otherwise, display the details of the node pointed to by
the start pointer.
4. Make the temporary pointer point to the same thing as
the next pointer of the node it is currently indicating.
5. Jump back to step 2.
13
The temporary pointer moves along the list, displaying the
details of the nodes it comes across. At each stage, it can
get hold of the next node in the list by using the next
pointer of the node it is currently pointing to. Here is the
C++ code that does the job:
temp = head;
do {
if (temp == NULL)
cout << "End of list" << endl;
else { // Display details for what temp points to
cout << temp->name << " " <<temp->age << " "
<< temp->gpa << endl;
temp = temp->next; // Move to next node (if present)
}
} while (temp != NULL);
14
Using while loop to display the list of node
temp = head;
while (temp != NULL) {
// Display details for what temp points to
cout << temp->name << " " <<temp->age << " "
<< temp->gpa << endl;
temp = temp->next; // Move to next node (if present)
}
cout << "End of list" << endl;
15
Using for loop to display the list of node
student *temp;
for(temp=head; temp != NULL; temp=temp->next) {
// Display details for what temp points to
cout << temp->name << " " <<temp->age << " "
<< temp->gpa << endl;
}
cout << "End of list" << endl;
16
Navigating through the list
One thing you may need to do is to navigate through the
list, with a pointer that moves backwards and forwards
through the list, like an index pointer in an array. This is
certainly necessary when you want to insert or delete a
node from somewhere inside the list, as you will need to
specify the position.
We will call the mobile pointer current. First of all, it is
declared, and set to the same value as the start (head)
pointer
student *current;
current = head;
17
The above statement makes them both point to the same thing:
20
When a node is deleted, the space that it took up should be
reclaimed(reuse). Otherwise the computer will eventually
run out of memory space. This is done with the delete
instruction:
delete temp; // Release the memory pointed to by temp
However, we can't just delete the nodes willy-nilly
(Without having a choice) as it would break the chain. We
need to reassign the pointers and then delete the node at
the last moment. Here is how we go about deleting the
first node in the linked list:
temp = head; // Make the temporary pointer
// identical to the start pointer
21
Now that the first node has been safely tagged, we can
move the start pointer to the next node in the chain:
head = head->next; // Second node in chain.
22
When this statement is executed
delete temp;
24
The next thing is to delete the node pointed to by temp1
25
We suppose you want some code for all that!
void delete_end_node( ) {
student *temp1, *temp2;
if (head == NULL)
cout << "The list is empty!" << endl;
else {
temp1 = head;
while (temp1->next != NULL) {
temp2 = temp1;
temp1 = temp1->next;
}
delete temp1;
temp2->next = NULL;
}
} 26
Deleting a node at the middle of of the list
The node temp1 is to be deleted:
Now the pointer from the temp2 node is made to leap-frog the
next node and point to the one after that:
temp2->next=temp1->next;
delete temp1;
27
Doubly Linked Lists
A doubly linked list is one where there are links from
each node in both directions:
You will notice that each node in the list has two pointers,
one to the next node and one to the previous one - again,
the ends of the list are defined by NULL pointers.
28
Creating Doubly Linked Lists
The nodes for a doubly linked list would be defined as follows:
struct node{
char name[20];
node *next; // Pointer to next node
node *prev; // Pointer to previous node
};
node *current;
current = new node;
current->name = "Alemu";
current->next = NULL;
current->prev = NULL;
29
We have also included some code to declare the first node
and set its pointers to NULL. It gives the following
situation:
30
while (temp->prev != NULL)
temp = temp->prev;
// Declare a new node and link it in
node *temp2;
temp2 = new node;
// Store the new name in the node
temp2->name = new_name;
// This is the new start of the list
temp2->prev = NULL;
temp2->next = temp; // Links to current list
temp->prev = temp2;
}
31
void add_node_at_end (char new_name[ ]))
{ // Declare a temporary pointer and move it to the end
node *temp = current;
while (temp->next != NULL)
temp = temp->next;
node *temp2; // Declare a new node and link it in
temp2 = new node;
// Store the new name in the node
temp2->name = new_name;
temp2->next = NULL; // This is the new start of the list
temp2->prev = temp; // Links to current list
temp->next = temp2;
}
32