0% found this document useful (0 votes)
18 views

Lect 5 Data Structure

Data Structure definition

Uploaded by

Yohans Brhanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lect 5 Data Structure

Data Structure definition

Uploaded by

Yohans Brhanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Usual Data Types

• Examples: Integer, Double, Boolean


– You do not know the implementation of these.
– There are some operations which can be done with these
Data Structures (add, multiply, read, write)
– Programmer just uses these operations without knowing
their implementation.
Fitsum Admasu
Department of Computer Science
Addis Ababa University

Structure Accessing Members of Structure Variables

• 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

Self-Referential Structures Abstract Data Types


• Structures can hold pointers to instances of themselves. • An ADT consists of an abstract data structure and operations.
struct list { • The ADT specifies:
– What can be stored in the Abstract Data Type?
char name[10];
– What operations can be done on/by the Abstract Data Type?
int count;
struct list *next; • Example 1: the 32-bit int data type is defined both by the fact that a value
}; of type int consists of 32 binary bits but also by the fact that two int values
• However, structures cannot contain instances of themselves. can be added, subtracted, multiplied, compared, and so on.

• Example 2: An array is defined both by the fact that it is a sequence of data


items of the same basic type, but also by the fact that it is possible to
directly access each of the positions in the list based on its numerical index.

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

Array Implementation of a List Adding an element

• 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

Adding at the End Adding at kth position

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[A[0]+1] = new element; Increment current size by 1;

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

Deleting at the beginning:


Move all elements one position ahead; Delete the element at the end

Decrement the current size by 1 Decrement current size by 1;

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

Deleting at the kth position Accessing an Element at the kth position

Move all elements one position ahead, k+1th A[k];


position onwards;
Decrement the current size by 1;

For (j = k; j < A[0]+1; j++) O(1) operation;


A[j] = A[j+1];
A[0]® A[0]-1;

Complexity: O(n-k)
15 16

Linked List Defining the data structure for a linked list


Consists of a sequence of nodes
• The key part of a linked list is a structure, which holds the data for each
A node in the linked list consists of two elements node (the name, address, age or whatever for the items in the list), and,
most importantly, a pointer to the next node.
Value of the corresponding element in the list • Example of a typical node:
Pointer to the next element struct node{
char name[20]; // Name of up to 20 letters
The pointer is a null pointer for the last element. int age;
head float height; // In metres
node *next; // Pointer to next node
};
struct node *header = NULL;
May be a special node at the beginning, known as the
header node 17 18

3
Adding an element at the beginning Adding an element at the end

• Create a new node;


struct node *newnode Create a new node;
newnode = new node;
Pointer from the last node points to new node;
• fill in the details
Create(newnode);
newnode -> name = // store the value of the name field
newnode -> age = // store the value of the age field
newnode -> height= // store the value of the heightfield Create(newnode);
newnode ->next = NULL
• if the list is empty to start with, last.next= newnode;
if (header== NULL) header = newnode ;
• Else Pointer from the newnode points to head;
newnode ->next= header; How do we find the last node?
header= newnode; Soln: step through the list until it finds the last node.

O(1); 19 20

How to find the last node? Adding an element at the end


last Last newnode
Head Head

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.

Adding an element at the end Displaying the list of nodes


void add_node_at_end () {
node *temp, *temp2; // Temporary pointers
// Set up link to this node • Method
if (head == NULL)
// Reserve space for new node and fill it with data head = temp;
1. Set a temporary pointer to point to the head
temp = new node; else { 2. If the pointer points to NULL, display the message "End of list" and
cout << "Please enter the name of the person: "; temp2 = head ; stop.
cin >> temp->name; // We know this is not NULL - list not empty! 3. Otherwise, display the details of the node pointed to by the head
cout << "Please enter the age of the person : "; while (temp2->nxt != NULL) pointer.
cin >> temp->age; {
cout << "Please enter the height of the person : "; 4. Make the temporary pointer point to the same thing as the nxt
temp2 = temp2->nxt;
cin >> temp->height; pointer of the node it is currently indicating.
// Move to next link in chain
temp->nxt = NULL; } 5. Jump back to step 2.
temp2->nxt = temp;
}
} // add node at end

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

Accessing an element Delete a node after the current position

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

• When it comes to delete nodes, we have three choices:


Create new node; – Delete a node from the start of the list,
– Delete one from the end of the list, or
Access (k-1)th element;
– Delete at the kth position
Pointer from new node points to kth element;
Pointer from (k-1)th element points to kth element;

Create(newnode); Complexity: O(k)


Acess (k-1)th node;
newnode.next® (k-1)node.next;
(k-1)node.next® newnode; 31 32

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

Deleting the last node Deleting the last node (1)


head
• Steps:
the list
1. Look at the head 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 head pointer is pointing to.
head
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 otherwise go to the set the pointer temp1 to
next step. the same as the head
4. Make another pointer, temp2, point to the current node in the list. pointer:

5. Make temp1 point to the next item in the list.


6. Go to step 3. head
7. Delete the node pointed by temp1.
8. Mark the nxt pointer of the node pointed by temp2 as NULL - it is set the pointer temp2
the new last node. to the same node as
temp1
35 36

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

temp1 is made to head


point to the next node set the nxt pointer of what
along temp2 indicates to NULL
37 38

Deleting the last node (4) Delete at the kth position


Sample code

void delete_end_node() Access the k-1th node;


{ else {
while (temp1->nxt != NULL) {
Deletenode = (k-1)thnode.next;
node *temp1, *temp2;
if (head == NULL) temp2 = temp1; (k-1)thnode.next = Deletenode.next;
cout << "The list is empty!" << endl; temp1 = temp1->nxt;
else { } Delete (Deletenode);
temp1 = head; delete temp1;
if (temp1->nxt == NULL) temp2->nxt = NULL;
Complexity depends on access complexity;
{ }
delete temp1; }
head = NULL; } // delete end of node O(1) for deleting first element;
}
O(1) or O(n) for deleting the last element;
39 40
O(k) for any other element;

Advantage of Linked List

Need not know the maximum number of elements ahead


of time.

41

You might also like