![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
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. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 555
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
|
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;
} |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Newbie
|
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 ![]() |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
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) void addIntoLinkList(cityNode *pLinkedList, char tempCity, double x, double y) 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. |
|
|
|
|
|
#8 |
|
Newbie
|
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; |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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
...
}
__________________
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 |
|
|
|
|
|
#10 |
|
Newbie
|
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? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|