Link List

8,453 views
8,149 views

Published on

link list data structure

Published in: Technology, Design
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,453
On SlideShare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
613
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Link List

  1. 1. LINK LIST SUBJECT : DATA STRUCTURE LECTURER : MADAM UMI KALSUM HASSAN
  2. 2. <ul><li>Introduction </li></ul><ul><li>Differences between Link List and Array </li></ul><ul><li>Link List Declaration </li></ul><ul><li>Basic Link List Operation </li></ul><ul><li>Circular Link List </li></ul><ul><li>Double Link List </li></ul><ul><li>Exercises </li></ul>OUTLINES
  3. 3. <ul><li>Link List is an ordered collection of elements called nodes which has two parts. </li></ul><ul><li>The nodes connect by pointer. </li></ul>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. 4. 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 <ul><li>Fixed number. </li></ul><ul><li>Size need to be specific during declaration. </li></ul><ul><li>Grow and contract since of insertions and deletions. </li></ul><ul><li>Maximum size depends on heap. </li></ul>Storage capacity <ul><li>Static: It’s location is allocated during compile time. </li></ul><ul><li>Dynamic: It’s node is located during run time. </li></ul>Order and sorting <ul><li>Stored consecutively. </li></ul><ul><li>Stored randomly. </li></ul>Accessing the element <ul><li>Direct or random access method. </li></ul><ul><li>Specify the array index or subscript. </li></ul><ul><li>Sequential access method. </li></ul><ul><li>Traverse starting from the first node in the list by pointer. </li></ul>Searching <ul><li>Binary search and linear search </li></ul><ul><li>Linear search </li></ul>
  5. 5. LINK LIST DECLARATION Basic Link List Operation Circular Link List <ul><li>Syntax: </li></ul><ul><li>struct nodename </li></ul><ul><li>{ variable declarations; </li></ul><ul><li>nodename *next; </li></ul><ul><li>}; </li></ul><ul><li>Example 1: </li></ul><ul><li>struct Node </li></ul><ul><li>{ int Data; // to store integer value; </li></ul><ul><li> Node *Next; // a pointer to next node; </li></ul><ul><li>}; </li></ul><ul><li>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. </li></ul><ul><li>struct STUDNODE </li></ul><ul><li>{ char ID [4]; </li></ul><ul><li>char name [20]; </li></ul><ul><li>float CGPA; </li></ul><ul><li>STUDNODE * Next; </li></ul><ul><li>}; </li></ul>Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  6. 6. LINK LIST EXTERNAL POINTER Basic Link List Operation Circular Link List <ul><li>External pointers are needed in a link list for the following purposes: </li></ul><ul><ul><li>Pointer to hold the list by pointing to the first node in list. ( usually declared as Head, First, Front, Hptr and etc ) </li></ul></ul><ul><ul><li>Pointer to traverse/ visit the nodes in list. ( usually declared as Current, Curr, CPtr, Currptr and etc ) </li></ul></ul><ul><ul><li>Pointer to hold new node . ( usually declared as NewPtr, Nptr and etc ) </li></ul></ul><ul><ul><li>Pointer to mark the location of the node to be declared or inserted . ( usually declared as Temp, Tempptr and etc ) </li></ul></ul>Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  7. 7. LINK LIST EXTERNAL POINTER Basic Link List Operation Circular Link List <ul><li>Syntax: </li></ul><ul><li>Nodename *pointer1,*pointer2,..,*pointerN; </li></ul><ul><li>Example 1: </li></ul><ul><li>struct Node </li></ul><ul><li>{ int Data; // to store integer value </li></ul><ul><li>Node *Next; // a pointer to next node </li></ul><ul><li>}; </li></ul><ul><li>Node *Head, *Current, *NewNode; </li></ul><ul><li>Example 2: </li></ul><ul><li>struct STUDNODE </li></ul><ul><li>{ char ID [4]; </li></ul><ul><li>char Name [20]; </li></ul><ul><li>float CGPA; </li></ul><ul><li>STUDNODE * Next; </li></ul><ul><li>}; </li></ul><ul><li>STUDNODE *Firstptr, *CurrPtr, *NewPtr; </li></ul>Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  8. 8. BASIC LINK LIST OPERATION Basic Link List Operation Circular Link List <ul><li>Create a dynamic node </li></ul><ul><li>Create a list </li></ul><ul><li>Traversing a list </li></ul><ul><li>Insert node </li></ul><ul><li>Delete node </li></ul><ul><li>Example : </li></ul><ul><li>struct NumNode </li></ul><ul><li>{ int value; </li></ul><ul><li> NumNode * Next; </li></ul><ul><li>}; </li></ul><ul><li>NumNode *head, *current, *temp, *Nptr; </li></ul><ul><li>head=NULL; //the list is empty </li></ul>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. 9. CREATE A DYNAMIC NODE Basic Link List Operation Circular Link List <ul><li>Using new operator. </li></ul><ul><li>Example : </li></ul><ul><li>Nptr = new NumNode </li></ul><ul><li>cout<<“Enter a number: “; </li></ul><ul><li>cin>>Nptr->value; </li></ul><ul><li>Nptr->Next=NULL; </li></ul>Nptr 10 NumNode DELETE A DYNAMIC NODE <ul><li>Using delete operator. </li></ul><ul><li>delete Nptr; // delete the node pointed by </li></ul><ul><li>NptrNptr=Null; // set the pointer to Null </li></ul><ul><li>If the link list more than one node: Need to change pointer of record before the one being deleted . </li></ul>Nptr 10 NumNode Nptr 5 10 NumNode 9 Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  10. 10. CREATE A LINK LIST Basic Link List Operation Circular Link List <ul><li>Using new operator. </li></ul><ul><li>Example : </li></ul><ul><li>// create a node </li></ul><ul><li>Nptr = new NumNode </li></ul><ul><li>cout<<“Enter a number: “; </li></ul><ul><li>cin>>Nptr->value; </li></ul><ul><li>Nptr->Next=NULL; </li></ul><ul><li>//test if the list empty </li></ul><ul><li>if (head==NULL) </li></ul><ul><li>head=Nptr; //head pointer to </li></ul><ul><li>//the new node </li></ul>Nptr 10 NumNode head Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  11. 11. TRAVERSING A LINK LIST Basic Link List Operation Circular Link List <ul><li>2 purpose: </li></ul><ul><ul><li>To process every node in the list. </li></ul></ul><ul><ul><li>Example : Traverse a list to sum all nodes, count the number of the node and calculate the average of nodes in the list. </li></ul></ul><ul><ul><li>sum = 0; </li></ul></ul><ul><ul><li>count = 0; </li></ul></ul><ul><ul><li>current = head; // point the first node in the list </li></ul></ul><ul><ul><li>while (current != NULL) </li></ul></ul><ul><ul><li>{ sum sum+current->data; // sum node </li></ul></ul><ul><ul><li> count++; // count the node </li></ul></ul><ul><ul><li> current = current->Next; // move to next node </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>average = sum/count; // calculate average </li></ul></ul><ul><ul><li>To find the last node in the list. </li></ul></ul><ul><ul><li>while (current->Next != NULL) </li></ul></ul><ul><ul><li> current=current->Next; </li></ul></ul>Introduction Link List Declaration Double Link List Exercises Differences between Link List and Array
  12. 12. INSERT NODE AT THE BEGINNING OF THE LIST Basic Link List Operation Circular Link List <ul><li>// create a new list </li></ul><ul><li>Nptr=new NumNode; </li></ul><ul><li>cout<<“Enter a new number: “; </li></ul><ul><li>cin>>Nptr->value; </li></ul><ul><li>Nptr-Next=NULL; </li></ul><ul><li>// test if the list is empty </li></ul><ul><li>if (head==NULL) </li></ul><ul><li>head=Nptr; // head pointer points to the new node </li></ul><ul><li>else // head pointer points to new node </li></ul><ul><li>{ Nptr->Next=head; </li></ul><ul><li>head=Nptr; </li></ul><ul><li>} </li></ul>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

×