4 - 5-Pointer and Linked List
4 - 5-Pointer and Linked List
CHAPTER 4+5:
POINTER AND LINKED LISTS
Main contents
Pointer
Singly Linked Lists
Doubly Linked Lists
Circular Linked Lists
Linked Stack, Linked Queue (Chapter 6)
Pointer
Variables in memory:
char c = “C”;
int a = 50;
float f = 3.45;
Pointer
• Variables in programming languages include the value
of the variable and the memory address of the variable,
• Definition: A pointer is a memory address of another
variable.
• A pointer variable is a variable that its value is the
address of another variable (the variable stores the
memory address of another variable),
• A pointer is a variable. Thus it needs to be declared
before using
Pointer
Declaration:
Data_type *name_of_pointer_var;
where:
- Data_type is the basic type of a pointer,
- name_of_pointer_var is the
- Notation “*” is used in pointer variable declaration.
Examples:
int *amount; //point to the address of an integer variable
float *GPA; //point to the address of a float variable
Pointer
Invalid
Pointer
NULL Pointer
• A null pointer is a pointer value that points to no valid location
int *p = NULL;
• A null pointer is a constant pointer that is compatible with any
pointer
• A null pointer value is an address that is different from any valid
pointer
• Dereferencing a null pointer is meaningless, typically resulting
in a run-time error.
• In dynamic memory allocation in C, when calloc() or malloc()
function fails to allocate memory block, they return NULL
Pointer
void Pointer
• A void pointer is a pointer, which may store the address of
any type of variable.
• The void pointer is also known as a type-less pointer or
generic pointer
• The indirection operator cannot be used with a void pointer.
• Void pointer allows violating the basic rule of the type
conversion of the pointer.
a datum
a link
A node
Singly Linked Lists (continued)
• The nodes in this list are objects created from the following definition
struct IntSLLNode {
int info;
IntSLLNode *next;
};
Singly Linked Lists - Advantages
• Linked list are dynamic data structures.
• Storage need not be contiguous.
• Efficient memory utilization. Here memory is not pre-
allocated. Memory is allocated whenever it is required.
• Insertion or deletion is easy and efficient (may be done
very fast, in a constant amount of times, independent of
the size of the list).
• The joining of two linked lists can be done by assigning
pointer of the second linked list in the last node of the
first linked list
Singly Linked Lists - Disadvantages
• The longer the list, the longer the chain of next pointers
that need to be followed to a given node
• This reduces flexibility, and is prone to errors
• An alternative is to use an additional pointer to the end of
the list
Singly Linked Lists
Operations on singly linked list
Singly Linked Lists
Create a list
//Define a generic type T
typedef int T;
// Define a node in the list
struct Node {
T data; // datum
Node * next; // link
};
// Define a list
struct List {
Node * head; // The pointer of the head
};
// Create an empty list
void listInit(List & list) {
list.head = NULL;
};
Singly Linked Lists
Check empty
A new node (v) is created with the link pointing to the head of list
first
node
v.next =
list.head;
pos -1 pos
Head
NULL
Singly Linked Lists
Example 3
head = head->next
39
prev->next=NULL
Step 1: If head points to NULL, return. The linked list is empty – there is no node.
Step 2: If the first node points to NULL – there is only one node in the linked list, delete the
first node and assign NULL to head.
Step 3: Point prev to first node and cur to second node.
Step 4: Keep moving prev and cur (prev = prev->next, cur = cur->next) until cur->next is NULL.
Step 5: If cur->next is NULL, set prev->next equals to NULL and delete cur.
Singly Linked Lists
Deletion a node at the middle of a list
pos=1 p temp = p->next
p->next = p->next->next
void *deleteAt(List &L, int pos){
node *p = L.head;
for (int i = 0; i < pos-1; i++){ p = p->next; }
node *temp = p->next;//the position of the node that needs to be deleted
p->next = p->next->next; //ignore the node in position pos
delete(temp); //release the memory of deleted node
}
Singly Linked Lists
Example
typedef int T;
// define a node
struct DNode {
T data; // date
DNode * next; // point to the next node
DNode * prev; // point to previous node
};
// Define a doubly linked list
struct DList {
DNode * header; // point to the beginning node (Pseudo node)
DNode * trailer; // point to ending node (Pseudo node)
DNode * currentPos; // point to current node
};
Doubly Linked Lists (continued)
(v->prev )->next = u
u->next = v
w->prev = u;
u->next = w;
w->prev = u;
delete v;
L.currentPos = w;
}
Circular Lists
typedef int T;
struct CNode {
T data; // Datum
CNode * next; // Link
};
struct CList {
CNode * cursor;
};
Circular Lists
• Add a node to right after the cursor
void clistInsert(DList & L, T e) {
CNode * v = new CNode;
v->data= e;
if (L.cursor == NULL) { //empty list
v->next = v;
L.cursor = v;
}
else {//non-empty list
v->next = L.cursor->next;
L.cursor->next = v;
}
}
Circular Lists
• Delete a node right after the cursor
void clistRemove(CList & L) {
// Keep the address of the node
CNode * old = L.cursor->next;
if (old == cursor) // The list has unique node
cursor = NULL;
else
cursor->next = old->next;
delete old;
}
Summary
• A linked list is a linear ordered collection of finite
homogeneous data elements called node.
• The linked list overcomes the demerits of array
in terms of memory allocation.
• Linked list can be classified into 4 types single
linked list, circular linked list, doubly linked list
and circular doubly linked list.
• Polynomial addition can be done using linked
list.
• Addition, Deletion and Insertion of a node in a
linked list is faster than array.
Exercises
1. Write an algorithm to insert a data X after a specific data
item Y in the linked list.
2. Write a function to delete the nth node of a singly linked list.
The error conditions are to be handled properly.
3. Write an algorithm to delete any node from the singly liked
list where the key value of the node is known.
4. Write an algorithm to delete a node from a doubly linked list.
5. Write an algorithm to delete all nodes having greater than a
given value, from a given singly linked list.
6. Write a program to INSERT and DELETE node in a circularly
doubly linked list. Display the elements of the list.
Excercises
I. Write a program with the function as below
1. Define a singly linked list with the data in each node is a
float
2. Insert some nodes to the list
3. Enter a float X and find the position of X in the list.
4. Print all the elements in the list
Let the users select the functions by using the below menu:
Excercises
II. Write a program with the function as below
To manage the information of students, a struct is defined as
below:
struct sinhvien{
string maSV;
string hotenSV;
string lop;
double diem;
};
Excercises
Define a singly linked list with the data in each node is the
information of a student and write the program to perform
following functions:
1. Insert some nodes to the list (use while loop)
2. Count the number of nodes in the list.
3. Enter a Student ID and print all the information of this
student in the list (if exists).