Question

                                          &nb

                                                                                                                       The language should be in C

Specifications

Use this structure and constant for the linked list node:

#define MAX_STR_LEN     80

struct link_node

{

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 return values are:

// -1: n1 < n2

// 0: n1 == n2

// +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 return value is the (possibly new) head pointer.

struct link_node *add_node( struct link_node *list,

                           struct link_node *node );

// Follow the linked list and print out each node along the way.

void display_list( struct link_node *head );

Use malloc() to create new link_node structures.

Use fgets() to read the user’s input string.

Pseudo-Code

Following is the start of some pseudo code for this lab. Expand on this to fully describe your solution:

Give the user some instructions on what to do.

WHILE ( the user doesn’t enter an “empty” string )

Get the user entered string.

Allocate a new node.

Initialize the node with the user’s string.

Add the node to the list in alphabetic order.

END-WHILE

Display the linked list of strings.


Example

Following is an example of running the program:

Please enter strings. When you are done, enter a blank line.

> Ken Arnold

> Tony Montiel

> Boba Fett

> Hoban Washburne

> Neil Armstrong

>

Boba Fett

Hoban Washburne

Ken Arnold

Neil Armstrong

Tony Montiel


if possible

You used malloc() to allocate the nodes. Do the right thing, and use free() to release the memory.

With fgets(), you may have noticed that it copies over the new-line character. Get rid of that pesky extra character.


0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define MAX_STR_LEN     80

struct link_node
{
char node_str[ MAX_STR_LEN ];
struct link_node *next;
};

int compare_node( struct link_node *n1, struct link_node *n2 );
struct link_node *add_node( struct link_node *list, struct link_node *node );
void removeNextln(char *s1);
void display_list( struct link_node *head );

//struct link_node *head = (struct link_node*)malloc(sizeof(struct link_node));// Creating the main/first link node

int main()
{
    struct link_node *head = (struct link_node*)malloc(sizeof(struct link_node));
    head->next = NULL;

printf("Please enter strings. When you are done, enter a blank line.\n");
    char input[MAX_STR_LEN];

    fgets(input,MAX_STR_LEN, stdin);
    removeNextln(input);

    strcpy(head->node_str, input); // #1

    while (input[0]!='\0') {

        fgets(input, MAX_STR_LEN, stdin);
        removeNextln(input);
        if (input[0]!='\0') {
            struct link_node *newNode = (struct link_node*)malloc(sizeof(struct link_node));
            strcpy(newNode->node_str,input);

            newNode->next = NULL;

            head = add_node(head,newNode);
        }

    }

    display_list(head);
    free_mem(head);
}

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

    int ret = -99;

    if (strcmp(n1->node_str,n2->node_str)==0) {ret = 0;}
    else if (strcmp(n1->node_str,n2->node_str)>0) {ret = 1;}
    else if (strcmp(n1->node_str,n2->node_str)<0) {ret = -1;}

    //printf("Comparing %s to %s: %d \n", n1->node_str,n2->node_str,ret);

    return ret;
}

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

    struct link_node *curNode = list;
    struct link_node *prevNode = list;

    int cont = 1;

    if (compare_node(curNode,node)>0) {

            //printf("Oh no! New input should be above head!");
            char temp[MAX_STR_LEN];
            strcpy(temp,list->node_str);
            strcpy(list->node_str,node->node_str);
            strcpy(node->node_str,temp);
    }

    while (curNode->next != NULL && cont == 1) {
        prevNode = curNode;
        curNode = curNode->next;

        if (compare_node(curNode,node)>0) {
            cont = 0;
        }
    }

    //printf("CurNode: %s, PrevNode: %s\n", curNode->node_str,prevNode->node_str);

    if (cont == 1) {
        //printf("Reached end...");
        curNode->next = node;
    }

    else if (cont == 0) {
        //printf("Found earlier match...");
        //printf("PrevNode: %s", prevNode->node_str);

        node->next = curNode;
        prevNode->next = node;


    }

    return list;
}


void display_list(struct link_node *head) {

    struct link_node *curNode = head;
    while (curNode->next != NULL) {
        printf("%s\n", curNode->node_str);
        curNode = curNode->next;
    }
    printf("%s\n", curNode->node_str);
}

void removeNextln(char *s1) {

    int count;
    for (count = 0; s1[count]!= '\n'; count++);
    s1[count--] ='\0';

}

void free_mem(struct link_node *head) {

    struct link_node *next = head;
    struct link_node *prev = NULL;

    while (next!=NULL) {
        prev = next;
        next = next->next;
        free(prev);
    }

}


sh-4.3$ main Please enter strings. When you are done, enter a blank line ken arnold tony monitel boba fett hoban washburne ne

Add a comment
Know the answer?
Add Answer to:
                                          &nb
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a code for linked list, it is about adding a node in the middle...

    This is a code for linked list, it is about adding a node in the middle of a list, I am really confused why int i = 2? can't it be 0? Add to the Middle • Allocate memory and store data for new node Traverse to node just before the required position of new node Change next pointers to include new node in between struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp head; for(int...

  • Programing C Just with #include <stdio.h> We will create a singly linked list of 7 nodes....

    Programing C Just with #include <stdio.h> We will create a singly linked list of 7 nodes. Then, the user will tell us whether to print the “odd-placed” nodes or the “even-placed” nodes. For example, if I had a list of 5 nodes like below, and the user specifies for the odd nodes to be printed, my program would print the values in nodes nl and n3. If the user instead indicated for the even nodes to be printed, my program...

  • Define a struct Node to represent a node in a double linked-list that stores a string...

    Define a struct Node to represent a node in a double linked-list that stores a string as data. Write a function to insert a node to the head of the linked list. The function takes two arguments: a pointer to the first node in the double linked list and a string value. It should create a new node with the given value to the head of the double linked list.

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • Create Functions for the following prototypes. Below is the setup.*program is in c* #include <stdio.h> #include...

    Create Functions for the following prototypes. Below is the setup.*program is in c* #include <stdio.h> #include <string.h> #include <stdarg.h> #include <stdlib.h> //#define constant values #define MAX_URL_LENGTH 50 #define TRUE 1 #define FALSE 0 //typedef for the Element struct which constains a c string to store a URL in the BrowserList typedef struct { char szURL[MAX_URL_LENGTH]; } Element; //Typedef for a node in the doubly linked list (has next and previous pointers). typedef struct NodeDL { Element element; struct NodeDL *pNext;...

  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • Problem Statement This problem provides you with a linked list composed of node objects chained together...

    Problem Statement This problem provides you with a linked list composed of node objects chained together via node pointers. Each node has a next pointer which points to the next node in the chain. The last node is identified by having a NULL (or nullptr) next pointer. The linked lists for this problem store string data. Your task: identify the longest string stored in the linked list. If two strings are of the same length, return the string that occurred...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT