Hi I'm using the Pascal language and I need help with the following problem. You need to find the most common word read in by a user of arbitrary length and then display the amount of times its found in the input. I placed all the words into a linked list without hassle but the problem is I don't know how to count the repeated words in the linked list. Can someone here help me?
P.S. It has to be a linked list, but since I'm very new to the linked list data structure, if you could explain what you did, I'd appreciate it.
Thanks
Havoc433
uses crt;
type
nodePointer = ^node;
node = record
word : string;
next : nodePointer;
end;
list = nodePointer;
var
wordlist:list;
words :nodePointer;
newword :nodePointer;
input : string;
i : integer;
position:integer;
cut :string;
begin
clrscr;
new(wordlist);
wordlist:=nil;
write('Enter a sentence:');
readln(input);
input:=input+' ';
for i:=1 to length(input) do
begin
if input[i]=' ' then
begin
position:=pos(input[i],input);
cut:=copy(input,1,position-1);
delete(input,1,position);
new(newword);
newword^.word := cut;
newword^.next := wordlist;
wordlist := newword;
end;
end;
words:=wordlist;
while words <> nil do
begin
write(words^.word);
words := words^.next;
end;
end.
Dear Frind
Ther are several sulotion for your problem.
I send you tow sulotion hear
1-
uses crt;
type
nodePointer = ^node;
node = record
word : string;
next : nodePointer;
end;
list = nodePointer;
var
wordlist:list;
words :nodePointer;
newword :nodePointer;
input : string;
i : integer;
position:integer;
cut :string;
Count :Integer;
Find :nodePointer;
begin
clrscr;
new(wordlist);
wordlist:=nil;
write('Enter a sentence:');
readln(input);
input:=input+' ';
for i:=1 to length(input) do
begin
if input[i]=' ' then
begin
position:=pos(input[i],input);
cut:=copy(input,1,position-1);
delete(input,1,position);
new(newword);
newword^.word := cut;
newword^.next := wordlist;
wordlist := newword;
end;
end;
words:=wordlist;
while words <> nil do
begin
find:=WordList;
Count:=0;
While Find <> nil do
Begin
if Find^.word=words^.word then
Count:=Count+1;
Find:=Find^.Next;
End;
writeLn(words^.word+'->',Count);
words := words^.next;
end;
ReadLn;
end.
this sulotion is simple and I dont Try to change your code but I Mast Say that your Programe Have some Buges
so I make some change to your programe ande Send you another sulotion
2-
uses crt;
type
nodePointer = ^node;
node = record
word : string;
Count : integer;
next : nodePointer;
end;
list = nodePointer;
var
wordlist:list;
words :nodePointer;
newword :nodePointer;
input : string;
i : integer;
position:integer;
cut :string;
Count :Integer;
Find :nodePointer;
Sentence:String;
begin
clrscr;
new(wordlist);
wordlist:=nil;
write('Enter a sentence:');
readln(input);
input:=input+' ';
While length(input)>0 do
begin
position:=pos(' ',input);
if Position>0 then
begin
cut:=copy(input,1,position-1);
If Cut<>'' then
Begin
delete(input,1,position);
new(newword);
newword^.word := cut;
if WordList=nil then
Begin
Newword^.count:= 1;
newword^.next := wordlist;
wordlist := newword;
End
Else
Begin
count:=1;
Find:= WordList;
While Find^.Next<> nil do
Begin
if Find^.Word=Cut then
Begin
Find^.Count:=Find^.Count+1;
Count:=Count+1;
End;
Find:=Find^.Next;
End;
if Find^.Word=Cut then
Begin
Find^.Count:=Find^.Count+1;
Count:=Count+1;
End;
If Count>1 Then
Count:=0;
Find^.Next:=NewWord;
NewWord^.Count:=Count;
NewWord^.Next:=nil;
End;
End
Else
Delete(Input,1,1);
end;
end;
words:=wordlist;
Sentence:='';
while words <> nil do
begin
If Words^.Count>0 then
Begin
write(words^.word+'->');
writeln(Words^.Count);
End;
Sentence:=Sentence+' '+Words^.Word;
words := words^.next;
end;
WriteLn;
Writeln('Sentence= ',Sentence);
ReadLn;
end.
Reputation Points: 12
Solved Threads: 10
Junior Poster in Training
Offline 66 posts
since Jun 2007