LINK LIST SUBJECT :  DATA STRUCTURE LECTURER : MADAM UMI KALSUM HASSAN
Introduction Differences between Link List and Array Link List Declaration Basic Link List Operation Circular Link List Double Link List Exercises OUTLINES
Link List  is an ordered collection of elements called nodes which has two parts.  The nodes connect by pointer.  LINK LIST Basic Link  List Operation Circular  Link List Data Pointer Information part : store element of the list Address part : pointer that indicates the location of next node Data Pointer Data \ Head If no next node, the pointer contains a NULL value Introduction Link List  Declaration Double Link List Exercises Differences between  Link List and Array
ARRAY VS. LINK LIST Basic Link  List Operation Circular  Link List Introduction Link List  Declaration Double Link List Exercises Differences between  Link List and Array Aspect Array Link List Size Fixed number. Size need to be specific during declaration. Grow and contract since of insertions and deletions. Maximum size depends on heap. Storage capacity Static: It’s location is allocated during compile time. Dynamic: It’s node is located during run time. Order and sorting Stored consecutively. Stored randomly. Accessing the element Direct or random access method. Specify the array index or subscript. Sequential access method. Traverse starting from the first node in the list by pointer. Searching Binary search and linear search Linear search
LINK LIST DECLARATION Basic Link  List Operation Circular  Link List Syntax: struct nodename { variable declarations; nodename *next; }; Example 1: struct Node {  int Data; // to store integer value;   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; }; Introduction Link List  Declaration Double Link List Exercises Differences between  Link List and Array
LINK LIST EXTERNAL POINTER Basic Link  List Operation Circular  Link List External pointers  are needed in a link list for the following purposes: Pointer to  hold  the list by  pointing to the first node  in list. ( usually declared as Head, First, Front, Hptr and etc ) Pointer to  traverse/ visit  the nodes in list. ( usually declared as Current, Curr, CPtr, Currptr and etc ) Pointer to  hold new node . ( usually declared as NewPtr, Nptr and etc ) Pointer to  mark the location  of the node to be  declared or inserted . ( usually declared as Temp, Tempptr and etc ) Introduction Link List  Declaration Double Link List Exercises Differences between  Link List and Array
LINK LIST EXTERNAL POINTER Basic Link  List Operation Circular  Link List Syntax: 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; Introduction Link List  Declaration Double Link List Exercises Differences between  Link List and Array
BASIC LINK LIST OPERATION Basic Link  List Operation Circular  Link List Create a dynamic node Create a list Traversing a list Insert node Delete node Example : struct NumNode {  int value;   NumNode * Next; }; NumNode *head, *current, *temp, *Nptr; head=NULL; //the list is empty To hold the list To traverse the list To mark a node in the list To allocate new node Introduction Link List  Declaration Double Link List Exercises Differences between  Link List and Array
CREATE A DYNAMIC NODE Basic Link  List Operation Circular  Link List Using  new  operator. Example : Nptr = new NumNode cout<<“Enter a number: “; cin>>Nptr->value; Nptr->Next=NULL; Nptr 10 NumNode DELETE A DYNAMIC NODE Using  delete  operator. delete Nptr; // delete the node pointed by 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 . Nptr 10 NumNode Nptr 5 10 NumNode 9 Introduction Link List  Declaration Double Link List Exercises Differences between  Link List and Array
CREATE A LINK LIST Basic Link  List Operation Circular  Link List Using  new  operator. Example : // create a node Nptr = new NumNode cout<<“Enter a number: “; cin>>Nptr->value; Nptr->Next=NULL; //test if the list empty if (head==NULL) head=Nptr; //head pointer to  //the new node Nptr 10 NumNode head Introduction Link List  Declaration Double Link List Exercises Differences between  Link List and Array
TRAVERSING A LINK LIST Basic Link  List Operation Circular  Link List 2 purpose: 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 To find the last node in the list. while (current->Next != NULL)   current=current->Next; Introduction Link List  Declaration Double Link List Exercises Differences between  Link List and Array
INSERT NODE AT THE BEGINNING OF THE LIST Basic Link  List Operation Circular  Link 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; } 10 head Nptr 15 head=Nptr Nptr->Next=Head 15 head 10 Nptr Process Result Introduction Link List  Declaration Double Link List Exercises Differences between  Link List and Array

Link List

  • 1.
    LINK LIST SUBJECT: DATA STRUCTURE LECTURER : MADAM UMI KALSUM HASSAN
  • 2.
    Introduction Differences betweenLink List and Array Link List Declaration Basic Link List Operation Circular Link List Double Link List Exercises OUTLINES
  • 3.
    Link List is an ordered collection of elements called nodes which has two parts. The nodes connect by pointer. LINK LIST Basic Link List Operation Circular Link List Data Pointer Information part : store element of the list Address part : pointer that indicates the location of next node Data Pointer Data \ Head If no next node, the pointer contains a NULL value Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  • 4.
    ARRAY VS. LINKLIST Basic Link List Operation Circular Link List Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array Aspect Array Link List Size Fixed number. Size need to be specific during declaration. Grow and contract since of insertions and deletions. Maximum size depends on heap. Storage capacity Static: It’s location is allocated during compile time. Dynamic: It’s node is located during run time. Order and sorting Stored consecutively. Stored randomly. Accessing the element Direct or random access method. Specify the array index or subscript. Sequential access method. Traverse starting from the first node in the list by pointer. Searching Binary search and linear search Linear search
  • 5.
    LINK LIST DECLARATIONBasic Link List Operation Circular Link List Syntax: struct nodename { variable declarations; nodename *next; }; Example 1: struct Node { int Data; // to store integer value; 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; }; Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  • 6.
    LINK LIST EXTERNALPOINTER Basic Link List Operation Circular Link List External pointers are needed in a link list for the following purposes: Pointer to hold the list by pointing to the first node in list. ( usually declared as Head, First, Front, Hptr and etc ) Pointer to traverse/ visit the nodes in list. ( usually declared as Current, Curr, CPtr, Currptr and etc ) Pointer to hold new node . ( usually declared as NewPtr, Nptr and etc ) Pointer to mark the location of the node to be declared or inserted . ( usually declared as Temp, Tempptr and etc ) Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  • 7.
    LINK LIST EXTERNALPOINTER Basic Link List Operation Circular Link List Syntax: 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; Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  • 8.
    BASIC LINK LISTOPERATION Basic Link List Operation Circular Link List Create a dynamic node Create a list Traversing a list Insert node Delete node Example : struct NumNode { int value; NumNode * Next; }; NumNode *head, *current, *temp, *Nptr; head=NULL; //the list is empty To hold the list To traverse the list To mark a node in the list To allocate new node Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  • 9.
    CREATE A DYNAMICNODE Basic Link List Operation Circular Link List Using new operator. Example : Nptr = new NumNode cout<<“Enter a number: “; cin>>Nptr->value; Nptr->Next=NULL; Nptr 10 NumNode DELETE A DYNAMIC NODE Using delete operator. delete Nptr; // delete the node pointed by 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 . Nptr 10 NumNode Nptr 5 10 NumNode 9 Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  • 10.
    CREATE A LINKLIST Basic Link List Operation Circular Link List Using new operator. Example : // create a node Nptr = new NumNode cout<<“Enter a number: “; cin>>Nptr->value; Nptr->Next=NULL; //test if the list empty if (head==NULL) head=Nptr; //head pointer to //the new node Nptr 10 NumNode head Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  • 11.
    TRAVERSING A LINKLIST Basic Link List Operation Circular Link List 2 purpose: 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 To find the last node in the list. while (current->Next != NULL) current=current->Next; Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  • 12.
    INSERT NODE ATTHE BEGINNING OF THE LIST Basic Link List Operation Circular Link 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; } 10 head Nptr 15 head=Nptr Nptr->Next=Head 15 head 10 Nptr Process Result Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array