|
-
April 8th, 2012, 05:17 PM
#1
Structure Linked Lists: Searching for Words.
So a user inputs an artist name, and then if the input matches one of the nodes containing the artist names i need to return that. but then also if the input does not match any of the nodes, I need to insert a new one. I have some lame attempt below, any guidance to fix it up would be fantastic. This is to be written in C
Also my structure that this function goes off of is shown below as well.
Code:
artistNodePtr findOrInsertArtist( artistNodePtr *sPtr, char *artistID ) {
artistNodePtr newNodePtr;
temp = sPtr
if(temp->artistName_p == artistID){
return temp
}else{
newNodePtr = (artistNodePtr) malloc(sizeof(sPtr)
}
}
Code:
struct artist {
char *artistName_p;
struct disc *disc_p;
struct artist *nextArtist_p;
};
typedef struct artist artist_t;
typedef struct artist *artistNodePtr;
Last edited by chucker; April 8th, 2012 at 05:25 PM.
-
April 8th, 2012, 05:28 PM
#2
Re: Structure Linked Lists: Searching for Words.
 Originally Posted by chucker
So a user inputs an artist name, and then if the input matches one of the nodes containing the artist names i need to return that. but then also if the input does not match any of the nodes, I need to insert a new one.
So where is your basic linked list code? The code to add, search, etc. I don't see any. That is the code you should be working on.
If you had that code, then this function you described above is very easy. Just call the functions that performs the actions you described:
Code:
artistNodePtr addName(const char *name)
{
artistNodePtr ptr = NULL;
ptr = findArtistName( name );
if ( !artistNodePtr )
artistNodePtr = insertNameInList( name );
return artistNodePtr;
}
But again, if the foundation doesn't work, then code such as above doesn't work.
Regards,
Paul McKenzie
-
April 8th, 2012, 05:44 PM
#3
Re: Structure Linked Lists: Searching for Words.
shoot. Well you are not going to like it, you helped me before and said my set up was excessive, but it was also given to me by the proffessor :/ so it is what it is. here is the entirety again
Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define LINEBUFFERSIZE 256
/* Definitions for data structure nodes (artist, disc, track) */
struct song {
char *songName_p;
int trackNumber;
struct song *nextSong_p;
};
struct disc {
char *discName_p;
int year;
struct song *song_p;
struct disc *nextDisc_p;
};
struct artist {
char *artistName_p;9i
struct disc *disc_p;
struct artist *nextArtist_p;
};
typedef struct artist artist_t;
typedef struct disc disc_t;
typedef struct song song_t;
typedef struct artist *artistNodePtr;
typedef struct disc *discNodePtr;
typedef struct song *songNodePtr;
/* function prototypes */
artistNodePtr findOrInsertArtist( artistNodePtr *sPtr, char *artistID );
discNodePtr findOrInsertDisc(discNodePtr *sPtr, char *discID, int releaseYear);
void findOrInsertSong(songNodePtr *sPtr, char *songID, int trackID);
void getNextLine(char buffer[], int bufferSize, FILE *fptr);
/* May need more function prototypes here */
int main(int argc, char *argv[]) {
char lineBuffer[LINEBUFFERSIZE];
artistNodePtr startPtr = NULL;
FILE *musicFile;
char *artistTemp, *discTemp, *yearTemp, *trackTemp, *songTemp;
int year, track;
artistNodePtr theArtist;
discNodePtr theDisc;
if(argc==1) {
printf(" Must supply a file name as command line argument/n");
return 0;
}
if((musicFile = fopen(argv[1], "r")) == NULL) {
printf ("Error opening music file. Program terminated/n");
return 0;
}
getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
while(!feof(musicFile)) {
artistTemp = strtok(lineBuffer,";");
if (artistTemp == NULL) {
printf("Error analyzing input txt file; Program is terminated\n");
return 0;
}
discTemp = strtok(NULL ,";");
if (discTemp == NULL) {
printf("Error analyzing input txt file; Program is terminated\n");
return 0;
}
yearTemp = strtok(NULL ,";");
if (yearTemp == NULL) {
printf("Error analyzing input txt file; Program is terminated\n");
return 0;
}
trackTemp = strtok(NULL ,";");
if (trackTemp == NULL) {
printf("Error parsing input file; Program is terminated\n");
return 0;
}
songTemp = strtok(NULL ,"\n");
if (songTemp == NULL) {
printf("Error parsing input file; Program is terminated\n");
return 0;
}
/* convert the year and track strings to integers using the C atoi() function */
year = atoi(yearTemp);
track = atoi(trackTemp);
theArtist = findOrInsertArtist(&startPtr, artistTemp);
theDisc = findOrInsertDisc(&(theArtist->disc_p), discTemp, year);
findOrInsertSong(&(theDisc->song_p), songTemp, track);
getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
}
/* database display code somewhere here */
}
artistNodePtr findOrInsertArtist( artistNodePtr *sPtr, char *artistID ) {
artistNodePtr newNodePtr;
temp = sPtr
if(temp->artistName_p == artistID){
return temp
}else{
newNodePtr = (artistNodePtr) malloc(sizeof(sPtr)
}
discNodePtr findOrInsertDisc( discNodePtr *sPtr, char *discID, int releaseYear) {
}
void findOrInsertSong(songNodePtr *sPtr, char *songID, int trackID) {
}
void getNextLine(char buffer[], int bufferSize, FILE *fptr) {
char temp;
int i = 0;
buffer[0] = fgetc(fptr);
while ( (!feof(fptr)) && (buffer[i] != '\n') && i<(bufferSize-1)) {
i = i +1;
buffer[i]=fgetc(fptr);
}
if ((i == (bufferSize-1)) && (buffer[i] != '\n')) {
temp = fgetc(fptr);
while (temp != '\n') {
temp = fgetc(fptr);
}
}
buffer[i] = '\0';
}
-
April 8th, 2012, 05:49 PM
#4
Re: Structure Linked Lists: Searching for Words.
Maybe im missing what you mean by the basic linked list code. eh its in there right :/
-
April 8th, 2012, 06:14 PM
#5
Re: Structure Linked Lists: Searching for Words.
added some code for creating the new node if it doesnt exist already. step in the right direction ?
Code:
artistNodePtr findOrInsertArtist( artistNodePtr *sPtr, char *artistID ) {
artistNodePtr newNodePtr;
temp = sPtr
if(temp->artistName_p == artistID){
return temp
} else{
artistNodePtr newNodePtr = (artistNodePtr)malloc(sizeof(artist_t));
if (newNodePtr != NULL) {
newNodePtr->artistName_p = (char*)malloc((strlen(artistID)+1)*sizeof(char));
if (newNodePtr->artistName_p != NULL) {
strcpy(newNodePtr->artistName_p, artistID);
}
newNodePtr->nextArtist_p = NULL;
newNodePtr->disc_p = NULL;
}
-
April 8th, 2012, 06:40 PM
#6
Re: Structure Linked Lists: Searching for Words.
Let's say I have a linked list of numbers. Basic operations on this list would be to insert a number, delete a number, and find a number. Let us also suppose I impose the restriction that the list must be sorted and the numbers unique. Well, before any insert operation I would need to first see if that number is in the list. If so, the quit. Otherwise I proceed with the insert, making sure to insert at the correct "index" such that the new list is still sorted.
This is what Paul is getting at. Forget about being specific and trying to do all the work in one function. Break down the operations you need into smaller functions. A find function simply needs to start at the head of your list, and go through comparing the data items with a passed in key and then return a pointer to the correct node if it exists.
-
April 8th, 2012, 07:17 PM
#7
Re: Structure Linked Lists: Searching for Words.
k i need to make a new function like so then?:
artistNodePtr FindArtist( artistNodePtr *sPtr, char *artistID); {
(pseudo)
while sptr != null
if sptr -> data = artistID
return sptr
else sptr -> nextartist_p
-
April 8th, 2012, 07:29 PM
#8
Re: Structure Linked Lists: Searching for Words.
I think adding such a function makes sense. I assume you mean:
Code:
(more C-ish pseudocode :P)
while sptr != null
if sptr -> data == artistID
return sptr
else
sptr = sptr -> nextartist_p
-
April 8th, 2012, 07:32 PM
#9
Re: Structure Linked Lists: Searching for Words.
thank u. aah, do i need to use the strcmp function since im dealling with letters of a word too? or no?
-
April 8th, 2012, 07:56 PM
#10
Re: Structure Linked Lists: Searching for Words.
That depends, if you have a unique integer ID for the artists, you can use ==. Otherwise, if you are trying to compare the actual name, then you will need to use strcmp. Of course, if this was C++ and you were using the string class, then == would work as expected.
-
April 8th, 2012, 09:10 PM
#11
Re: Structure Linked Lists: Searching for Words.
would this be how to strcmp:
Code:
artistNodePtr FindArtist( artistNodePtr *sPtr, char *artistID); {
while (artistNodePtr != NULL){
if (strcmp(sPtr ->artistName_p == artistID)){
return artistNodePtr;
}
else{
artistNodePtr = artistNodePtr-> nextArtist_p;
-
April 8th, 2012, 10:28 PM
#12
Re: Structure Linked Lists: Searching for Words.
 Originally Posted by chucker
would this be how to strcmp:
No. Are you reading the documentation on the strcmp() function, or just guessing how it's used?
Some advice:
If you have a question concerning how a function such as strcmp() works, why not just write a tiny main() program to test how it works?
Code:
#include <string.h>
#include <stdio.h>
int main()
{
int retVal = strcmp("abc", "123");
printf("The value of strcmp when I compared is %d\n", retVal);
retVal = strcmp("abc", "abc");
printf("The value of strcmp when I compared is %d\n", retVal);
retVal = strcmp("123", "abc");
printf("The value of strcmp when I compared is %d\n", retVal);
}
That is how programmers who do not know how a function works goes about learning how it works -- by writing small programs such as above.
The strcmp() function takes two arguments and returns a value depending on whether the value are less than, greater, or equal to each other.
Regards,
Paul McKenzie
-
April 9th, 2012, 01:30 AM
#13
Re: Structure Linked Lists: Searching for Words.
Okay so I tried doing strcmp correctly after reading about it, but its giving the error: request for member 'artistName_p' in something not a structure. i mean it is a part of the structure, so why does it not work? I guess the way i have my program set up i need to use another method besides strcmp?
Code:
artistNodePtr findOrInsertArtist( artistNodePtr *sPtr, char *artistID ) {
char name[20];
printf("enter an artist name");
scanf("%s", &name);
artistID = name;
while (sPtr != NULL){
if (strcmp(artistID, sPtr->artistName_p)==0){
-
April 9th, 2012, 02:01 AM
#14
Re: Structure Linked Lists: Searching for Words.
The problem is not with strcmp, but with your typedef usage.
Code:
artistNodePtr findOrInsertArtist( artistNodePtr *sPtr, char *artistID )
-
April 9th, 2012, 02:09 AM
#15
Re: Structure Linked Lists: Searching for Words.
crap. This format they gave us to work with... kinda wish i just went and did my own thing instead. Lol have any alternatives or ways to make it work? Like i could i just erase the typedef and would it still work? Or how about throwing "typedef struct artist *artistNodePtr;" into the struct instead of typedefing it
Last edited by chucker; April 9th, 2012 at 02:13 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|