0

I have to write a code.The program compute random sentences by using singly linked list date structure.The same word cannot be used more than once.
I've already written the some codes which takes the list.

// random_sentence_operator.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 40

typedef struct SLLTag{
   char * data;
   struct SLLTag * next;
}SLLNode;

void addNode(SLLNode *cur,char *str);
void displayList(SLLNode *cur);
void randomSentence(SLLNode *cur,char *sentence);


int _tmain(int argc, _TCHAR* argv[])
{
	char str[SIZE];
	SLLNode *head;
	head=(SLLNode *)malloc(sizeof(SLLNode));
	head->next=NULL;
	head->data="";
	printf("Enter the words to add the list:(Press '0' to stop)\n");
	while(1)
	{
		scanf("%s",str);
		if(!strcmp(str,"0")==0)
			addNode(head,str);
		else break;
	
	}
	displayList(head);
	return 0;
}

void addNode(SLLNode *cur,char *str)
{
	while(cur->next != NULL)
		cur=cur->next;
	cur->next=(SLLNode *)malloc(sizeof(SLLNode));
	cur->next->data = (char *)malloc(sizeof(char)*SIZE);

	strcpy(cur->next->data,str);
	cur->next->next=NULL;
}
void displayList(SLLNode *cur)
{
	printf("The words which you added the list are:\n");
	while(cur->next!=NULL)
	{
		printf("%s\n",cur->next->data);
		cur=cur->next;
	}

}
void randomSentence(SLLNode *cur,char *sentence)
{


}

I take the words with this code.Now,How can i create a random sentece with these words.Need some more ideas.

2
Contributors
1
Reply
4
Views
5 Years
Discussion Span
Last Post by harinath_2007
0

I take the words with this code.Now,How can i create a random sentece with these words.Need some more ideas.

All you need to do is to just generate a random number(or just sort of index value)
Retreive the words (random words) by using the random value generated. I mean
suppose your linked list is like this...

hello -> hai -> good morning -> good evening->....

now suppose your random value is say 4, then loop through the linked list 4 times and retrieve the value i.e good evening or whatsoever.
Append that word to some character array or pointer.(as you wish)

Like this you generate some random number and repeat the above steps.

Here's how you generate random number
http://www.phanderson.com/C/random.html

There are so many ways or solutions to your problem .. What is said is one among the many solutions.

You can also have your own logic to do so....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.