You are on page 1of 30

CHAPTER 4:

LINKED LIST

Concept
Basic Operations
Implementation of linked-List using
Pointer

1
4.1 Concept

 An ordered collection of data in which each


element contains the location of the next
element or elements using pointers.
 A dynamic data structure where size can be
changed.
 Each element / node contains two fields:
• Data field – stored data item.
• Link / pointer – contains the address of the next
element in the list.

data link

2
4.1 Concept – cont

 A linked list:

List Node 1 Node 2 Node 3


 Node 1 is linked with Node 2 using pointer as well as
Node 2 and Node 3.
 For Node 3, the pointer in this last node of the list
does not point to anything, it is given the NULL
(empty) value.

3
4.1 Concept – cont

 Linked list structures usually save space,


time and programming effort.
• The structures are large or need to hold large
records.
• The size of the list is not known in advance.
• Flexibility is needed during insertions,
deletions and reordering of elements.

4
4.2 Implementation

 Linked list structures can be implemented using


Pointer.
 Pointer is referring to assign sufficient memory
dynamically using malloc( ) function or new.
x = (int*) malloc (sizeof (int));

x = new int;

 The allocated memory is not initialized.


 The size specification in malloc’s actual parameter is
generally computed using sizeof operator.

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;
};

name studentID CGPA link

10
first last newNode
Student *first;
Student *last;
Student *newNode;

11
first last newNode
first = NULL;

12
first last newNode
newNode = new Student;

name studentID CGPA link

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

name studentID CGPA link

15
first last
newNode = new Student;

Aliya 1111 3.25 newNode


name studentID CGPA link

name studentID CGPA link

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;

Aliya 1111 3.25 newNode


name studentID CGPA link

John 1112 2.89

name studentID CGPA link

18
first
last = newNode;

Aliya 1111 3.25 newNode last


name studentID CGPA link

John 1112 2.89

name studentID CGPA link

19
first
newNode = new Student;

Aliya 1111 3.25 last


name studentID CGPA link

John 1112 2.89 newNode


name studentID CGPA link

name studentID CGPA link

20
first
Last ->link = newNode

Aliya 1111 3.25 last


name studentID CGPA link

John 1112 2.89 newNode


name studentID CGPA link

name studentID CGPA link

21
first

name studentID CGPA link

newNode last
name studentID CGPA link

name studentID CGPA link

22
first

name studentID CGPA link

last
name studentID CGPA link

newNode
name studentID CGPA link

name studentID CGPA link

23
first

name studentID CGPA link

last
name studentID CGPA link

newNode
name studentID CGPA link

name studentID CGPA link

24
first

name studentID CGPA link

name studentID CGPA link

newNode last
name studentID CGPA link

name studentID CGPA link

25
first current
Student *current;

name studentID CGPA link

name studentID CGPA link

newNode last
name studentID CGPA link

name studentID CGPA link

26
first current
current = first;

name studentID CGPA link

name studentID CGPA link

newNode last
name studentID CGPA link

name studentID CGPA link

27
first
current = current->link;

current
name studentID CGPA link

name studentID CGPA link

newNode last
name studentID CGPA link

name studentID CGPA link

28
first
current = current->link;

name studentID CGPA link

current
name studentID CGPA link

newNode last
name studentID CGPA link

name studentID CGPA link

29
first
current = current->link;

name studentID CGPA link

name studentID CGPA link current

newNode last
name studentID CGPA link

name studentID CGPA link

30

You might also like