Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming (not C++) In this lab, we will create an application which allows

ID: 3686276 • Letter: C

Question

C programming (not C++)

In this lab, we will create an application which allows the user to build a sorted list of strings. The program will perform the following actions: 1. Prompt the user to enter a string 2. Call a function to insert that string, in alphabetic order, into a linked list. 3. If the user did not enter a string, print the list in order and terminate. Specifications . Use this structure and constant for the linked list node: #de fine MAX STR LEN struct link node 80 char node str[ MAX STR LEN struct link node *next; . Your solution should incorporate the following functions // This function is used to compare two nodes. /The returnvalues are: // +1: n1 n2 int compare node ( struct link node *n1, struct link node *n2); //This function is used to add a new node into the // alphabetically sorted linked list. The head of the list is pointed to by 'list' /The returnvalue is the (possibly new) head pointer struct link node *add node ( struct link node *list,

Explanation / Answer

Answer:

Note: code skeleton, methods and variables are provided in the code. Code is implemented as per the instructions provided.

code

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define MAX_STR_LEN 80

//decalring the structure

struct link_node

{

   //declaring the variables

   char node_str[ MAX_STR_LEN ];

   struct link_node *next;

};

//method declaration

typedef struct link_node link_node;

struct link_node *add_node( struct link_node *list,struct link_node *node );

int compare_node( struct link_node *n1, struct link_node *n2 );

void display_list( struct link_node *head );

//implementing the method main

int main()

{

    //declaring the main variables

    link_node * head=NULL,*temp;

    char str[MAX_STR_LEN];

   do

   {

       //getting the STRING input

       printf("Enter the string : ");

       gets(str);

       //malloc() is implemented for memory

      temp=(link_node*)malloc(sizeof(link_node));

       temp->next=NULL;

       strcpy(temp->node_str,str);

       head=add_node(head,temp);

   }while(strlen(str)>1);

   display_list(head);

   return 0;

}

//method definition for compare node

int compare_node( struct link_node *n1, struct link_node *n2 )

{

    if(strcmp(n1->node_str,n2->node_str)==0)

    return 0;

    if(strcmp(n1->node_str,n2->node_str)<0)

    return -1;

    else

    return 1;

}

//method definition for adding the node

struct link_node *add_node( struct link_node *list,struct link_node *node )

{

    link_node *temp=list;

    if(list==NULL)

    {

        return node;

    }

    if(compare_node(node,list)==-1)

    {

        node->next=list;

        list=node;

        return list;

    }

    else

    {

        link_node *prev=list;

        while(temp!=NULL&&compare_node(node,temp)>=0)

        {

             prev=temp;

             temp=temp->next;

        }

        prev->next=node;

        node->next=temp;

        return list;

    }

}

//method definition for displaying the list

void display_list( struct link_node *head )

{

    link_node *temp;

    while(head)

    {

        printf("%s ",head->node_str);

        temp=head;

        head=head->next;

        free(temp);

    }

}

Sample output:

Enter the string:

Geneva

America

Goa

China

America

China

Geneva

Goa