week07_LinkedList
week07_LinkedList
AND ALGORITHMS
CONTENTS
1. Basic concepts
3
OBJECTIVES CONTENTS
7 8
1. BASIC CONCEPTS 1. BASIC CONCEPTS
1.1. Pointer 1.1. Pointer
Address in memory cell 0x13AB
Declare: int *p; Example
• Declare a pointer to point to an integer 0x56CD
variable Print value: 22
Print value : 2
Basic variable
Source: https://www.programiz.com/c-programming
9 10
typedef struct TNode{ TNode* q: q is a pointer that points to a variable of type TNode
int a; Qa: access to member a of the struct type
double b;
char* s; q = (TNode*)malloc(sizeof(TNode)): allocate memory for a TNode type
}TNode; and q points to the allocated memory area
11 12
CONTENTS 2. SINGLY LINKED LIST
2.1. Introduction
1. Basic concepts Singly linked list is an ordered list of elements; Elements are connected to each other
1.1. Pointer through a link.
Linked list and array:
1.2. Struct
3. Operations on linked list Data structure type Don’t need to be the same Must be the same
13 14
15 16
3. OPERATIONS ON SINGLY LINKED LIST 3. OPERATIONS ON SINGLY LINKED LIST
3.1. Browse the list 3.1.Browse the list
Task: Visit each element of the list exactly once Task: Visit each element of the list exactly once
Idea: Use the next pointer to access the next element
head
5 9 3 ... 6 4
17 18
For example, find the first element of the list with value 3
head
5 9 3 ... 3 4
19 20
SUMMARY AND SUGGESTIONS CONTENTS
1. Summary:
1. Insert an element at the beginning of the list
The lesson introduced singly linked lists and two basic operations on singly linked lists:
browse and search 2. Insert an element at the end of the list
2. Suggestions: 3. Insert an element before an element in the list
Design and implement other operations on the list
21
OBJECTIVES CONTENTS
24
1. INSERT AN ELEMENT AT THE BEGINNING OF THE LIST 1. INSERT AN ELEMENT AT THE BEGINNING OF THE LIST
1.1. Idea 1.2. Implement
head
Create new element
5 9 3 ... 6 4
If the current list is empty, returns
the new element
10
New element (1) Update head pointer to point
to the new element
Step 1: Create a new element (2) Update the next pointer of the
Step 2: Update head pointer to point to the new element new element: makes it points to
Step 3: Update the next pointer of the new element: makes it points to the the beginning of the old list,
beginning of the old list, thus the new element becomes the first thus the new element becomes
element of the list the first element of the list
25
10
New element
Step 1: Create a new element
Step 2: Find the last element of the list
Step 3: Update the next pointer of the last element to point to the new
element
27 28
2. INSERT AN ELEMENT AT THE END OF THE LIST CONTENTS
2.2. Implement
1. Insert an element at the beginning of the list
Use a loop to find the last 2. Insert an element at the end of the list
element of a list
3. Insert an element before an element in the list
29 30
3. INSERT AN ELEMENT BEFORE AN ELEMENT OF THE LIST 3. INSERT AN ELEMENT BEFORE AN ELEMENT OF THE LIST
3.1. Idea 3.1. Implement
head
Find the element preceding a
X given node
5 9 3 ... 6 4
10
Insert anew element before the element X of the list: New element
Step 1: Create a new element
Step 2: Update the next pointer of the element before X as the new element Insertion
Step 3: Update the next pointer of new element as the X
31 32
SUMMARY AND SUGGESTIONS CONTENTS
2. Suggestions:
33
OBJECTIVES CONTENTS
After this lesson, students can:
Understand the algorithm and successfully implement two basic operations on singly 1. Delete an element of the list
36
1. DELETE AN ELEMENT OF THE LIST 1. DELETE AN ELEMENT OF THE LIST
1.1. Idea 1.2. Implement
If the list and element to be deleted
p are NULL, return head of the list
p
head
head
Output: 4 6 ... 3 9 5
39 40
2. REVERSE THE ORDER OF THE LIST ELEMENTS 2. REVERSE THE ORDER OF THE LIST ELEMENTS
2.2. Idea 2.3. Implement
Nguồn: https://www.geeksforgeeks.org/reverse-a-linked-list/
41 42
1. Summary:
Implement two important operations on singly linked lists: remove an element from
the list and reverse the order of the list's elements.
43 44