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...
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...
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
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 (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
In this problem, you will write a program that reverses a linked list. Your program should...
In this problem, you will write a program that reverses a linked list. Your program should take as input a space-separated list of integers representing the original list, and output a space-separated list of integers representing the reversed list. Your algorithm must have a worst-case run- time in O(n) and a worst-case space complexity of O(1) beyond the input. For example, if our input is: 5 7 1 2 3 then we should print: 3 2 1 7 5 Please...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT