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
  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • 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...

  • Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...

    Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...

  • 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...

  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

  • 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...

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