Question

In: Computer Science

Write a C program that creates and prints out a linked list of strings. • Define...

Write a C program that creates and prints out a linked list of strings.

• Define your link structure so that every node can store a string of up to 255 characters.

• Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position.

• Implement the print_list function that prints the list.

• In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character, tab, or newline).

• Read and insert every string into the link list until you reach a single dot “.” or EOF.

• Print the list. Use scanf to read strings. Use strcpy() for copying strings.

Here is a sample text input:

This is a sample text. The file is terminated by a single dot: .

Solutions

Expert Solution

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//defining node structure
typedef struct node
{
   char data[255];//can store upto 255 characters
   struct node *next;
}Node;
Node *head=NULL;//initially linked list is empty
//method to insert in sorted order
void insert_dictionary_order(char *a)
{
   Node *n = (Node*)malloc(sizeof(Node));//creating new node
   strcpy(n->data,a);//reading data
   n->next=NULL;
   Node *temp=head,*prev=NULL;
   if(temp==NULL)
   {
       head=n;  
   }
   else
   {//inserting in right position
       while(temp!=NULL)
       {
           if(0<strcmp(temp->data,a))
           break;
           prev=temp;
           temp=temp->next;  
       }
       if(prev==NULL)
       {
           n->next=head;
           head=n;  
       }
       else
       {
           n->next=prev->next;
           prev->next=n;  
       }  
   }
}
//method to print all words in list
void print_list()
{
   Node *temp=head;
   while(temp!=NULL)
   {
       printf("%s ",temp->data);
       temp=temp->next;  
   }
   printf("\n");
}
int main()
{
   printf("Enter words seperated by spaces:(. or EOF to stop):\n");
  
   do
   {
       char s[255];
       scanf("%s",s);
       if(strcmp(s,".")==0 || strcmp(s,"EOF")==0)
       {
           insert_dictionary_order(s);//adding to list
           break;
       }
       else
       {
           insert_dictionary_order(s);//adding to list  
       }
   }
   while(1);
   //now printing list
   print_list();
   return 0;
}

output:

Enter words seperated by spaces:(. or EOF to stop):
This is a sample text. The file is terminated by a single dot: .
. The This a a by dot: file is is sample single terminated text.


Process exited normally.
Press any key to continue . . .


Related Solutions

Write a program in C++ that will make changes in the list of strings by modifying...
Write a program in C++ that will make changes in the list of strings by modifying its last element. Your program should have two functions: 1. To change the last element in the list in place. That means, without taking the last element from the list and inserting a new element with the new value. 2. To compare, you need also to write a second function that will change the last element in the list by removing it first, and...
IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
Write a c program that reads a .img file by rows and columns and prints out...
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be: 2 4 DFJSK HJ5JF HFDY5
Write a program in C++ that prints out the even numbers between 1 and 21 using...
Write a program in C++ that prints out the even numbers between 1 and 21 using WHILE loop. Also, find the sum AND product of these numbers and display the resulting sum and product.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
1) a. Write down a C++ program which will create a list (simple linear linked list)...
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function...
Write a recursive function in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT