









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of linked lists, including counting nodes, printing contents, searching for specific nodes, creating linked lists, reversing linked lists, and deleting nodes. It includes both recursive and iterative solutions for counting nodes and examples of creating linked lists by adding nodes at the front and end. The document also covers inserting nodes in a sorted list.
Typology: Study notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Counting the nodes in a List
int count (struct node * pHead) { if (pHead==NULL) return 0; else return(1 + count(pHead->next)); }
int count(struct node *pHead) { struct node * p; int c = 0;
p = pHead; while (p != NULL){ c = c + 1; p = p->next; } return c; }
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 ) { struct node * pNew=(struct node *) (malloc(sizeof(struct node))); pNew -> data = d ; pNew ->next = list ;
return pNew; }
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 p and the value of the element to the function. Thus given the same values as in previous case, the desired Linked list is:
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; }
Creation of Linked Lists by adding nodes at front
of the list (using double pointers )
Here we present a different mechanism to create a linked list. Instead of sending the pointer to the list, we send the address of the pointer to the list as a parameter in the function call and have the list in the memory updated by this function.
This is nothing but passing-by-reference the address of the pointer to head node. This can be done through a double pointer. While *list is the address of list, the pointer to the address itself can be passed through the double pointer **list. See the following code where the function is getting the proper pointer, and the called function can see the changes made to the list. Note this function does not return any list. .
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; }
A typical call from the main function would have the following form:
Add_Front( &pList, number );
You may also see pages 12-14 of
for more information on this.
Deleting a Node from a Linked List
Deleting a node requires that we logically remove the node from the list by changing various link pointers and then physically deleting the node from the heap.
We can delete
To logically delete a node:
Note : We may be deleting the only node in a list. So take care of it separately.
This will result in an empty list in which case the head pointer is set to NULL.
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);
Algorithm for Deleting a node containing integer
d from the list pHead.
It is assumed that the node containing the integer d is present in the list. Let PCur point to the first node. Let pPre be the pointer to the predecessor of the current node pCur. The first step would be to move through the list till pCur is the node containing the integer d. The pointer pPre is also moved so that it is always one step behind pCur.
I pPre=NULL; pCur= pHead; while(pCur !=NULL && pCur->data != value) { pPre = pCur; pCur = pCur->next; } //now pCur contains the integer d and pPre is the node previous to it if (pPre== NULL){ // this means that the first node is to be deleted //so make the second node the head node pCur = pHead; pHead = pCur->next; } else pPre->next = pCur->next;
free(pCur);
At this point the list looks like this:
pNew -> next = pCur -> next; pCur -> next = pNew ; } }
/ 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; }