11 Replies - 16160 Views - Last Post: 18 October 2006 - 09:46 AM Rate Topic: -----

#1 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

ordered linked list

Post icon  Posted 12 October 2006 - 09:03 AM

what to know if im on the right track for this ordered linked list I'm just lost as to where to put the InsertNode. SO that it will sort teh linked list then print it to the file. If there is data in the file i hope that it will be sorted. Bu t i need my list to sort the data in the file and add new data to it.
##include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#define Nlen	26

typedef struct person_t {
   char FName[Nlen];
   char LName[Nlen];
   char Sex;
   char Grade;
   struct person_t *Next;
  } PERSON;

//Funstion to Ordered linked list
PERSON* InsertNode (PERSON *Top, PERSON *Node )
{
PERSON *Here, /* Node being compared to new node */  
*Prev; /* Node in front of Here node */
if (Top == NULL)/* List is empty  Place new node at top of list */
Top = Node;
else { /* Top != NULL *//* Start at Top of list */
Here = Top;
if ( Here->LName > Node->LName ) {
Node->Next = Top;
Top = Node;
}
else {/* Search for place to insert new node */
Prev = Top;
Here = Prev->Next;
while ( (Here != NULL) &&
(Here->LName < Node->LName) ) {
Prev = Here;
Here = Here->Next;
}/* Insert into list at found spot */
Node->Next = Prev->Next;
Prev->Next = Node;
}
} /* end of Top != NULL */
return Top;
}

void amend_file( char file[], PERSON *STU );
void show_file( char file[], PERSON *STU );
void PRINT_file( char file[], PERSON *STU );

int main (void)
{
   PERSON *STU;
   int Ans;
		char file[] = "data.txt";
	int Records [100];
	
	
  do
   {
	  printf("\n\n"
			 "1. Enter new record\n"
			 "2. Display List\n"
			 "3. Quit\n\n");
	  printf("Prompt: ");
	  fflush(stdout);
	  if ( scanf("%d", &Ans) == 1 ) /* get answer */
	  {
		 STU = (PERSON*) malloc (sizeof (PERSON));
		 if (STU == NULL)
		 {
			perror("PERSON");
			exit(EXIT_FAILURE);
		 }
		 switch(Ans)
		 {
			case 1:
			   amend_file(file, STU);
			  break;
			case 2:
			   show_file(file, STU);
			   break;
			case 3: default:
			   Ans = 3;
			   break;
		 }
	  } /* /get answer */
   }
	while ( Ans != 3 );
	
	  fflush(stdout);
   free(STU);
   return EXIT_SUCCESS;
}
/*
* amend_file: Enter stuff into the working file.
* Very much a bare bones function so far; work on making this better
* Perhaps make it work with a linked list, as your struct suggests?
*/
void amend_file( char file[], PERSON *STU )
{
   assert(STU != NULL);
   FILE *fin = fopen(file, "a");
   if ( fin == NULL )
   {
	  perror(file);
	  exit(EXIT_FAILURE);
   }
   printf("Enter new record: ");
   printf ("\nEnter students First name ( up to %d letters):", Nlen);
   printf ("\nEnter students  Last name  ( up to %d letters):", Nlen);
   printf ("\nPlease enter Sex");
   printf ("\nPlease enter Grade\n");
   scanf("%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
	  &STU->Grade);
 InsertNode;
	
   fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
			  fflush(fin);
			  fclose(fin);  
	  }
/*
* show_file: Reads the working file.
* Bare bones again...
*/
void show_file ( char file[], PERSON *STU )
{
  assert(STU != NULL);
  int b = 1;
  FILE *fout = fopen(file, "r");
	
  if (file == NULL)
  {
	perror(file);
	exit(EXIT_FAILURE);
  }
   while (fscanf(fout, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
	 &STU->Grade) == 4)
  {
	InsertNode;			   
	printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
	   STU->Sex, STU->Grade);
	b++;
  }
  fclose(fout);
}











This post has been edited by mitchelltab: 12 October 2006 - 01:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: ordered linked list

#2 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: ordered linked list

Posted 13 October 2006 - 02:09 AM

you may have aproblem here....
PERSON* InsertNode (PERSON *Top, PERSON *Node )
{
	PERSON *Here, /* Node being compared to new node */  
	*Prev; /* Node in front of Here node */
	if (Top == NULL)/* List is empty  Place new node at top of list */
		Top = Node;
	else { /* Top != NULL *//* Start at Top of list */
		Here = Top;
		if ( Here->LName > Node->LName ) {
			Node->Next = Top;
			Top = Node;
		}
		else {/* Search for place to insert new node */
			Prev = Top;
			Here = Prev->Next;
			while ( (Here != NULL) && (Here->LName < Node->LName) ) {
				Prev = Here;
				Here = Here->Next;
		}/* Insert into list at found spot */
	Node->Next = Prev->Next;
	Prev->Next = Node;
	}
} /*


with so much code, its getting hard to read that clearly.

I did a project that used a struct and we did all of the file read, sort, display and free up in under 200 lines of code... demonstrates what a little planning will do.

Mitchell - have you tried to step your C code in the compiler?
Was This Post Helpful? 0
  • +
  • -

#3 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: ordered linked list

Posted 13 October 2006 - 09:04 AM

I can run it in the complier with no issues. I know that my InsetNode funcation. Has not been added to the code. Because i'm not putting it in there correctly. That is where im having my issue. I know that the code might be off as well. SO i dont know
Was This Post Helpful? 0
  • +
  • -

#4 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: ordered linked list

Posted 13 October 2006 - 03:45 PM

View Postmitchelltab, on 13 Oct, 2006 - 09:04 AM, said:

I can run it in the complier with no issues. I know that my InsetNode funcation. Has not been added to the code. Because i'm not putting it in there correctly. That is where im having my issue. I know that the code might be off as well. SO i dont know


I ask if you can step throught your code... running from the compiler is not the same thing :)

The better IDE's have code "step into" and "step over" functionality, that way you can observe the code one line at a time....
Was This Post Helpful? 0
  • +
  • -

#5 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: ordered linked list

Posted 13 October 2006 - 03:50 PM

View PostgregoryH, on 13 Oct, 2006 - 03:45 PM, said:

View Postmitchelltab, on 13 Oct, 2006 - 09:04 AM, said:

I can run it in the complier with no issues. I know that my InsetNode funcation. Has not been added to the code. Because i'm not putting it in there correctly. That is where im having my issue. I know that the code might be off as well. SO i dont know


I ask if you can step throught your code... running from the compiler is not the same thing :)

The better IDE's have code "step into" and "step over" functionality, that way you can observe the code one line at a time....

no i do not know how to do that
Was This Post Helpful? 0
  • +
  • -

#6 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: ordered linked list

Posted 13 October 2006 - 03:51 PM

what compiler are you using?
Was This Post Helpful? 0
  • +
  • -

#7 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: ordered linked list

Posted 13 October 2006 - 04:18 PM

View PostgregoryH, on 13 Oct, 2006 - 03:51 PM, said:

what compiler are you using?

dev C++
Was This Post Helpful? 0
  • +
  • -

#8 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: ordered linked list

Posted 13 October 2006 - 04:42 PM

View Postmitchelltab, on 13 Oct, 2006 - 04:18 PM, said:

View PostgregoryH, on 13 Oct, 2006 - 03:51 PM, said:

what compiler are you using?

dev C++

good...

I can assist you with this. You may have noticed a number of buttons that preform compile, run, etc.

First step is to alter your project. Press Alt+P. This should open up the project dialog

The dialog has tabs (in a slightly different format to the usual) located just below the title bar. Click on "Compiler".

A new portion of the dialog is displayed. First item is "Default Compiler", then there are two windows side by side.

In the left window, click on "Linker", now in the right window change "Generate debugging information" from No to Yes (use your mouse, point and click)

re-compile the code. use the help to find out how to set break points and now you can see you code executing one line at a time......

this is one way to help you see what your program is doing and help you fix problems
Was This Post Helpful? 0
  • +
  • -

#9 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: ordered linked list

Posted 13 October 2006 - 04:47 PM

View PostgregoryH, on 13 Oct, 2006 - 04:42 PM, said:

View Postmitchelltab, on 13 Oct, 2006 - 04:18 PM, said:

View PostgregoryH, on 13 Oct, 2006 - 03:51 PM, said:

what compiler are you using?

dev C++

good...

I can assist you with this. You may have noticed a number of buttons that preform compile, run, etc.

First step is to alter your project. Press Alt+P. This should open up the project dialog

The dialog has tabs (in a slightly different format to the usual) located just below the title bar. Click on "Compiler".

A new portion of the dialog is displayed. First item is "Default Compiler", then there are two windows side by side.

In the left window, click on "Linker", now in the right window change "Generate debugging information" from No to Yes (use your mouse, point and click)

re-compile the code. use the help to find out how to set break points and now you can see you code executing one line at a time......

this is one way to help you see what your program is doing and help you fix problems
thanks for the insit. I will try that one. if i need help i'll ask if that is ok
Was This Post Helpful? 0
  • +
  • -

#10 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: ordered linked list

Post icon  Posted 17 October 2006 - 08:18 AM

I changed the code. I know that I'm still having an issues reading the last name into the lineked list. Do i need to do a funcation that inserts it or do the ordered funcation i have do that for me. And if this is the case. Can someone please tell me what I'm missing. Im so lost.
  #include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define Nlen	26

typedef struct person_t {
   char FName[Nlen];
   char LName[Nlen];
   char Sex;
   char Grade;
   struct person_t *Next;
  } PERSON;

struct person_t  *Top = NULL;		 
struct person_t  *Node = NULL;
/*********************************************************************
 Funstion to Ordered linked list
 This is where i will call the Last name and put it into the linked   
 list ordered by last name. 
 
********************************************************************/
PERSON* InsertNode (PERSON *Top, PERSON *Node )
{
PERSON *Here, /* Node being compared to new node */  
*Prev; /* Node in front of Here node */
if (Top == NULL)/* List is empty  Place new node at top of list */
Top = Node;
else { /* Top != NULL *//* Start at Top of list */
Here = Top;
if (Here->LName > Node->LName) {			   
Node->Next = Top;
Top = Node;
}
else {/* Search for place to insert new node */
Prev = Top;
Here = Prev->Next;
while ( (Here != NULL) &&
(Here->LName < Node->LName) ) {
Prev = Here;
Here = Here->Next;
}/* Insert into list at found spot */
Node->Next = Prev->Next;
Prev->Next = Node;
}
} /* end of Top != NULL */
return Top;
}
/*************************************************************
end of  Funstion to Ordered linked list
***************************************************************/

void amend_file( char file[], PERSON *STU ); 
void show_file( char file[], PERSON *STU );


int main (void)
{
   PERSON *STU;
   int Ans;
		char file[] = "e:data.txt";
	int Records [100];
	 FILE *fin = fopen(file, "a+");
	 struct person_t  *Top = NULL;	
	struct person_t  *Node = NULL;
	
	
	
  do
   {
	  printf("\n\n"
			 "1. Enter new record\n"
			 "2. Display List\n"
			 "3. Quit\n\n");
	  printf("Prompt: ");
	  fflush(stdout);
	  if ( scanf("%d", &Ans) == 1 ) /* get answer */
	  {
		fgets(STU->FName,sizeof(STU->FName),stdin);
		 if (STU == NULL)
		 {
			perror("PERSON");
			exit(EXIT_FAILURE);
		 }
		 switch(Ans)
		 {
			case 1:
			   amend_file(file, STU);  //calls the amend file funcation 
			  break;
			case 2:
			   show_file(file, STU);  //calls teh show file funcation
			   break;
			case 3: default:
			   Ans = 3;
			   break;
		 }
	  } /* /get answer */
   }
	while ( Ans != 3 );
	
	  fflush(stdout);
	  free(STU);
	  getchar ();
   return EXIT_SUCCESS;
}
/*************************************************************************
* amend_file: Enter stuff into the working file.
*
**************************************************************************/
void amend_file( char file[], PERSON *STU )
{
   assert(STU != NULL);
   FILE *fin = fopen(file, "a+");
   if ( fin == NULL )
   {
	  perror(file);
	  exit(EXIT_FAILURE);
   }
   printf("Enter new record: ");
   printf ("\nEnter students First name ( up to %d letters):", Nlen);
   printf ("\nEnter students  Last name  ( up to %d letters):", Nlen);
   printf ("\nPlease enter Sex");
   printf ("\nPlease enter Grade\n");
   scanf("%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
	  &STU->Grade);
	   Top = InsertNode (Top, Node);  //calls the insert Node funcation  which it is not getting into becuase
				//Im haveing issues with getting the file to print. 
			fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
		   fflush(fin);
			fclose(fin); 
	  }
/******************************************************************************
* show_file: Reads the working file.
* Bare bones again...
******************************************************************************/
void show_file ( char file[], PERSON *STU )
{
  assert(STU != NULL);
  int b = 1;
  FILE *fin = fopen(file, "r");
	
  if (file == NULL)
  {
	perror(file);
	exit(EXIT_FAILURE);
  }
  while (fscanf(fin, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
	 &STU->Grade)== 4)
  {   
   Top = InsertNode (Top, Node);  //read insernode function
	b++;		  
	printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
	   STU->Sex, STU->Grade);
  }
  fclose(fin);
}



Was This Post Helpful? 0
  • +
  • -

#11 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: ordered linked list

Posted 18 October 2006 - 02:41 AM

View Postmitchelltab, on 17 Oct, 2006 - 08:18 AM, said:

I changed the code. I know that I'm still having an issues reading the last name into the lineked list. Do i need to do a funcation that inserts it or do the ordered funcation i have do that for me. And if this is the case. Can someone please tell me what I'm missing. Im so lost.


Your code is a little better,,, some immediate observations (for you and those that review)
  • Try to use some white spcae (as in use tabs and returns) to spread the code out a bit and show structure,,, I had trouble spotting the second if statement in the InsertNode routine.
  • PERSON * STU is just a 32 bit pointer.... how do you suggest your data squeezed into that? You must allocate memory, see here
  • In main() there is only one release of near the endPERSON, yet your code allocates several (to the number of records) and the user can add more inside the do loop.
  • char file[] = "e:data.txt"; you need to add a "\\" like this: char file[] = "e:\\data.txt";. fopen() fails because of this
  • when you want to get first into the record, did you get any type of error?
Your main problem stems from the fact that you never allocate space for data, so it may seem to work, but give you strange results. A good compiler would pick up more things and you are more likely to get runtime errors with your code than you indicate.

first... learn how to allocate and deallocate memory.. then see where that must be done for your program to work.
Was This Post Helpful? 0
  • +
  • -

#12 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: ordered linked list

Post icon  Posted 18 October 2006 - 09:46 AM

I found out that I have not been allicating the space. so I added a malloc. But know im getting this error message. And need some help please
89 H:\lab4a.cpp invalid conversion from `void*' to `PERSON*' the line that is goiving me the error is in read. ANd ides please
 #include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>


#define Nlen	26

typedef struct person_t {
   char FName[Nlen];
   char LName[Nlen];
   char Sex;
   char Grade;
   struct person_t *Next;
  } PERSON;

struct person_t  *Top = NULL;		 
struct person_t  *Node = NULL;


/*********************************************************************
 Funstion to Ordered linked list
 This is where i will call the Last name and put it into the linked   
 list ordered by last name. 
 
********************************************************************/
PERSON* InsertNode (PERSON *Top, PERSON *Node )
{
PERSON *Here, /* Node being compared to new node */  
*Prev; /* Node in front of Here node */

	if (Top == NULL)/* List is empty  Place new node at top of list */
	  Top = Node;
		 else { /* Top != NULL *//* Start at Top of list */
		  Here = Top;
		  
	strcmp (Here->LName , Node->LName);			 
	 Node->Next = Top;
	 Top = Node;
	
		  // else {/* Search for place to insert new node */
			// Prev = Top;
			 //Here = Prev->Next;
 
   while ( (Here != NULL) &&(Here->LName < Node->LName) ) {
	   Prev = Here;
	   Here = Here->Next;
		}/* Insert into list at found spot */
		
Node->Next = Prev->Next;
Prev->Next = Node;
} /* end of Top != NULL */
return Top;
}
/*************************************************************
end of  Funstion to Ordered linked list
***************************************************************/

void amend_file( char file[], PERSON *STU ); 
void show_file( char file[], PERSON *STU );


int main (void)
{
   PERSON *STU;
   int Ans;
   char file[] = "data.txt";
	int Records [100];
	 FILE *fin = fopen(file, "a+");
	 struct person_t  *Top = NULL;	
	struct person_t  *Node = NULL;
	
	
	
  do
   {
	  printf("\n\n"
			 "1. Enter new record\n"
			 "2. Display List\n"
			 "3. Quit\n\n");
	  printf("Prompt: ");
	  fflush(stdout);
	 

		 
	  if ( scanf("%d", &Ans) == 1 ) /* get answer */
	 {			
		  [color=#FF0000]STU = malloc(sizeof(PERSON));[/color]
		 if (STU == NULL){
	   printf("Out of memeory");
		exit(0);
			 }  
 
		fgets(STU->FName,sizeof(STU->FName),stdin);
		 if (STU == NULL)
		 {
			perror("PERSON");
			exit(EXIT_FAILURE);
		 }
		 
		 switch(Ans)
		 {
		  
			  case 1:
			   amend_file(file, STU);  //calls the amend file funcation 
			  break;
		   
			case 2:
			   show_file(file, STU);  //calls teh show file funcation
			   break;
		   
			case 3: default:
			   Ans = 3;
			   break;
		 }
	  } /* /get answer */
   }
	
	
	 while ( Ans != 3 );
	
	  fflush(stdout);
	  free(STU);
	  getchar ();
   return EXIT_SUCCESS;
}

/*************************************************************************
* amend_file: Enter stuff into the working file.
*
**************************************************************************/
void amend_file( char file[], PERSON *STU )
{
	 int i;
   assert(STU != NULL);
	STU = (struct person_t *) malloc(sizeof(person_t));
   FILE *fin = fopen(file, "a+");
   if ( fin == NULL )
   {
	  perror(file);
	  exit(EXIT_FAILURE);
   }
   printf("Enter new record: ");
   printf ("\nEnter students First name ( up to %d letters):", Nlen);
   printf ("\nEnter students  Last name  ( up to %d letters):", Nlen);
   printf ("\nPlease enter Sex");
   printf ("\nPlease enter Grade\n");
   scanf("%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
	  &STU->Grade);
	  
	  InsertNode (Top, Node);
		fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
		   fflush(fin);
			fclose(fin); 
	  }


/******************************************************************************
* show_file: Reads the working file.
* Bare bones again...
******************************************************************************/
void show_file ( char file[], PERSON *STU )
{
  assert(STU != NULL);
  int b = 1;
  FILE *fin = fopen(file, "rb");
	
  if (file == NULL)
  {
	perror(file);
	exit(EXIT_FAILURE);
  }
  while (fscanf(fin, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
	 &STU->Grade)== 4)
  {				   
	
	  InsertNode (Top, Node);
	  b++;		  
	printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
	   STU->Sex, STU->Grade);
  }
  fclose(fin);
}
 

This post has been edited by mitchelltab: 20 October 2006 - 06:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1