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...
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...
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) }
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
Write a program that asks the user for an integer and then prints out all its...
Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 84, the program should print 2 2 3 7 Validate the input to make sure that it is not a character or a string using do loop.in c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT