linklist2 - LINKED LIST - II Counting number of nodes in a...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
LINKED LIST - II Counting number of nodes in a list Printing a list Searching for a specific node in a list Creating a list Reversing a list Deleting a specific node Inserting a node in a sorted list
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The following site gives a good coverage of linked lists. http://cslibrary.stanford.edu/103/LinkedListBasics.pdf Counting the nodes in a List Recursive version: int count (struct node * pHead) { if (pHead==NULL) return 0; else return(1 + count(pHead->next)); } Iterative version: int count(struct node *pHead) { struct node * p; int c = 0; p = pHead; while (p != NULL){ c = c + 1; p = p->next; } return c; }
Background image of page 2
Printing the contents of a list To print the contents, traverse the list from the head node to the last node. int PrintList( struct node *list) { struct node current = list ; The dummy name current is assigned to the first node of the list . We shall be moving down the list by following the next link of current , without disturbing any item in the list including its name. while (current != NULL) { printf(“%d”, current -> data); current = current -> next; } return 1; } Searching for a node containing a specific item Given a target value, the search attempts to locate the requested node in the linked list. If a node in the list matches the target value, the search returns 1; otherwise it returns 0. // Start with a dummy pointer to headnode pCur = pHead; // Search until target is found or we reach // the end of list while (pCur != NULL ){ if( pCur->data == target) return 1; pCur = pCur->next; } // target not found return 0;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Creation of Linked Lists by adding nodes at front of the list Here we assume that the main program is sending a list and an integer to the function. The integer is to be included as the first element of the list, and the new list is to be sent back to the main. A typical statement in the main program could be : p =Add_Start(p, 60). Thus given the values 32,40,55,60 and 80 the following linked list is desired. We invoke malloc to allocate space for a new node. The given integer is stored in the data part. To add the node at the front of the list, all you have to do is to store the pointer to the list in the address part of this node. struct node * Add_Start(struct node *list,int d )
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 15

linklist2 - LINKED LIST - II Counting number of nodes in a...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online