Professional Documents
Culture Documents
Chapter 4 - Linked List
Chapter 4 - Linked List
LINKED LIST
Concept
Basic Operations
Implementation of linked-List using
Pointer
1
4.1 Concept
data link
2
4.1 Concept – cont
A linked list:
3
4.1 Concept – cont
4
4.2 Implementation
x = new int;
5
malloc returns the address of the first byte in
the memory space allocated – based on data
numbers & available allocated memory.
In one list, there are 3 important pointers:
• head – refer to first node in list.
• cur – refer to current node.
• prev – refer to the previous node before current
node.
6
4.3 Basic Operation
6 basic operation:
1. Create list – allocates the head structure.
2. Create new node – allocate space for node and
initializes the data for the node.
3. Insert new node – adds node to a list.
4. Delete node – removes node from list by changing
various link pointers & then physically deleting the node
from list.
5. Retrieve – uses search node to locate the data in the
list.
6. Traversal – traverse a list start at the first node and
examine each node in succession until the last node
has been processed.
7
4.4 Linked List using Pointer
E.g.: Define a linked list structure called Student that stores name of
type string, studentID of type of integer, CGPA of type of floating point
number and pointer to the next record in the structure which is called
link.
Solution 1:
struct Student
{
char name[20];
int studentID;
float CGPA;
struct Student *link;
};
8
4.4 Linked List using Pointer
Solution 2:
struct Student
struct node
{
{
char name[20];
struct Student Info;
int studentID;
struct Student *link;
float CGPA;
};
};
9
4.4 Linked List using Pointer
struct Student
{
char name[20];
int studentID;
float CGPA;
struct Student *link;
};
10
first last newNode
Student *first;
Student *last;
Student *newNode;
11
first last newNode
first = NULL;
12
first last newNode
newNode = new Student;
13
first last newNode
cout<<“Enter Student name:”;
cin.getline(newNode->name,20);
cout<<“Enter Student ID:”;
Aliya 1111 3.25
cin>>newNode->studentID;
name studentID CGPA link cout<<“Enter Student CGPA:”;
cin>>newNode->CGPA;
newNode->link = NULL;
14
first last newNode
first = newNode;
last = newNode;
Aliya 1111 3.25
15
first last
newNode = new Student;
16
first last
cout<<“Enter Student name:”;
cin.getline(newNode->name,20);
cout<<“Enter Student ID:”;
newNode
Aliya 1111 3.25
cin>>newNode->studentID;
name studentID CGPA link cout<<“Enter Student CGPA:”;
cin>>newNode->CGPA;
John 1112 2.89 newNode->link = NULL;
name studentID CGPA link
17
first last
last->link = newNode;
18
first
last = newNode;
19
first
newNode = new Student;
20
first
Last ->link = newNode
21
first
newNode last
name studentID CGPA link
22
first
last
name studentID CGPA link
newNode
name studentID CGPA link
23
first
last
name studentID CGPA link
newNode
name studentID CGPA link
24
first
newNode last
name studentID CGPA link
25
first current
Student *current;
newNode last
name studentID CGPA link
26
first current
current = first;
newNode last
name studentID CGPA link
27
first
current = current->link;
current
name studentID CGPA link
newNode last
name studentID CGPA link
28
first
current = current->link;
current
name studentID CGPA link
newNode last
name studentID CGPA link
29
first
current = current->link;
newNode last
name studentID CGPA link
30