Lect 5 Data Structure
Lect 5 Data Structure
• Structures are aggregate data types built using elements of primitive data types. • The Dot operator ( . ): to access data members of structure
• Structure is defined using the struct keyword:
variables.
– E.g. struct Time {
int hour; • The Arrow operator ( -> ): to access data members of pointer
int minute; variables pointing to the structure.
int second; • E.g. Print member hour of timeObject and timeptr.
};
• cout << timeObject . hour; or
• The struct keyword creates a new user defined data type that is used to declare
variables of an aggregate data type. • cout << timeptr -> hour;
• Structure variables are declared like variables of other types. • TIP: timeptr -> hour is the same as (*timeptr) . hour
– Syntax: struct <structure tag> <variable name>;
– E.g. struct Time timeObject,
• The parentheses is required since (*) has lower precedence
– struct Time *timeptr; than ( . )
3 4
5 6
1
Abstract Data Types (ADT) Lists
• ADTs • Linked lists are the most basic self-referential structures.
– Implement once in the program • Linked lists allow you to have a chain of structs with related
– Describe these operations once data.
– Use these operations again and again without going into the
– Sequence of elements
implementation
• However, the complexity of these operation depend on the implementation
and should not be considered constant
• An ADT may use a different ADT
• Examples commonly used complex abstract data types are Linked lists, • Operations
Stacks, Queues, Trees, etc.
– Add an element at the beginning/end/ith position
– Delete an element at the beginning/end/ith position
– Access(read/change) an element at the beginning/end/ith position.
7 8
• List can be implemented as an array • Normally first position (A[0])stores the current size of the list
– Actual number of elements currsize + 1
• Need to know the maximum number of elements in the list at
the start of the program • Adding at the beginning:
– Move all elements one position behind
• Adding/Deleting an element can take O(n) operations if the list – Add at position 1; Increment the current size by 1
has n elements.
• Accessing/changing an element anywhere takes O(1) For (j = A[0]+1; j > 1; j--)
operations independent of n A[j] = A[j-1];
A[1] = new element; Complexity: O(n)
A[0]® A[0]+1;
9 10
Add the element at the end Move all elements one position behind, kth
position onwards;
Increment current size by 1;
Add the element at the kth position
A[0]® A[0]+1;
For (j = A[0]+1; j > k; j--)
Complexity: O(n-k)
A[j] = A[j-1];
Complexity: O(1) A[k] = new element;
11 A[0]® A[0]+1; 12
2
Deleting an Element at the Beginning Deleting at the End
A[0]® A[0]-1;
For (j = 1; j < A[0] ; j++)
A[j] = A[j+1];
A[0]® A[0]-1;
Complexity: O(1)
Complexity: O(n)
13 14
Complexity: O(n-k)
15 16
3
Adding an element at the beginning Adding an element at the end
O(1); 19 20
Create(newnode);
last = head; // We know this is not NULL - list not empty!
while (last>nxt != NULL){ last.next= newnode;
last= last>nxt; // Move to next link in chain
}
The loop will terminate when last points to the last node in the chain, and it
knows when this happened because the nxt pointer in that node will point
21 22
to NULL.
Complexity: O(n) 23 24
4
Displaying the list of nodes Navigating through the list
temp = head; • Necessary when you want to insert or delete a node from
do { somewhere inside the list
if (temp = = NULL)
head
cout << "End of list" << endl;
else
{// Display details for what temp points to node *current;
cout << "Name : " << temp->name << endl; current = head;
cout << "Age : " << temp->age << endl; if (current -> nxt = = NULL)
cout << "Height : " << temp->height << endl; cout << "You are at the end of the list." << endl;
cout << endl; // Blank line else
// Move to next node (if present) current = current -> nxt;
temp = temp -> nxt;
}
} while (temp != NULL); • Moving the current pointer back one step is a little harder.
Complexity: O(n) 25 26
Head previous current 1. Firstly, the temporary pointer is assigned to the node after the current
one. This is the node to be deleted:
NULL current temp
NULL
if (current = = head)
cout << "You are at the start of the list" << endl;
else {
node *previous; // Declare the pointer 2. Now the pointer from the current node is made to leap-frog the next
previous = head; node and point to the one after that:
while (previous -> nxt != current) current temp
{
previous = previous -> nxt; NULL
}
current = previous; // possibly delete, update or insert operations at the current position
} 3. The last step is to delete the node pointed to by temp.
Complexity: O(n) 27 28
Delete a node after the current position Add a node after the current one
- Sample code
if (current->nxt = = NULL)
if (current->nxt == NULL) add_node_at_end();
cout << "There is no node after current" << endl; else {
else node *temp;
{ temp = new node;
node *temp; get_details(temp); //Call the function to enter the details
temp = current -> nxt; // Make the new node point to the same thing as the current node
current -> nxt = temp -> nxt; // Could be NULL temp->nxt = current->nxt;
delete temp; // Release the memory pointed to by temp // Make the current node point to the new link in the chain
} current->nxt = temp;
}
29 30
5
Adding at the kth position Deleting a node from the list
Deleting the first node in the linked list Deleting the first node in the linked list
head
temp = head; //Make the temp pointer //point • Here is the function that deletes a node from the head:
to the head pointer void delete_start_node()
{
node *temp;
head temp = head;
head = head->nxt; // Second node in chain. head= head-> nxt;
delete temp;
}
delete temp; // Wipe out original start node
head
33 34
6
Deleting the last node (2) Deleting the last node (3)
head
Goes on until temp1 is
head
pointing to the last node NULL
in the list, with temp2
move temp1 to the pointing to the one
next node in the list before the last node
temp 2 temp1
head
temp1 still doesn't point to head delete the node
the last node in the list, so we NULL pointed to by
make temp2 point to what temp1
temp1 points to
temp 2 temp1
head
41