Download presentation
Presentation is loading. Please wait.
1
Data Structure Dr. Mohamed Khafagy
2
Review of Sequential Representations
Previously introduced data structures, including array, queue, and stack, they all have the property that successive nodes of data object were stored a fixed distance apart. The drawback of sequential mapping for ordered lists is that operations such as insertion and deletion become expensive. Also sequential representation tends to have less space efficiency when handling multiple various sizes of ordered lists.
3
Linked List A better solutions to resolve the aforementioned issues of sequential representations is linked lists. Elements in a linked list are not stored in sequential in memory. Instead, they are stored all over the memory. They form a list by recording the address of next element for each element in the list. Therefore, the list is linked together. A linked list has a head pointer that points to the first element of the list. By following the links, you can traverse the linked list and visit each element in the list one by one.
4
Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node next node elem A B C D
5
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next
6
Layout of L = (a,b,c,d,e) using an array representation.
Memory Layout Layout of L = (a,b,c,d,e) using an array representation. a b c d e The figures show computer memory. An array uses a contiguous chunk of memory. A linked representation uses an arbitrary layout. c a e d b
7
Linked Representation
pointer (or link) in e is NULL c a e d b first use a variable first to get to the first element a
8
Normal Way To Draw A Linked List
b c d e NULL first link or pointer field of node data field of node
9
Chain first NULL a b c d e A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a NULL (or 0) pointer.
10
Node Representation template <class T> class ChainNode {
private: T data; ChainNode<T> *link; // constructors come here }; link data Better to define as a struct than a class as otherwise need to make every using class a friend so that node fields may be accessed.
11
Insertion To insert a new data value into a linked list, we must first obtain a new node and store the value in its data part The second step is to connect this new node to existing list Two cases in this situation: (1) insertion after some element in the list and (2) insertion at the beginning of the list
12
Inserting at the Head Allocate a new node update new element
Have new node point to old head Update head to point to new node
13
predptr points to the node containing 17
Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr
14
Insertion Note: insertion also works at end of list
pointer member of new node set to null Insertion at the beginning of the list predptr must be set to first pointer member of newptr set to that value (Where first points to) first set to value of newptr In all cases, no shifting of list elements is required !
15
Insert X immediately after current position
b c d current Insert X immediately after current position a b c d x current
16
Insertion immediately after current position
// create a new node tmp = new ListNode<DataType>(); a b current tmp
17
Implementing Insertion: Step By Step
Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; a b x current tmp
18
Implementing Insertion: Step By Step
Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; // x’s next node is b tmp.next = current.next; a b x current tmp
19
Implementing Insertion: Step By Step
Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; // x’s next node is b tmp.next = current.next; // a’s next node is x current.next = tmp; a b x current tmp
20
Inserting at the Tail Allocate a new node Insert new element
Have new node point to null Have old last node point to new node Update tail to point to new node
21
Deletion For deletion, there are also two cases to consider:
Deleting an element that has a predecessor Delete the first element in the list
22
Removing at the Head Update head to point to next node in the list
Allow garbage collector to reclaim the former first node
23
Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant- time way to update the tail to point to the previous node
24
Deletion Delete node containing 22 from list. Do a bypass operation:
predptr ptr To free space Delete node containing 22 from list. Suppose ptr points to the node to be deleted predptr points to its predecessor (the 17) Do a bypass operation: Set the next pointer in the predecessor to point to the successor of the node to be deleted Deallocate the node being deleted.
25
Deletion The second case is easier
Just set the first points to the second node in the list and then returning the deleted node to the storage pool
26
Implementing Basic Deletion
Delete an item immediately after current position Basic deletion is a bypass in the linked list. a x b current a b current
27
Implementing Basic Deletion
Need a reference to node prior to the one to be deleted. current.next = current.next.next; a x b current a x b current a b current
28
The Scenario Begin with an existing linked list Could be empty or not
head 6 42 4 17 Begin with an existing linked list Could be empty or not Could be ordered or not
29
The Scenario Begin with an existing linked list Could be empty or not
head 6 42 4 17 Begin with an existing linked list Could be empty or not Could be ordered or not
30
The Scenario Begin with an existing linked list Could be empty or not
head 42 4 17 Begin with an existing linked list Could be empty or not Could be ordered or not
31
The Scenario Begin with an existing linked list Could be empty or not
head 42 4 17 Begin with an existing linked list Could be empty or not Could be ordered or not
32
Finding the Match We’ll examine three situations:
Delete the first element Delete the first occurrence of an element Delete all occurrences of a particular element
33
Deleting the First Element
This can be done without any traversal/searching Requires an in/out pointer procedure DeleteFront (current iot in/out ptr toa Node) // deletes the first node in the list if (current <> nil) then current <- current^.next endif endprocedure
34
Deleting the First Element
This can be done without any traversal/searching Requires an in/out pointer procedure DeleteFront (current iot in/out ptr toa Node) // deletes the first node in the list if (current <> nil) then current <- current^.next endif endprocedure
35
Deleting from a Linked List
Deletion from a linked list involves two steps: Find a match to the element to be deleted (traverse until nil or found) Perform the action to delete Performing the deletion is trivial: current <- current^.next This removes the element, since nothing will point to the node.
36
head 6 42 4 17 . Delete(head, 4)
37
head 6 42 4 17
38
head 6 42 4 17
39
head 6 42 4 17
40
head 6 42 4 17
41
head 6 42 4 17
42
head 6 42 4 17
43
head 6 42 4 17
44
head 6 42 4 17
45
head 6 42 4 17
46
head 6 42 17
47
Deleting All Occurrences
Deleting all occurrences is a little more difficult. Traverse the entire list and don’t stop until you reach nil. If you delete, recurse on current If you don’t delete, recurse on current^.next
48
Applications of Linked Lists
Stacks and Queues Implemented with Linked Lists Polynomials Implemented with Linked Lists Remember the array based implementation? Hint: two strategies, one efficient in terms of space, one in terms of running time
49
Linked Stacks and Queues
top front rear Linked Queue Linked Stack
50
Stack with a Singly Linked List
We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time nodes t elements
51
Queue with a Singly Linked List
We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(1) time r nodes f elements
52
Array-Based Implementation of Linked Lists
Given a list with names Implementation would look like this
53
How to do it??? First of all, define a 2D array int array[10][2];
54
How to do it??? Constructor( )
we make “first” variable equals to 7 to indicate we start with position 7 first ==7; How to choose a start point??
55
How to do it??? Put 88 into data position and set next position to NULL array[first][0]=88; array[first][1]=-1; size==1;
56
How to do Insertion??? Find a empty position (in this example it’s node 1) Insert(array, 1) { ptr==1; // find a empty position pre_ptr==7; // you need a function to find pre_ptr array[new_node][0]==54; array[new_node][1]==array[pre_ptr][1]; array[pre_ptr][1]==ptr; size++; }
57
Insertion Insertion predptr points to the node containing 17
To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr
58
How to do Deletion???
59
Implementation Details
For Insertion, there are also two cases to consider: insertion after some element in the list and insertion at the beginning of the list For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list
60
Polynomials Representation typedef struct poly_node *poly_pointer;
int coef; int expon; poly_pointer next; }; poly_pointer a, b, c; coef expon link
61
Example a null b -3 10 null
62
Adding Polynomials 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14
2 8 1 0 a -3 10 b 11 14 a->expon == b->expon d 2 8 1 0 a -3 10 b 11 14 -3 10 a->expon < b->expon d
63
Adding Polynomials (cont’d)
2 8 1 0 a -3 10 b 11 14 -3 10 2 8 d a->expon > b->expon
64
Adding Polynomials (cont’d)
poly_pointer padd(poly_pointer a, poly_pointer b) { poly_pointer front, rear, temp; int sum; rear =(poly_pointer)malloc(sizeof(poly_node)); if (IS_FULL(rear)) { fprintf(stderr, “The memory is full\n”); exit(1); } front = rear; while (a && b) { switch (COMPARE(a->expon, b->expon)) {
65
case -1: /* a->expon < b->expon */
attach(b->coef, b->expon, &rear); b= b->next; break; case 0: /* a->expon == b->expon */ sum = a->coef + b->coef; if (sum) attach(sum,a->expon,&rear); a = a->next; b = b->next; case 1: /* a->expon > b->expon */ attach(a->coef, a->expon, &rear); a = a->next; } for (; a; a = a->next) for (; b; b=b->next) rear->next = NULL; temp = front; front = front->next; free(temp); return front;
66
Doubly Linked List Keep a pointer to the next and the previous element in the list typedef struct node *pnode; typedef struct node { char data [4]; pnode next; pnode prev; }
67
Doubly Linked List Keep a header and trailer pointers (sentinels) with no content header.prev = null; header.next = first element trailer.next = null; trailer.prev = last element Update pointers for every operation performed on the list How to remove an element from the tail of the list ?
68
Doubly Linked List – removeLast()
Running time? How does this compare to simply linked lists?
69
Doubly Linked List insertFirst swapElements
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.