









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
its notes of data structure .in this we show about data structure
Typology: Exams
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Time budget: 25 minutes
Task: Work alone through this learning exercise. Follow the working instructions marked with a pen. The learning exercise consists of eight parts. You will probably not be able to finish everything during the lecture. If you get up to part number four it’s already fine. Finishing this paper will be part of exercise number eleven.
Target: You learn what linked lists are and how to handle them. Beside you repeat pointers and dynamic data object management.
Contents:
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 can be represented by a diagram like this one:
This linked list has four nodes in it, each with a link to the next node in the series. The last node has a link to the special value NULL The NULL pointer has been presented in the lecture and is used here, to show that it is the last link in the chain. There is also another special pointer, called Start or Root, which points to the first link in the chain so that we can keep track of it.
Can you think at two situations where you would use linked lists:
2 ) .………………………………………………………………………………………………………………………………………….
The key part of a linked list is a structure, which holds the data for each node (the name, address, age or whatever for the items in the list), and, most importantly, a pointer to the next node. Here I have given the structure of a typical node:
struct Node { char name[20]; // Name of up to 20 letters int age; // D.O.B. would be better float height; // In meters *Node next; // Pointer to next node }; *Node start_ptr = NULL; // Start Pointer (root)
The important part of the structure is the line before the closing curly brackets. This gives a pointer to the next node in the list. This is the only case in C++ where you are allowed to refer to a data type (in this case Node) before you have even finished defining it! I have also declared a pointer called start_ptr which will permanently point to the start of the list. To start with, there are no nodes in the list, which is why start_ptr is set to NULL.
A library wants to manage its books with a computer program. A book is characterized by the author, the title and the ISBN number. Write down a useful node struct for this case.
The first problem that we face is how to add a node to the list. For simplicity's sake, we will assume that it has to be added to the end of the list, although it could be added anywhere in the list (a problem I will deal with later on). Firstly, we declare the space for a pointer item and assign a temporary pointer to it. This is done using the new statement as follows:
Node *temp; temp = new Node;
We can refer to the new node as *temp, i.e. "the node that temp points to". Having declared the node, we ask the user to fill in the details of the person, i.e. the name, age, address or whatever:
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->next = NULL;
The full code for adding a node at the end of the list is shown below, in its own little function:
void add_node_at_end () { node *temp, *temp2; // Temporary pointers
// Reserve space for new node and fill it with data temp = new node; 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->next = NULL;
// Set up link to this node if (start_ptr == NULL) start_ptr = temp; else { temp2 = start_ptr; // We know this is not NULL - list not empty! while (temp2->next != NULL) temp2 = temp2->next; // Move to next link in chain temp2->next = temp; } }
((OOppttiioonnaall)) If this was to easy till now. Assume that our linked list is sorted by age. So if we add a new node, we have to put it at the right position in the list. Adaped the above code dealing with this new requirenment.
Having added one or more nodes, we need to display the list of nodes on the screen. This is comparatively easy to do. You just have to traverse the whole list as we learned it in the previous part. Here is again a summary of the method:
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.
Complete the following code snippet to print out the content of the linked list. Hint: It might help if you draw a diagram of a linked list. If you have absolutely no idea how to manage this, study again at the procedure add_node_at_end from last chapter.
Node *temp; temp = start_ptr;
// Display details for what temp points to cout << "Name : " << temp->name << endl; cout << "Age : " << temp->age << endl; cout << "Height : " << temp->height << endl; cout << endl;
delete temp; // Wipe out original start node
Here is the function that deletes a node from the start:
void delete_start_node() { Node *temp; temp = start_ptr; start_ptr = start_ptr->next; delete temp; }
Deleting a node from the end of the list is not much harder, as the temporary pointer must find where the end of the list is by hopping along from the start. This is done using code that is almost identical to that used to insert a node at the end of the list. But it is necessary to maintain two temporary pointers, temp1 and temp2. The pointer temp1 will point to the last node in the list and temp2 will point to the previous node. We have to keep track of both as it is necessary to delete the last node and immediately afterwards, to set the next pointer of the previous node to NULL (it is now the new last node).
Every thing clear? No? So let's try it with a rough drawing. This is always a good idea when you are trying to understand an abstract data type. But in some of the following drawings marked with the hand the temp1 and temp2 pointers disappeared. Fix them.
Suppose we want to delete the last node from this list:
Firstly, the start pointer doesn't point to NULL, so we don't have to display a "Empty list, wise guy!" message. Let's get straight on with step2 - set the pointer temp1 to the same as the start pointer:
The next 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 temp
and then move temp1 to the next node in the list:
and set the next pointer of what temp2 indicates to NULL:
I suppose you want some code for all that! All right then ....
void delete_end_node() { Node *temp1, *temp2; if (start_ptr == NULL) cout << "The list is empty!" << endl; else { temp1 = start_ptr; while (temp1->next != NULL) { temp2 = temp1; temp1 = temp1->next; } delete temp1; temp2->next = NULL; } }
The code seems a lot shorter than the explanation!
Let’s have a profound look at the code above. Can you spot a problem?
Now, the sharp-witted amongst you will have spotted a problem. If the list only contains one node, the code above will malfunction. This is because the function goes as far as the temp1 = start_ptr statement, but never gets as far as setting up temp2. The code above has to be adapted so that if the first node is also the last (has a NULL next pointer), then it is deleted and the start_ptr pointer is assigned to NULL. In this case, there is no need for the pointer temp2.
I already prepared a new code snippet for you catching this problem. But it still lacks of some lines handling the critical sitatuation. Try to fixe them.
void delete_end_node() { node *temp1, *temp2; if (start_ptr == NULL) cout << "The list is empty!" << endl; else { temp1 = start_ptr; if (temp1->next == NULL) // This part is new! {
// Here comes your code // Fix me!
else { while (temp1->next != NULL) { temp2 = temp1; temp1 = temp1->next; } delete temp1; temp2->next = NULL; } } }
(O (Oppttiioonnaall)) If this was very boring and to easy for you I have an expert task for you. Try to adaped the above code to delete alle nodes with a certain age.
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 the moment. We can tell when this happens, as the next pointer from that node will point to exactly the same place in memory as the current pointer (i.e. the current node).
First of all, we had better check to see if the current node is also first the one. If it is, then there is no "previous" node to point to. If not, check through all the nodes in turn until we detect that we are just behind the current one (Like a pantomime - "behind you!")
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->next != current) previous = previous->next; current = previous; }
The else clause translates as follows: Declare a temporary pointer (for use in this else clause only). Set it equal to the start pointer. All the time that it is not pointing to the node before the current node, move it along the line. Once the previous node has been found, the current pointer is set to that node - i.e. it moves back along the list.
Now that you have the facility to move back and forth, you need to do something with it. Firstly, let's see if we can alter the details for that particular node in the list:
cout << "Please enter the new name of the person: "; cin >> current->name; cout << "Please enter the new age of the person : "; cin >> current->age; cout << "Please enter the new height of the person : "; cin >> current->height;
The next easiest thing to do is to delete a node from the list directly after the current position. We have to use a temporary pointer to point to the node to be deleted. Once this node has been "anchored", the pointers to the remaining nodes can be readjusted before the node on death row is deleted. Here is the sequence of actions:
Firstly, the temporary pointer is assigned to the node after the current one. This is the node to be deleted:
Now the pointer from the current node is made to leap-frog the next node and point to the one after that:
The last step is to delete the node pointed to by temp.
Here is the code for deleting the node. It includes a test at the start to test whether the current node is the last one in the list:
if (current->next == NULL) cout << "There is no node after current" << endl; else { node *temp; temp = current->next; current->next = temp->next; // Could be NULL delete temp; }
Adding a node after the current one is done very similarly, but I haven't illustrated it with diagrams.
Develop the code to add a node after the current one.