In: Computer Science
FINISH print and freelist
#include<stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node* insert(struct node* list,int d );
struct node* del(struct node* list,int d );
void print( struct node *list);
void freeList(struct node* list);
void copy ( struct node *q, struct node **s );
int main( ) {
int number = 0, choice = 0;
struct node *pList=NULL;
struct node *nList = NULL;
while(choice!= 4)
{
printf("Do you want to (1)insert, (2)delete, (3)Copy (4)quit.\n");
scanf("%d", &choice);
printf("Your choice is %d\n", choice);
if (choice == 1)
{
printf("Enter the value to insert\n");
scanf("%d", &number);
pList = insert(pList, number);
printf("Items in linked list: ");
print(pList);
printf("\n");
}
else if (choice == 2)
{
printf("Enter the value to delete.\n");
scanf("%d", &number);
pList = del(pList, number);
printf("Items in linked list: ");
print(pList);
printf("\n");
}
else if (choice == 3)
{
if (nList)
freeList(nList);
copy(pList, &nList);
printf("Items in NEW linked list: ");
print(nList);
printf("\n");
}
else
{
break;
}
}
freeList(nList);
freeList(pList);
printf("\nBye..\n");
return 0;
}
void copy ( struct node *source, struct node **dest )
{
if(source != NULL)
{
*dest = malloc(sizeof(struct node));
(*dest)-> data = source -> data;
(*dest) -> next= NULL;
copy(source->next, &((*dest)->next));
}
}
struct node* insert(struct node *list,int item)
{
if(list == NULL || item <= list->data)
{
struct node * pNew = (struct node *) (malloc(sizeof(struct node)));
pNew->data = item;
pNew->next = list;
return pNew;
}
list->next = insert(list->next, item);
return list;
}
struct node* del(struct node *list, int item)
{
if(list == NULL)
return NULL;
if(list->data == item)
{
struct node* rest = list->next;
free(list);
return rest;
}
list->next = del(list->next, item);
return list;
}
void print(struct node *list)
{
}
void freeList(struct node* list)
{
}
Given below is the code for the print and the freeList functions. I have included comments (between /* and */) after every line of code to fully explain its working for your easy and better understanding. Also, you can refer to the code summary that I have provided after the code, so that you can understand the approach that we use for both the print and freeList functions.
CODE:
/*Answer 1 - code for the print function to print and display all the contents of a given linked list*/
void print(struct node *list)
{
while (list != NULL)
{
printf(" %d ", list->data); /*print the current contents of a node of the list*/
list = list->next; /*move to the next node of the list and make it the current node*/
}
}
/* Answer 2- code for the freeList function to free/delete the entire linked list */
void freeList(struct Node** list)
{
struct Node* currentNode = *list; /*make the head of the node as the current node*/
struct Node* next; /*make a temporary node to hold the next element to be deleted*/
while (currentNode != NULL) /*iterate or traverse through the given list until we reach its end or NULL*/
{
next = currentNode->next; /*store the next node of the list in the temporary node*/
free(currentNode); /*delete the current node using the free() function*/
currentNode = next; /*assign the temporarily saved next node to the original list as current or the head node*/
}
/* assign the head of the list as NULL after completely deleting the list */
*list = NULL;
}
Code Summary: In the print function, for printing a linked list, we simply traverse each node of the linked list and print its contents until the list becomes empty and reaches NULL or in simple words we reach the end of the list. In the freeList function, to delete the entire linked list, we again simply traverse each node of the linked list and delete that node using the free() function until we reach the end of the list.
Thanks!