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.

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.

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

The actual algorithm is pretty simple:

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...?

what if we use some procedure and function..?

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 part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.