Question

In: Computer Science

FINISH print and freelist #include<stdio.h> #include <stdlib.h> struct node {      int data;      struct node  *next; }; struct...

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)

{

    

}

Solutions

Expert Solution

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!


Related Solutions

#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*);...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*); void showList(node*); void printListBackwards(node *); int main(void) { node *list1; printf("\n Create a sorted list.."); printf("\n Enter values for the first list (-999 to end):"); list1=(node*)malloc(sizeof(node*)); //Allocate memory for the list node insert(list1); //insert values by calling the function insert showList(list1); //display values entered by user printf("\n After recursively reversing the list is :\n"); printListBackwards(list1); //print the values in reverse order using the function...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data;...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; int main() { link ptr,head; int num,i; head = ( link ) malloc(sizeof(node)); ptr = head; printf("enter 10 data \n"); for ( i = 0; i <= 9; i++ ) { scanf("%d",&num); ptr->data = num; ptr->next = ( link ) malloc(sizeof(node)); if ( i == 9 ) ptr->next = NULL; else ptr =...
#include <stdio.h> #include <string.h> #include<stdlib.h> #include<conio.h> struct Bank_Account_Holder { int account_no; char name[80]; int balance; };...
#include <stdio.h> #include <string.h> #include<stdlib.h> #include<conio.h> struct Bank_Account_Holder { int account_no; char name[80]; int balance; }; int n; void accept(struct Bank_Account_Holder[], int); void display(struct Bank_Account_Holder[], int); void save(struct Bank_Account_Holder[], int); void load(struct Bank_Account_Holder[], int); int search(struct Bank_Account_Holder[], int, int); void deposit(struct Bank_Account_Holder[], int, int, int); void withdraw(struct Bank_Account_Holder[], int, int, int); int lowBalenquiry(int,int); void main(void) { clrscr(); struct Bank_Account_Holder data[20]; int choice, account_no, amount, index; printf("NHU Banking System\n\n"); printf("Enter the count of records: "); scanf("%d", &n); accept(data, n); do {...
C++ language. struct Node {    int data;    Node *next; } Write a function to...
C++ language. struct Node {    int data;    Node *next; } Write a function to concatenate two linked lists. Given lists A* = (4, 6) and B* = (3, 7, 12), after return from Concatenate_Lists(Node A*, Node B*) the list A should be changed to be A = (4, 6, 3, 7, 12). Your function should not change B and should not directly link nodes from A to B (i.e. the nodes inserted into A should be copies of...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
#include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size);...
#include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size); int main(){ int arraySize, limit, count, srand(time(0)); print f("Enter the size of array\n"); scanf("%d", arraySize); int array[arraySize]; printf("Enter the upper limit\n"); scanf("%d", &limit); count = 0; while(count <= arraySize){ array[count] = (rand() % (limit + 1)); count++; } printArray(array, &arraySize); sort(array, arraySize); printArray(array, arraySize); Return 0; } void printArray(int a[], int size){ int i = 0; printf("Array: ["); while(i < size){ if(i != size...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i =...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i = 0; i < lines; ++i ){         printf("\n");     }     return; } void printRules(void){     printf("\t|*~*~*~*~*~*~*~*~*~ How to Play ~*~*~*~*~*~*~*~*~*~|\n");     printf("\t|   This is a 2 player game. Player 1 enters the   |\n");     printf("\t|   word player 2 has to guess. Player 2 gets a    |\n");     printf("\t|   number of guesses equal to twice the number    |\n");     printf("\t|   of characters. EX: If the word is 'example'    |\n");     printf("\t|   player 2 gets 14 guesses.                      |\n");     printf("\t|*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~|\n");     clrScreen(10);     return; } //------------------------------------------------------------------------------------------------------------ /*...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT