0% found this document useful (0 votes)
22 views29 pages

Lecture 3 - Data Structures

Chapter 3 discusses data structures, focusing on structures and linked lists in C++. It explains how to define structures, access their members, and create linked lists using self-referential structures, along with methods for adding, displaying, navigating, and deleting nodes. The chapter also introduces doubly linked lists, which allow bidirectional traversal of nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views29 pages

Lecture 3 - Data Structures

Chapter 3 discusses data structures, focusing on structures and linked lists in C++. It explains how to define structures, access their members, and create linked lists using self-referential structures, along with methods for adding, displaying, navigating, and deleting nodes. The chapter also introduces doubly linked lists, which allow bidirectional traversal of nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter 3

Data Structures
Structures
• Structures are aggregate data types built using elements of primitive data types
• Structure are defined using the struct keyword:
• Example :
struct Time{
int hour;
int minute;
int second; };
• The struct keyword creates a new user defined data type that is used to declare variables of an
aggregate data type
• Structure variables are declared like variables of other types.

Syntax: struct <structure tag> <variable name>;


E.g. struct Time timeObject, // instance variables
struct Time *timeptr; // pointer variables
Accessing Members of Structure Variables
• The Dot operator (.): to access data members of structure variables.
• The Arrow operator (->): to access data members of pointer variables pointing to the
structure.

E.g. Print member hour of timeObject and timeptr.


cout<< timeObject.hour; or
cout<<timeptr->hour;
TIP: timeptr->hour is the same as (*timeptr).hour.
cout<<(*timeptr).hour;
• The parentheses is required since (*) has lower precedence
than (.).
Self-Referential Structures
• Structures can hold pointers to instances of themselves

struct list{
char name[10];
int count;
struct list *next; // pointer that points to instance of themselves
};

• However, structures cannot contain instances of themselves.


struct list{
char name[10];
int count;
struct list lstobj; // this is not allowed
};
Single Linked Lists
• The most basic self-referential structures.
• It allow to have a chain of structs with related data
Array vs. Linked lists
• Arrays are simple and fast but it specify their size at construction time
• Array has static memory allocation
Advantages of Linked Lists
• Flexible space use by dynamically allocating space for each element as needed
• Memory is efficiently utilized

• A linked list is made up of a chain of nodes. Each node


contains:
 the data item, and
 a pointer to the next node
Creating Linked Lists in C++
• A linked list is a data structure that is built from structures (holding entire thing)and
pointers (links the chain in of the nodes)

 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, it shows last link in the chain.
 Start (also called head) pointer , which points to the first link in the chain so that we
can keep track of it.
Defining the data structure for a linked list
• The key part of a linked list is a structure, which holds the data for each node and, most
importantly, a pointer to the next node
struct node {
char name[20]; // Name of up to 20 letters
int age;
float height; // In metres
node *nxt;// Pointer to next node in the list – important part of structure
};
struct node *start_ptr = NULL;
/* permanently point to the start of the list and to start with there is no nodes in
the list , which is why start_ptr is set to NULL */
Adding a node to the list
Step 1:- declare the space for a pointer item and assign a temporary pointer to it.
temp = new node;

• The new node as *temp, i.e. "the node that temp points to".
Step 2 : Fill the node with data , use the arrow pointer notation

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. */
Adding a node to the list
Add a node with empty list (to end list)
• If the list is empty to start with, set the start pointer to point to this node
if (start_ptr == NULL)
start_ptr = temp;
Add a node to already nodes in the list (to end list)
• It is harder 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.
void add_node_at_end ()
{ node *temp, *temp2; // Temporary pointers

// Reserve space for new node and fill it with


data
Adding a node to the list temp = new node;
cout << "Please enter the name of the
temp2 = start_ptr; // We know this is not NULL - list not empty!
person: ";
while (temp2->nxt != NULL) { cin >> temp->name;
temp2 = temp2->nxt; // Move to next link in chain cout << "Please enter the age of the person :
} temp2->nxt = ";
temp; cin >> temp->age;
cout << "Please enter the height of the
person : ";
cin >> temp->height;
temp->nxt = 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->nxt != NULL)
{ temp2 = temp2->nxt;
// Move to next link in chain
}
Displaying the list of nodes
• Having added one or more nodes, we need to display the list of nodes on the screen.
• Here is the method:
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 nxt pointer of the node it is
currently indicating.
5. Jump back to step 2.
• The temporary pointer moves along the list, displaying the details of the nodes it comes
across.
Displaying the list of nodes
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);
Navigating through the list
• With a pointer that moves backwards and forwards through the list, like an index pointer
in an array.
• Necessary to insert or delete a node from somewhere inside the list
• First of all, declared mobile pointer, and set to the same value as the start_ptr pointer:
node *current; // declare a mobile pointer
current = start_ptr;

In fact, check that it isn't pointing to the last item in the list. If it is, then
there is no next node to move to:
if (current->nxt == NULL)
cout << "You are at the end of the list." << endl;
else
current = current->nxt; // unless move to next nodes
Navigating through the list
Moving back
• 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

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
Navigating through the list
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;
}
Navigating through the list
Delete a node after current position
• 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:
1. Firstly, the temporary pointer is assigned to the node after the current one. This is the
node to be deleted
current temp
NULL
Navigating through the list
2. Now the pointer from the current node is made to leap-frog the next node and point to the
one after that

3. 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->nxt == NULL)
cout << "There is no node after current" << endl;
else
{ node *temp;
temp = current->nxt; // point to node after current
current->nxt = temp->nxt; // Could be NULL – link the current to node after
temp
delete temp; // delete the temp node – node after current node
Navigating through the list
Add a node after current node
Here is the code to add a node after the current one. This is done similarly, but we haven't
illustrated it with diagrams:
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;
}
Deleting a node from the list
• Three choices of deleting a node : Delete a node from the start of the list(simple), delete
one from the end of the list (simple), or delete one from somewhere in the middle.
• When a node is deleted, the space that it took up should be reclaimed. 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 as it would break the chain. We need to
reassign the pointers and then delete the node at the last moment.

Deleting the first node in the linked list:


temp = start_ptr; // Make the temporary pointer
// identical to the start pointer
Deleting a node from the list

Now that the first node has been safely tagged and move the start pointer to the next node in
the chain:
start_ptr = start_ptr->nxt; // Second node in chain.
Deleting a node from the list
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->nxt;
delete temp;
}
Deleting a node from the list
Deleting a node from end of list
• Deleting a node from the end of the list is harder, as the temporary pointer must find
where the end of the list is by hopping along from the start.
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 node pointed to by temp2 as NULL - it is the new last node.
Deleting a node from the list
• 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:
Deleting a node from the list
• 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

• and then move temp1 to the next node in the list:


Deleting a node from the list
• 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

• and temp1 is made to point to the next node along:


Deleting a node from the list
• Eventually, this goes on until temp1 really is pointing to the last node in the list, with
temp2 pointing to the penultimate node:

• Now we have reached step 8. The next thing to do is to delete the node pointed to by
temp1
Deleting a node from the list
• and set the nxt pointer of what temp2 indicates to NULL:
void delete_end_node()
{ node *temp1, *temp2;
if (start_ptr == NULL)
cout << "The list is empty!"
<< endl;
else
{ temp1 = start_ptr;
while (temp1->nxt != NULL)
{ temp2 = temp1;
temp1 = temp1->nxt;
}
delete temp1;
temp2->nxt = NULL;
}
}
void delete_end_node()
{ node *temp1, *temp2;
Deleting a node from the list if (start_ptr == NULL)
cout << "The list is empty!" << endl;
• If the list only contains one node, the code else
above will malfunction. This is because the { temp1 = start_ptr;
if (temp1->nxt == NULL) // This part
function goes as far as the temp1 =
is new!
start_ptr statement, but never gets as far { delete temp1;
as setting up temp2. start_ptr = NULL;
}
• The code above has to be adapted so that if
else
the first node is also the last (has a NULL { while (temp1->nxt != NULL)
nxt pointer), then it is deleted and the { temp2 = temp1;
start_ptr pointer is assigned to NULL. In this temp1 = temp1->nxt;
case, there is no need for the pointer }
temp2: delete temp1;
temp2->nxt = NULL;
}
}
}
Doubly Linked Lists
• A doubly linked list is one where there are links from each node in both directions:

• Each node in the list has two pointers, one to the next node and one to the previous
one
• The ends of the list are defined by NULL pointers.
• Also there is no pointer to the start of the list. Instead, there is simply a pointer to
some position in the list that can be moved left or right.
• With the doubly linked list, we can move the current pointer backwards and forwards
at will.

You might also like