You are on page 1of 12

LINK LIST

SUBJECT : DATA STRUCTURE


LECTURER : MADAM UMI KALSUM
HASSAN

1
OUTLINES

Introduction
Differences between Link List and
Array
Link List Declaration
Basic Link List Operation
Circular Link List
Double Link List
Exercises

2
Differences betweenLink List Basic Link Circular
ntroduction Double Link List
Exercises
Link List and Array
Declaration
List Operation
Link List

LINK LIST
Link List is an ordered collection of elements called
nodes which has two parts.
The nodes connect by pointer.

Head Data Pointer Data Pointer Data \

Information part : store element of


If no next node, the
the list
Address part : pointer that pointer contains a
indicates the location of NULL value
next node

3
Differences betweenLink List Basic Link Circular
ntroduction Double Link List
Exercises
Link List and Array
Declaration
List Operation
Link List

ARRAY VS. LINK LIST


Aspect Array Link List
Size Fixed number. Grow and contract since
Size need to be specific of insertions and
during declaration. deletions.
Maximum size depends
on heap.
Storage Static: Its location is Dynamic: Its node is
capacity allocated during compile located during run time.
time.
Order and Stored consecutively. Stored randomly.
sorting
Accessing the Direct or random access Sequential access
element method. method.
Specify the array index Traverse starting from
or subscript. the first node in the list
by pointer.
4
Searching Binary search and linear Linear search
Differences betweenLink List Basic Link Circular
ntroduction Double Link List
Exercises
Link List and Array
Declaration
List Operation
Link List

LINK LIST DECLARATION


Syntax: Example 1:
struct nodename struct Node
{ variable { int Data; // to store integer
declarations; value;
nodename *next; Node *Next; // a pointer to
}; next node;
Example 2: A link list node, STUDNODE stores an ID of a
};
student (4 digit characters), name of a student (20
characters) and a floating point CGPA and a pointer to
the next node. Write the declaration of the node.

struct STUDNODE
{ char ID [4];
char name [20];
float CGPA;
STUDNODE * Next;
};
5
Differences betweenLink List Basic Link Circular
ntroduction Double Link List
Exercises
LINK LIST EXTERNAL
Link List and Array
Declaration
List Operation
Link List

POINTER
External pointers are needed in a link list for the
following purposes:
1) Pointer to hold the list by pointing to the first
node in list. (usually declared as Head, First,
Front, Hptr and etc)
2) Pointer to traverse/ visit the nodes in list.
(usually declared as Current, Curr, CPtr,
Currptr and etc)
3) Pointer to hold new node. (usually declared as
NewPtr, Nptr and etc)
4) Pointer to mark the location of the node to be
declared or inserted. (usually declared as
Temp, Tempptr and etc)

6
Differences betweenLink List Basic Link Circular
ntroduction Double Link List
Exercises
LINK LIST EXTERNAL
Link List and Array
Declaration
List Operation
Link List

Syntax:
POINTER
Nodename *pointer1,*pointer2,..,*pointerN;

Example 1:
struct Node
{ int Data; //to store integer value
Node *Next; //a pointer to next node
};
Node *Head, *Current, *NewNode;

Example 2:
struct STUDNODE
{ char ID [4];
char Name [20];
float CGPA;
STUDNODE * Next;
};
STUDNODE *Firstptr, *CurrPtr, *NewPtr; 7
Differences betweenLink List Basic Link Circular
ntroduction Double Link List
Exercises
Link List and Array
Declaration
List Operation
Link List

BASIC LINK LIST OPERATION


1) Create a dynamic node
2) Create a list
3) Traversing a list
4) Insert node
5) Delete node
To hold the list
Example :
struct NumNode To traverse the list
{ int value;
NumNode * Next; To mark a node in the list
};
NumNode *head, *current, *temp, *Nptr;
head=NULL; //the list is empty

To allocate new node

8
Differences betweenLink List Basic Link Circular
ntroduction Double Link List
Exercises
Link List and Array
Declaration
List Operation
Link List

CREATE A DYNAMIC NODE


Using new operator.
Example : 10
Nptr = new NumNode Nptr NumNode
cout<<Enter a number: ;
cin>>Nptr->value;
Nptr->Next=NULL;

DELETE A DYNAMIC NODE


Using delete operator. 10
delete Nptr; //delete the node pointed
Nptr
by
NumNode
NptrNptr=Null; //set the pointer to Null
If the link list more than one node: Need to change
pointer of record before the one being deleted.
5 10 9
Nptr NumNode
9
Differences betweenLink List Basic Link Circular
ntroduction Double Link List
Exercises
Link List and Array
Declaration
List Operation
Link List

CREATE A LINK LIST


Using new operator.
Example :
// create a node 10
Nptr = new NumNode
Nptr NumNode
cout<<Enter a number:
;
head
cin>>Nptr->value;
Nptr->Next=NULL;

//test if the list empty


if (head==NULL)
head=Nptr; //head
pointer to
//the new
node

10
Differences betweenLink List Basic Link Circular
ntroduction Double Link List
Exercises
Link List and Array
Declaration
List Operation
Link List

TRAVERSING A LINK LIST


2 purpose:
1) To process every node in the list.
Example : Traverse a list to sum all nodes, count
the number of the node and calculate the average
of nodes in the list.

sum = 0;
count = 0;
current = head; // point the first node in the list
while (current != NULL)
{ sum sum+current->data; // sum node
count++; // count the node
current = current->Next; // move to next node
}
average = sum/count; // calculate average

2) To find the last node in the list.

while (current->Next != NULL) 11


Differences betweenLink List Basic Link Circular
ntroduction Double Link List
Exercises
INSERT NODE AT THE
Link List and Array
Declaration
List Operation
Link List

BEGINNING OF THE LIST


//create a new list
Nptr=new NumNode;
cout<<Enter a new number: ;
cin>>Nptr->value;
Nptr-Next=NULL;
//test if the list is empty
if (head==NULL)
head=Nptr; //head pointer points to the new
node
else //head pointer points to new node
{ Nptr->Next=head;
head=Nptr;
Process } Result
10 15 10
head head
head=Nptr Nptr->Next=Head

15
Nptr
Nptr 12

You might also like