Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 28th, 2005, 6:55 AM   #1
Silme
Newbie
 
Silme's Avatar
 
Join Date: Oct 2005
Posts: 7
Rep Power: 0 Silme is on a distinguished road
Send a message via MSN to Silme
Circularly Linked Lists

warm greetings,

I have this assignment due next Friday and i'm really strugling with solution.
I'm new to programming and this doesnt make it any easier.
The thing is that i have to read from file ( these file contain sting and two doubles) in a line. (City name and its coordinates - x and y).
I've done the part of reading from the file but now i have to store those values in circularly linked list. The first node has to be the lowest in alphabetical order from the city names in file. With the rest of them i need to compare the distance and print the shortest one (travelling salesman problem).
So i was just wondering if someone would be able to help me here.
Im not including any codes here cause im not sure which part of it would be useful.
I know that i have to create new node and then add it to the list but as i said im not familiar with it too much.
thank you.
Silme is offline   Reply With Quote
Old Oct 28th, 2005, 7:03 AM   #2
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 555
Rep Power: 5 Benoit is on a distinguished road
Post what code you have so far and we'll help A lot of people come on here asking us to do their homework without attempting it first...Good luck!
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Oct 28th, 2005, 7:10 AM   #3
Silme
Newbie
 
Silme's Avatar
 
Join Date: Oct 2005
Posts: 7
Rep Power: 0 Silme is on a distinguished road
Send a message via MSN to Silme
well im really trying hard but these link lists are ... difficult
#include <stdio.h>
#include <stdlib.h>

#define CITYNAMESIZE 30

struct cityRecord
{
   double x, y;
   char city[CITYNAMESIZE];
   struct cityRecord *next;
};

typedef struct cityRecord cityNode;

void getFiles(char filename[]);
FILE* fileOpen(char *filename, char *mode);
void exitIfFault(FILE *fp, char filename[]);
void reorderList();
void printDistance();
void calculateDistance();
void selectFirstNode();
void printList();
cityNode* createNode(double x, double y, char tempCity);
void addIntoLinkList(char tempCity, double x, double y);

int main(int argc, char *argv[])
{
   cityNode *start;
   
   start = NULL;
         
   getFiles(argv[1]);
   /* printList */
   /* selectFirstNode */
   /* calculateDistance */
   /* printDistance */
   /* reorderList */
   /* printList */
   /* calculateDistance */
   /* printDistance */   
   
   return 0;
}

void printList()
{
}

void selectFirstNode()
{
}

void calculateDistance()
{
}

void printDistance()
{
}

void reorderList()
{
}

cityNode* createNode(double x, double y, char tempCity)
{
   cityNode *tempPtr;
   
   tempPtr = (cityNode*) malloc(sizeof(cityNode));
   if(tempPtr == NULL)
   {
      printf("Memory allocation error\n");
      exit(EXIT_FAILURE);      
   }
   tempPtr->x = x;
   tempPtr->y = y;
   tempPtr->city = tempCity;
   tempPtr->next = NULL;
   
   return tempPtr;
}

void addIntoLinkList(char tempCity, double x, double y)
{
   cityNode *temp, *prev, *loc;
   temp = createNode(tempCity,x,y);
   if(*start ==NULL)
   {
      /* add first item to the list */
      *start = temp;
   }
   
   
}
void getFiles(char filename[])
{
   FILE *fp;      /* defines a file pointer */
   int status;
   double x, y;    
   char tempCity[CITYNAMESIZE];     
   /* open stream to file */
   fp=fileOpen(filename, "r");   
   while(!feof(fp))
   {
      /* lf -long float = double*/
      status = fscanf(fp, "%s%lf%lf", tempCity, &x, &y);       
 
      if(status == -1)
      { 
         /* do nothing */
      } 
      else if(status == 3)      /* if there are 3 arg in the line in the file */
      {
         addIntoLinkList(tempCity,x,y);
         /* addnode */
         /* no printing here */
         printf("%-12s %-8.2f %-8.2f\n", tempCity, x, y);      
      }
      else
      {
         printf("\n");
         printf("ERROR - Corrupted record in file\n");
         exit(0); 
      }
   }    
}
FILE* fileOpen(char *filename, char *mode)
{
   FILE *fp = fopen(filename, mode);   
   if(fp == NULL)
   {
      printf("ERROR - Unable to access %s\n", filename);
      exit(1);
   }
   return fp;
}
Silme is offline   Reply With Quote
Old Oct 28th, 2005, 7:29 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The best approach, when you're new to linked lists, is to make a short one by hand, using paper and pencil (aid to visualization). Let's presume to want to insert a node. Look at your list, observe which pointers you have to change in order to break a link, point out to a new node, and point back in to the original. Don't just scribble. Erase a link name/destination, write in the new info, and continue to the next step in your procedure. Don't rely on your memory, your program won't be able to -- write something you're going to erase to a scratchpad area. You may find that your procedure needs tuned; you've lost a critical value (null pointer, seg fault, out into the weeds goes your program, barfing on its shoes all the way).

You really haven't written anything significant for us to critique. I'm not going to write it for you. On the other hand, I may actually copy/paste the code you produce, get out compilation errors, point out logical errors, and suggest changes.

Someone might come along and do it for you. Better you do it yourself, with all the help we're prepared to give.

If you have a deadline problem, the odds are very good you brought it on yourself as a fan of procrastination or partying.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 28th, 2005, 7:33 AM   #5
Silme
Newbie
 
Silme's Avatar
 
Join Date: Oct 2005
Posts: 7
Rep Power: 0 Silme is on a distinguished road
Send a message via MSN to Silme
yeah, i know there are errors cause i tried to complie and it didnt like it .. no surprise here
and i dont expect that someone will write a code for me , i just need a little guidence through here....
like i have this questions about createNode function...if this is correct...and im still confused if after reading from the file (in getFiles function), should i be calling addIntoLinkList by reference or by value????

and with this paper and pen is good idea, i will try that, thank you
Silme is offline   Reply With Quote
Old Oct 28th, 2005, 7:37 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
It's a very common learning exercise, but I don't recall any really good examples on this particular forum. Why don't you work out just one fuctional item at a time (like insert, or delete), and post back with your troubles.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 28th, 2005, 6:42 PM   #7
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
One thing to start with is that you have to decide how you are going to access your linked list within your functions. The
void addIntoLinkList(char tempCity, double x, double y)
function needs to have a linked list to work with. You can either make your linked list pointer global, or you can pass it in to the function. The second option is better as it means latter on you can have more than one linked list if you need it. Something like
void addIntoLinkList(cityNode *pLinkedList, char tempCity, double x, double y)
I called the variable "pLinkedList" to indicate that it is a pointer to the linked list.
Note that the linked list would have to be passed in from your getFiles function, so it needs to have the linked list as a parameter too, so it can pass it on.
The Dark is offline   Reply With Quote
Old Oct 28th, 2005, 6:59 PM   #8
Silme
Newbie
 
Silme's Avatar
 
Join Date: Oct 2005
Posts: 7
Rep Power: 0 Silme is on a distinguished road
Send a message via MSN to Silme
did you mean something like i have in main function
cityNode *start?
and i have a problem when i try to compile im getting error msg:
"In function createNode
incompatible types in assignment "
its in the line:
tempPtr->city = tempCity;
Silme is offline   Reply With Quote
Old Oct 28th, 2005, 7:59 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
One would need to see the entire error message to know precisely what the problem is. Very, very often, it's a mismatch in the level of indirection, or a misperception between the difference between an array name and a pointer. Sometimes it's as simple as the difference between a char and an array of char. Look at your function definition:
cityNode* createNode(double x, double y, char tempCity)
{
   ...
   blah blah blah
   ...
}
This says that "tempCity" is A SINGLE CHARACTER, from the viewpoint of what the function expects to find in its argument list. That would (in English) restrict you to a maximum of 26 distinct cities. I doubt that's what you truly intend. I suspect you meant to reference an array of char.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 28th, 2005, 8:20 PM   #10
Silme
Newbie
 
Silme's Avatar
 
Join Date: Oct 2005
Posts: 7
Rep Power: 0 Silme is on a distinguished road
Send a message via MSN to Silme
oh,yeah, i know what do you mean, and i've changed it, so it says char tempCity[], but still im getting the same error msg.
now i have

cityNode* createNode(char tempCity[],double x, double y)

error says:
In function `createNode':
assign2.c:76: incompatible types in assignment

that's whole msg.

and line 76 is:

tempPtr->city = tempCity;

as far as i understand it, from the fscanf im reading what is in the file - string, double, double. Then im sending it to the function addIntoLinkList as (tempCity,x,y), in addIntoLInkList function im creating new node temp by sending values tempCity, x, y to createNode function.
and there i have tempPtr which allocates space for this node and sets data into it.
So im trying to assign tempCity (array of strings) into city (array of strings).
Is there something im missing?
Silme is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC