| | |
How to find the most common word in a Linked List in Pascal.
Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
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
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)
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.
Last edited by havoc433; Oct 4th, 2009 at 1:44 pm.
•
•
Join Date: Jun 2007
Posts: 36
Reputation:
Solved Threads: 6
•
•
•
•
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)
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-
Pascal and Delphi Syntax (Toggle Plain Text)
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-
Pascal and Delphi Syntax (Toggle Plain Text)
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.
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 1
I can't help you with your pascal, but I can tell you what you need to do.
The actual algorithm is pretty simple:
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...?
The actual algorithm is pretty simple:
Pascal and Delphi Syntax (Toggle Plain Text)
Get a word from the string Check if its currently in your linked list if so increment its counter by 1 else add it to your linked list and set the counter to 1 Once you have reached the end of the string Loop though your linked list if the count of the next element is greater that the last mostCommon then set mostCommon to point at that element 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...?
0
#4 Oct 5th, 2009
what if we use some procedure and function..?
pascal Syntax (Toggle Plain Text)
PROGRAM LinkedList; USES Crt; TYPE DataType = string; NodePtr = ^ NodeType; NodeType = RECORD Data : DataType; Count:INTEGER; Next : NodePtr; END; VAR Head, Temp : NodePtr; input : string; i : integer; position:integer; cut :string; { add at end of list } PROCEDURE AddNode(VAR H : NodePtr; Item : DataType); VAR NewNode, Temp : NodePtr; BEGIN New(NewNode); NewNode^.Data := Item; NEWNODE^.COUNT:=1; NewNode^.Next := NIL; IF H = NIL THEN H := NewNode ELSE begin Temp := H; WHILE (Temp^.Next <> NIL) DO { find last node } Temp := Temp^.Next; Temp^.Next := NewNode; end; END; FUNCTION SEARCHNODE(H : NodePtr):BOOLEAN; VAR Temp : NodePtr; BEGIN Temp := H; SEARCHNODE:=FALSE; WHILE Temp <> NIL DO BEGIN IF Temp^.Data=CUT THEN BEGIN INC(TEMP^.COUNT); SEARCHNODE:=TRUE; BREAK; END ELSE BEGIN Temp := Temp^.Next; END; END; END; PROCEDURE SHOWNODE(H : NodePtr); VAR Temp : NodePtr; BEGIN Temp := H; WHILE Temp <> NIL DO BEGIN if temp^.data='' then break; WRITELN( Temp^.Data:10,':',TEMP^.COUNT:10); Temp := Temp^.Next; END; END; PROCEDURE DestroyList(H : NodePtr); BEGIN IF H^.Next <> NIL THEN DestroyList(H^.Next); Dispose(H); END; begin { main } Clrscr; Head:=NIL; { list is empty at the beginning } write('Enter a sentence:');{output} readln(input); {catch the input} input:=input+' '; for i:=1 to length(input) do begin {step from 1 to the lenght of input} if input[i]=' ' then begin position:=pos(input[i],input); cut:=copy(input,1,position-1); delete(input,1,position); IF SEARCHNODE(HEAD)=FALSE THEN BEGIN ADDNODE(HEAD,CUT); END; end; end; ShowNode(Head); { display all nodes } DestroyList(Head); { done with list so free up memory } ReadKey; end.
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
![]() |
Similar Threads
- Linked List (reverse and alphebetical order) (C++)
- Linked Lists: Find length and check for empty list (C++)
- linked list problem in my program (C)
- help needed with C program using a linked list to determine if a word is a palindrome (C)
- how to find maximum value in linked list ?? please help. (C)
- Linked List (C++)
- help me on linked list (C)
- how to compare stings in linked list (C)
- Bumping my object on a linked list (C++)
- linked list library (C)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: delphi logon code
- Next Thread: Ordinal types
| Thread Tools | Search this Thread |
Tag cloud for Pascal and Delphi





