







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








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;
Delete First Node
pCur = pHead; pPre = NULL; pHead = pCur -> next; free (pCur);
General Delete Case
Here , you have to keep track of not only pCur, the node to be deleted but also its predecessor pPre. pPre->next = pCur->next; free(pCur);
Creation of Linked Lists
We want to create a list by adding nodes at the end of the list, as and when the user inputs the elements. The main function sends the list and the value of the element to the function. Here is a function, which adds a node to end of the linked list named list. Malloc is used to get a new node pNew. The element value is stored in the data part. If there are no elements in the list (empty list), the first pNew is going to be the only node of list., so it returns pNew. If there are nodes in the list, the while loop finds the last node and the pointer to pNew is stored in its address part. struct node* Addrear(struct node *list, int d) { struct node *pNew = NULL; struct node current = NULL; pNew = (struct node)malloc(sizeof(struct node)); pNew ->data = d; pNew ->next = NULL; // Store front of the linked list current = list; // if list is empty then this becomes the first node. if (list == NULL) return pNew; while (current ->next != NULL) current = current ->next; current ->next = pNew; // Return a pointer to the created list. return list; } The function can be invoked from the main in the following manner:
pList = AddRear (pList, number);
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. 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 ) { struct node * pNew=(struct node *) (malloc(sizeof(struct node))); pNew -> data = d ; pNew ->next = list ; return pNew; } The call from the main would be pList = Add_Start( pList, number)
/ To add elements at the start of a list and to print the contents / #include <stdio.h> #include <stdlib.h> typedef struct node { int data; struct node *next; }; int PrintList( struct node list); int Add_Front(struct node *list,int d ); main( ) { int number = 0; struct node pList=NULL; while(number!= -1) { printf(“enter data for next node \n “); scanf(“%d”, &number); if (number !=-1) { AddStart ( &pList, number ); / pass address of pList to the add function */ } } printf(“items in linked list \n”); PrintList ( pList ); return 1; }
int Add_Front(struct node* *list,int d ) { struct node * pNew=(struct node ) (malloc(sizeof(struct node))); pNew-> data = d ; pNew->next = NULL) ; if(list== NULL) { *list = pNew; } else { pNew->next = *list; *list = pNew ; } return 1; } int PrintList( struct node *list) { struct node current = list ; while (current != NULL) { printf(“%d”, current -> data); current = current -> next; } return 1; }
Reversing a linked list by storing elements backwards in another list Each node in a list called “front “ contains one integer value. We want to reverse the order of the integers in another list called “ second ”. // Reverse the list by inserting every element in front of the previous one pCur= front; while(pCur !=NULL) { struct node* pNew = (struct node*) (malloc(sizeof(struct node))); pNew ->data = pCur ->data; pNew->next = second; second = pNew; pCur = pCur->next; } pCur = second;