I'm trying to create an alphabetically ordered linked list from a file by placing the node in the correct spot after reading it. The file must not be alphabetically ordered. The program reads the file correctly and I'm able to add everything at the end of the list.
Place search_place(Place first, char *new){
Place aux = first;
while (aux->abcnext != NULL){
if ( strcmp(new,aux->place) > 0)
aux = aux->abcnext;
else
break;
}
return aux;
}
void insert_place(Place first, char* string){
Place previous,temp,new;
previous = search_place(first, string);
if (previous->abcnext == NULL){
new = create_place();
previous->place = string;
new->abcnext = previous->abcnext;
previous->abcnext = new;
}
else{
new = (Place)malloc(sizeof(place_node));
new->place = string;
new->abcnext = previous;
previous = new;
}
}
Place create_place(){
Place aux;
aux=(Place)malloc(sizeof(place_node));
if (aux!=NULL){
aux->place=malloc(25*sizeof(char));
aux->abcnext=NULL;
}
return aux;
}
typedef struct placenode*Place;
typedef struct placenode{
char *place;
Place abcnext;
}place_node;
Considering the results that I've obtained from this code I suppose the problem is related to either pointers or the header of the linked list or both. With 4 places: P, Z, W, L - I get only P -> Z from the list.
if (previous->abcnext == NULL){
new = create_place();
previous->place = string;
new->abcnext = previous->abcnext;
previous->abcnext = new;
}
A couple of obvious problems with the above code. Firstly, you don't set new->place - you replace previous->place which doesn't seem right. So your new node will have NULL for it's "place" and you'll have lost the value for the previous node.
Secondly you're assigning the value of string rather than making a new copy. If you're using the same string each time you call the function, you'd end up with all the nodes pointing to the same string.
You should do something like
new->place = malloc(strlen(string)+1);
strcpy(new->place, string);
or if your version of C has it, use strdup
new->place = strdup(string);
Related
im trying to insert one list in the end of another
with this code:
typedef struct Element
{
int vKey;
char vInfo[30];
struct Element *pNext;
} tsElement;
typedef struct
{
tsElemento *pFirst;
int vLength;
} tsLDSE;
void Unir(tsLDSE *pListIn, tsLDSE *pListOut)
{
tsElement *pElementOut;
pElementOut = pListOut->pFirst;
while (pElementOut != NULL)
{
pElementOut = pElemenoOut->pNext;
}
pElementOut = pListIn->pFirst;
pListOut->vLength = pListOut->vLength + pListIn->vLength ;
}
i checked printing the adresses, pElementoOut is really the end of the first list and is poiting to NULL, then it receives the fisrt adress of the second list, but when i print it out it only prints the first list and i can't figure out why.
Your function Unir only adds the length of the input list to the length of the output list.
This loop:
while (pElementOut != NULL)
{
pElementOut = pElemenoOut->pNext;
}
Only gets pElementOut to be NULL.
In addition, when you write pElementOut = pListIn->pFirst;, all you change is the local variable pElementOut.
What you want to do instead is this:
while (pElementOut->pNext != NULL)
{
pElementOut = pElementOut->pNext;
}
pElementOut->pNext = pListIn->pFirst;
This puts the first element of pListIn at the end of the last element of pListOut.
Also, add a NULL check at the beginning of your function! You can easily get a NULL pointer dereference there if you aren't careful.
I don't know why I can read the Linked list without problems in LABEL : 1 ; but the program just crashes and print grabage in the LABEL : 0 ;
In other terms, why the linked list works fine inside the lecture function , but not outside it ?
Here is my code :
/* including libraries */
#define V 20
typedef struct DATA{
char* NomP;char* NomA;
struct DATA *Next;
}DATA;
// Prototypes .
int main(void)
{
char FileName[V];
puts("Data file ? : ");gets(FileName);
FILE* fs = fopen(FileName,"r"); // Check if fs is NULL
DATA *HEAD = MALLOC(sizeof (DATA)); int len = lecture_data(fs,HEAD);
print_data(HEAD,len); //LABEL : 0
return 0;
}
int lecture_data(FILE *fs,DATA *ROOT)
{
char cNom[V],cArticle[V];
int eofs=0;int i=0;
while(!eofs)
{
DATA *Data = MALLOC(sizeof (DATA));
fscanf(fs,"%s %s",cNom,cArticle);
Data->NomA = MALLOC(strlen(cArticle)+1);
Data->NomP = MALLOC(strlen(cNom)+1);
strcpy(Data->NomA,cArticle);
strcpy(Data->NomP,cNom);
if( i==0 )
{
Data -> Next = NULL ;
ROOT = Data ;
}
else
{
DATA* Ptr = ROOT ;
while( (Ptr->Next) != NULL )
{
Ptr = (Ptr -> Next);
}
Data -> Next = NULL ;
Ptr -> Next = Data ;
}
i++;
eofs = feof(fs) ;
// check ferror(fs) here
}
puts("Start of reading :");
print_data(ROOT,len); // LABEL : 1
puts("End Of Reading ");
fclose(fs);
return i;
}
Here is the printing function :
void print_data(DATA *L_ROOT,int len)
{
int i = 0 ;
DATA* LINK;
LINK = L_ROOT;
while( LINK != NULL )
{
printf("%d : DATA->NomA : %s\n",i,LINK->NomA);
printf("%d : DATA->NomP : %s\n",i,LINK->NomP);
LINK = LINK -> Next ;
i++;
}
}
You're allocating data for the root of the list in the main function, and pass that to the function so that it may populate the list, but the first time you allocate an element you overwrite the ROOT pointer value.
this makes you lose the only connection between the function and the outside world (since the return value is just a number), so the HEAD value in main() is left pointing at nothing meaningful (because your function never uses it), while the list remains allocated in some memory location that no one outside is pointing to, which means it's lost. Running valgrind would have been able to identify this.
You can fix that by changing the (i==0) case from -
ROOT = Data ;
into
ROOT->next = Data ;
but make sure you're ignoring the data of the root node later on.
p.s. - using capitalized variables and types is not considered a good idea (it's mostly reserved for macros). It also makes your code look like you're shouting :)
The (main) problem is that lecture_data doesn't use it's input parameter (ROOT) for storage of the linked list, nor does it return the internal generated list. The correct way to handle this is to have ROOT reference the calling scope's parameter so that it can update it's reference as necessary.
int main(void)
{
char FileName[V];
puts("Data file ? : ");gets(FileName);
FILE* fs = fopen(FileName,"r"); // Check if fs is NULL
DATA *HEAD = NULL;
int len = lecture_data(fs, &HEAD);
print_data(HEAD); //LABEL : 0
return 0;
}
int lecture_data(FILE *fs,DATA **ROOT)
{
char cNom[V],cArticle[V];
int i=0;
DATA *current = *ROOT; // grab the passed in reference
while(!feof(fs))
{
if(fscanf(fs,"%s %s",cNom,cArticle) <= 0) // This call is only successful if the return value is > 0
{
// check ferror(fs) here
continue; // Can also "break;" here, essentially, it's eof already
}
DATA *Data = MALLOC(sizeof (DATA));
Data->NomA = MALLOC(strlen(cArticle)+1);
Data->NomP = MALLOC(strlen(cNom)+1);
strcpy(Data->NomA,cArticle);
strcpy(Data->NomP,cNom);
if(NULL == current) // ROOT was uninitialized before the call
{
Data -> Next = NULL;
*ROOT = Data;
}
else
{ // We don't need to iterate the list in every step.
Data->Next = current->Next; // This part allows the function to insert nodes in the middle / end of an existing list
current->Next = Data;
current = Data;
}
i++;
}
puts("Start of reading :");
print_data(ROOT); // LABEL : 1
puts("End Of Reading ");
fclose(fs);
return i;
}
Note: print_data didn't do anything with the len parameter, so no need passing it in at all.
This solution is not wasteful in terms of "empty" nodes in the list (as opposed to having an empty head to ignore), and is suitable both for initializing the list from scratch AND for cases where you need to append / insert into an existing list.
First post, extremely limited in coding knowledge and new to C. Be gentle! I am at the point where "trying" different things is just confusing me more and more. I need someone's correct guidance!
This particular problem is from an online edX course I am attempting which ultimately when implemented correctly, checks a given word read in from a text file (the 'check' function) and compares it to each word read into (from the 'load' function) a linked list of structs.
I believe I have the load function implemented correctly as when I use gdb, as I am seeing what I anticipate as I step through it, but my question and my problem relates specifically to the check function. I still have a lot to implement to finish my code but while testing with gdb, I am not seeing values of the char* member of the struct correspond with what I anticipate I should see.
When using gdb and stepping through the 'check' function and trying to access the dword member of the struct nodes in the linked list I created in the load function, I anticipate I should see a string for the char* member. For instance, I anticipate the word "cat" assigned to current->dword , but am instead seeing in gdb when I test:
~(gdb) print current->dword
$13 = 0xbfffede2 "\004\b\214\365\372D\300\355\377\277"
My thoughts are that I'm still only accessing an address somehow and not the actual value, but I'm oblivious as to why this is. When the node is created in the load function, a value is assigned to the dword member correctly (at least as far as I can tell while stepping through the code in gdb) but doesn't seem to be accessed correctly in the check function. Any help for a newbie would be appreciated!
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
typedef struct node
{
char* dword;
struct node* next;
}
node;
// keep track of #of words in dictionary loaded
int wordCounter = 0;
// create root for hash table
node* root[26];
// create cursor to keep place in creating, pointing, and traversing through nodes
node* current = NULL;
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
// size of word read into buffer
int wordSize = sizeof(word);
// prepare to make a new lowercase only word for comparison to lowercase only dictionary
char bufWord[wordSize];
// make it
for(int i = 0; i < wordSize; i++)
{
if (i == wordSize - 1)
{
bufWord[i] = '\0';
}
else
{
bufWord[i] = tolower(word[i]);
}
}
// hash word to achieve proper root node location
int hash = bufWord[0] - 97;
// point to the correct root node to begin traversing
current = root[hash];
// make sure there is even a word in hash table location
if(root[hash] == NULL)
{
return false;
}
else if(root[hash] != NULL)
{
// progress through the nodes until the last node's next pointer member is NULL
while(current != NULL)
{
// compare 1st letter only of current->dword[i] to bufWord[i] to save time
// if they don't match, return false
// if they do match then continue
\
char dictWord[wordSize];
// hold copy of struct member value to compare to dictWord
char* wordTemp = current->dword;
//
for(int i = 0; i < wordSize; i++)
{
dictWord[i] = wordTemp[i];
}
// do a spell check
if(strcmp(bufWord, dictWord) == 0)
{
return true;
}
else
{
// set current to the next node if any or NULL if it's already the last node in the list
current = current->next;
}
}
}
return false;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
// buffer for reading in dictionary words
char wordIn[LENGTH + 1];
// open the dictionary file
FILE* newDict = fopen(dictionary, "r");
for (int i = 0; i < 27; i++)
{
root[i] = NULL;
}
// while there are words to read
while(fscanf(newDict, "%s ", wordIn) > 0)
{
// keep track of #of words for constant time read in size function
wordCounter++;
// hash the first letter for the location in root
int hash = wordIn[0] - 97;
// malloc space for a new node
node* newNode = malloc(sizeof(node));
// error check
if (newNode == NULL)
{
return false;
}
// set value member of node to current word
newNode->dword = wordIn;
// first insertion into linked list if that root node has not been used yet
if(root[hash] == NULL)
{
// sets to NULL
newNode->next = root[hash];
// link it
root[hash] = newNode;
}
else if(root[hash] != NULL)
{
// starts at the root
node* current = root[hash];
// insert into new beginning of list
newNode->next = current;
root[hash] = newNode;
}
}
fclose(newDict);
return true;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
return wordCounter;
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
// TODO
return false;
}
The source of your problem is the line:
newNode->dword = wordIn;
wordIn is a local array in load. You are storing the address of wordIn in the dword of your nodes. When you return from load, those addresses are no valid any longer.
What you need to do is allocate memory for the string in wordIn, assign the allocated memory to newNode->dword and copy the contents of wordIn to newNode->dword.
If your platform provides the non-standard function strdup, you can change the above line to:
newNode->dword = strdup(wordIn);
If not, it is easily implemented:
char* strdup(char const* in)
{
char* r = malloc(strlen(in)+1);
strcpy(r, in);
return r;
}
I'm trying to create a linked list structure to store data. The head of the linked list seems to be updating somehow. I have the following code. I can't seem to figure out how put char array data into a node and keep it from updating when the address to said char array's data updates.
The following code prints out whatever string is passed into the processStr function. How do I keep head from updating ?
//Linked List Structure
mainNode *head = NULL;
//take and store word in data structure
void processStr(char *str){
//char array
char strArray[sizeof(str)+1];
//stores lower case string
char strLower[strlen(str)];
int i;
for(i = 0; str[i]; i++)
strLower[i] = tolower(str[i]);
strLower[i] = '\0';
//printf("%s : ", strLower);
//Starts Linked List
if(head == NULL){
mainNode *mainPtr = (mainNode *)malloc(sizeof(mainNode));
nameNode *namePtr = (nameNode *)malloc(sizeof(nameNode));
mainPtr->name = strLower;
mainPtr->numOccurances = 1;
mainPtr->next = NULL;
mainPtr->nextName = namePtr;
namePtr->name = strArray;
namePtr->next = NULL;
head = mainPtr;
}
printf("%s : " , head->name);
}
You assign the pointers mainPtr->name and namePtr->name to variables strLower and strArray that are declared locally in processStr(). That means after that function returns, any access to these pointers results in undefined behaviour. You could do sth. like
mainPtr->name = strdup( strLower );
instead to allocate memory for the strings.
Btw.: strLower must also be declared as char strLower[strlen(str)+1];
The above code will only run once only which will add information to head only once. If you want to add more information in case of second run then add code for else condition. Example:-
if ( head == NULL ) {
// code to insert data in case of first run
}else{
// code to insert data for second run and so.....
}
I have a linked list whose elements are of type
typedef struct List * News;
struct List
{
char * Text;
News NextN;
};
In the main function I declared an array of type News as follows
News PTH[50];
for (i = 0; i < 50; i++)
{
PTH[i] = (News) malloc(sizeof(struct List));
PTH[i] -> Text = NULL;
PTH[i] -> NextN = NULL;
}
I added new nodes in the beginning of the list with
if ( PTH[i] -> Text == NULL)
PTH[i] -> Text = msg;
else
{
t -> Text = msg;
t -> NextN = PTH[i];
PTH[i] = t;
}
Where msg is a array of char of length 2000; and and then tried to print the texts apointed by PTH[i] -> Text with
p = PTH[i];
if (p -> Text != NULL)
{
printf("%s", p -> Text);
p = p -> NextN;
}
while (p != NULL)
{
printf("%s", p -> Text);
p = p -> NextN;
}
}
This algorithm only add one node. The error is how I define the PTH or is there an error in how I put nodes in the list.
maybe it is something like this you are after:
if ( PTH[i]->Text == NULL )
{
PTH[i]->Text = msg;
}
else // first create node, then append by first finding the last node
{
News it = NULL;
News t = malloc( sizeof(struct List));
t->Text = msg;
t->NextN = NULL;
for ( it = PTH[i]->NextN; it != NULL; it = it->NextN)
{
;
}
it->NextN = t;
}
Assuming that msg is a buffer you use to receive new data, you have to be careful with this statement:
PTH[i] -> Text = msg;
Since msg is a pointer to char, the assignment will not copy the characters sequence; instead, it will just make PTH[i]->Text point to the same location as msg. This is problematic if you change the contents in msg - the changes are, of course, reflected back in every PTH[i]->Text for which the assignment was made, namely, every node that you ever added. Probably, not quite what you want. This is why it seems like you can only handle one node at a time. They all get the same text, because they all point to the same memory location.
You should use strcpy instead:
strcpy(PTH[i]->Text, msg);
Don't forget to include string.h.
This assumes that PTH[i]->Text is already allocated. You might want to use strncpy if there is a chance that msg exceeds 2000 characters, to avoid buffer overflows.
If you didn't alloc space for PTH[i]->Text, you could allocate exactly strlen(msg)+1 positions for PTH[i]->Text and then use strcpy safely. Or you could use strdup, also declared in string.h, which has exactly this behavior:
PTH[i]->Text = strdup(msg);