944,092 Members | Top Members by Rank

Ad:
Oct 4th, 2009
-1

How to find the most common word in a Linked List in Pascal.

Expand Post »
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

Pascal and Delphi Syntax (Toggle Plain Text)
  1. uses crt;
  2. type
  3. nodePointer = ^node;
  4. node = record
  5. word : string;
  6. next : nodePointer;
  7. end;
  8. list = nodePointer;
  9.  
  10. var
  11. wordlist:list;
  12. words :nodePointer;
  13. newword :nodePointer;
  14. input : string;
  15. i : integer;
  16. position:integer;
  17. cut :string;
  18.  
  19. begin
  20. clrscr;
  21. new(wordlist);
  22. wordlist:=nil;
  23. write('Enter a sentence:');
  24. readln(input);
  25. input:=input+' ';
  26.  
  27. for i:=1 to length(input) do
  28. begin
  29. if input[i]=' ' then
  30. begin
  31. position:=pos(input[i],input);
  32. cut:=copy(input,1,position-1);
  33. delete(input,1,position);
  34. new(newword);
  35. newword^.word := cut;
  36. newword^.next := wordlist;
  37. wordlist := newword;
  38. end;
  39. end;
  40. words:=wordlist;
  41. while words <> nil do
  42. begin
  43. write(words^.word);
  44. words := words^.next;
  45. end;
  46. end.
Last edited by havoc433; Oct 4th, 2009 at 1:44 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
havoc433 is offline Offline
4 posts
since Oct 2009
Oct 5th, 2009
0

Re: How to find the most common word in a Linked List in Pascal.

Click to Expand / Collapse  Quote originally posted by havoc433 ...
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

Pascal and Delphi Syntax (Toggle Plain Text)
  1. uses crt;
  2. type
  3. nodePointer = ^node;
  4. node = record
  5. word : string;
  6. next : nodePointer;
  7. end;
  8. list = nodePointer;
  9.  
  10. var
  11. wordlist:list;
  12. words :nodePointer;
  13. newword :nodePointer;
  14. input : string;
  15. i : integer;
  16. position:integer;
  17. cut :string;
  18.  
  19. begin
  20. clrscr;
  21. new(wordlist);
  22. wordlist:=nil;
  23. write('Enter a sentence:');
  24. readln(input);
  25. input:=input+' ';
  26.  
  27. for i:=1 to length(input) do
  28. begin
  29. if input[i]=' ' then
  30. begin
  31. position:=pos(input[i],input);
  32. cut:=copy(input,1,position-1);
  33. delete(input,1,position);
  34. new(newword);
  35. newword^.word := cut;
  36. newword^.next := wordlist;
  37. wordlist := newword;
  38. end;
  39. end;
  40. words:=wordlist;
  41. while words <> nil do
  42. begin
  43. write(words^.word);
  44. words := words^.next;
  45. end;
  46. end.

Dear Frind
Ther are several sulotion for your problem.
I send you tow sulotion hear

1-
Pascal and Delphi Syntax (Toggle Plain Text)
  1. uses crt;
  2. type
  3. nodePointer = ^node;
  4. node = record
  5. word : string;
  6. next : nodePointer;
  7. end;
  8. list = nodePointer;
  9.  
  10. var
  11. wordlist:list;
  12. words :nodePointer;
  13. newword :nodePointer;
  14. input : string;
  15. i : integer;
  16. position:integer;
  17. cut :string;
  18. Count :Integer;
  19. Find :nodePointer;
  20.  
  21. begin
  22. clrscr;
  23. new(wordlist);
  24. wordlist:=nil;
  25. write('Enter a sentence:');
  26. readln(input);
  27. input:=input+' ';
  28.  
  29. for i:=1 to length(input) do
  30. begin
  31. if input[i]=' ' then
  32. begin
  33. position:=pos(input[i],input);
  34. cut:=copy(input,1,position-1);
  35. delete(input,1,position);
  36. new(newword);
  37. newword^.word := cut;
  38. newword^.next := wordlist;
  39. wordlist := newword;
  40. end;
  41. end;
  42. words:=wordlist;
  43. while words <> nil do
  44. begin
  45. find:=WordList;
  46. Count:=0;
  47. While Find <> nil do
  48. Begin
  49. if Find^.word=words^.word then
  50. Count:=Count+1;
  51. Find:=Find^.Next;
  52. End;
  53. writeLn(words^.word+'->',Count);
  54. words := words^.next;
  55. end;
  56. ReadLn;
  57. 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-
Pascal and Delphi Syntax (Toggle Plain Text)
  1. uses crt;
  2. type
  3. nodePointer = ^node;
  4. node = record
  5. word : string;
  6. Count : integer;
  7. next : nodePointer;
  8. end;
  9. list = nodePointer;
  10.  
  11. var
  12. wordlist:list;
  13. words :nodePointer;
  14. newword :nodePointer;
  15. input : string;
  16. i : integer;
  17. position:integer;
  18. cut :string;
  19. Count :Integer;
  20. Find :nodePointer;
  21. Sentence:String;
  22.  
  23. begin
  24. clrscr;
  25. new(wordlist);
  26. wordlist:=nil;
  27. write('Enter a sentence:');
  28. readln(input);
  29. input:=input+' ';
  30.  
  31. While length(input)>0 do
  32. begin
  33. position:=pos(' ',input);
  34. if Position>0 then
  35. begin
  36. cut:=copy(input,1,position-1);
  37. If Cut<>'' then
  38. Begin
  39. delete(input,1,position);
  40. new(newword);
  41. newword^.word := cut;
  42. if WordList=nil then
  43. Begin
  44. Newword^.count:= 1;
  45. newword^.next := wordlist;
  46. wordlist := newword;
  47. End
  48. Else
  49. Begin
  50. count:=1;
  51. Find:= WordList;
  52. While Find^.Next<> nil do
  53. Begin
  54. if Find^.Word=Cut then
  55. Begin
  56. Find^.Count:=Find^.Count+1;
  57. Count:=Count+1;
  58. End;
  59. Find:=Find^.Next;
  60. End;
  61. if Find^.Word=Cut then
  62. Begin
  63. Find^.Count:=Find^.Count+1;
  64. Count:=Count+1;
  65. End;
  66. If Count>1 Then
  67. Count:=0;
  68. Find^.Next:=NewWord;
  69. NewWord^.Count:=Count;
  70. NewWord^.Next:=nil;
  71. End;
  72. End
  73. Else
  74. Delete(Input,1,1);
  75.  
  76. end;
  77. end;
  78. words:=wordlist;
  79. Sentence:='';
  80. while words <> nil do
  81. begin
  82. If Words^.Count>0 then
  83. Begin
  84. write(words^.word+'->');
  85. writeln(Words^.Count);
  86. End;
  87. Sentence:=Sentence+' '+Words^.Word;
  88. words := words^.next;
  89. end;
  90. WriteLn;
  91. Writeln('Sentence= ',Sentence);
  92. ReadLn;
  93. end.
Reputation Points: 12
Solved Threads: 10
Junior Poster in Training
fayyaz is offline Offline
66 posts
since Jun 2007
Oct 5th, 2009
0

Re: How to find the most common word in a Linked List in Pascal.

I can't help you with your pascal, but I can tell you what you need to do.

The actual algorithm is pretty simple:

Pascal and Delphi Syntax (Toggle Plain Text)
  1. Get a word from the string
  2. Check if its currently in your linked list
  3. if so increment its counter by 1
  4. else add it to your linked list and set the counter to 1
  5.  
  6. Once you have reached the end of the string
  7. Loop though your linked list
  8. if the count of the next element is greater that the last mostCommon
  9. then set mostCommon to point at that element
  10.  
  11. Return mostCommon

I'm not sure if you can have two values in your linked list? One the actual word and the other a count of how many times its appeared in your string. If not then you could have two linked lists one with the words and one with a count of the words appearance...?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
theouteredge is offline Offline
1 posts
since Oct 2009
Oct 5th, 2009
0
Re: How to find the most common word in a Linked List in Pascal.
what if we use some procedure and function..?
pascal Syntax (Toggle Plain Text)
  1.  
  2.  
  3.  
  4. PROGRAM LinkedList;
  5.  
  6. USES Crt;
  7.  
  8. TYPE
  9. DataType = string;
  10.  
  11. NodePtr = ^ NodeType;
  12. NodeType = RECORD
  13. Data : DataType;
  14. Count:INTEGER;
  15. Next : NodePtr;
  16. END;
  17.  
  18. VAR
  19. Head, Temp : NodePtr;
  20. input : string;
  21. i : integer;
  22. position:integer;
  23. cut :string;
  24.  
  25. { add at end of list }
  26. PROCEDURE AddNode(VAR H : NodePtr; Item : DataType);
  27. VAR
  28. NewNode, Temp : NodePtr;
  29. BEGIN
  30. New(NewNode);
  31. NewNode^.Data := Item;
  32. NEWNODE^.COUNT:=1;
  33. NewNode^.Next := NIL;
  34. IF H = NIL THEN H := NewNode
  35. ELSE
  36. begin
  37. Temp := H;
  38. WHILE (Temp^.Next <> NIL) DO { find last node }
  39. Temp := Temp^.Next;
  40. Temp^.Next := NewNode;
  41. end;
  42. END;
  43.  
  44. FUNCTION SEARCHNODE(H : NodePtr):BOOLEAN;
  45. VAR
  46. Temp : NodePtr;
  47. BEGIN
  48. Temp := H;
  49. SEARCHNODE:=FALSE;
  50. WHILE Temp <> NIL DO
  51. BEGIN
  52. IF Temp^.Data=CUT THEN BEGIN
  53. INC(TEMP^.COUNT);
  54. SEARCHNODE:=TRUE;
  55. BREAK;
  56. END ELSE BEGIN
  57. Temp := Temp^.Next;
  58. END;
  59. END;
  60.  
  61. END;
  62.  
  63. PROCEDURE SHOWNODE(H : NodePtr);
  64. VAR
  65. Temp : NodePtr;
  66. BEGIN
  67. Temp := H;
  68.  
  69. WHILE Temp <> NIL DO
  70. BEGIN
  71. if temp^.data='' then break;
  72. WRITELN( Temp^.Data:10,':',TEMP^.COUNT:10);
  73. Temp := Temp^.Next;
  74.  
  75. END;
  76.  
  77. END;
  78.  
  79.  
  80. PROCEDURE DestroyList(H : NodePtr);
  81. BEGIN
  82. IF H^.Next <> NIL THEN
  83. DestroyList(H^.Next);
  84. Dispose(H);
  85. END;
  86.  
  87. begin { main }
  88. Clrscr;
  89. Head:=NIL; { list is empty at the beginning }
  90. write('Enter a sentence:');{output}
  91. readln(input); {catch the input}
  92. input:=input+' ';
  93.  
  94. for i:=1 to length(input) do begin {step from 1 to the lenght of input}
  95. if input[i]=' ' then begin
  96. position:=pos(input[i],input);
  97. cut:=copy(input,1,position-1);
  98. delete(input,1,position);
  99. IF SEARCHNODE(HEAD)=FALSE THEN BEGIN
  100. ADDNODE(HEAD,CUT);
  101. END;
  102. end;
  103. end;
  104.  
  105. ShowNode(Head); { display all nodes }
  106. DestroyList(Head); { done with list so free up memory }
  107. ReadKey;
  108. end.
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: delphi logon code
Next Thread in Pascal and Delphi Forum Timeline: Ordinal types





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC