I'm having some trouble with a linked list of strings. After I insert
a number of items, I call a print function and every node in the
linked list has the value from the last item I entered into the linked
list. Any ideas? Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct listNode { /* self-referential structure */
char *data;
int wordLength;
struct listNode *nextPtr;
};
typedef struct listNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;
void insert(LISTNODEPTR *, char *, int);
void printList(LISTNODEPTR);
int isChar(char);
void resetString(void);
char word[50];
main()
{
FILE *fptr;
LISTNODEPTR startPtr = NULL;
int maxLine = 0;
char file_name[20];
int count = 0;
char c;
char d;
printf("Type in the name of the file containing the text: \n");
scanf("%s",file_name);
fptr=fopen(file_name,"r");
int numChar; /* number of characters per line */
//printf("How many characters per line?: ");
//scanf("%d", &maxLine);
c = fgetc(fptr);
int tempCount = 0;
while ((d = fgetc(fptr)) != EOF) {
/* if two new lines in a row */
if ((c == '\n') && (d == '\n')) {
}
/* if not two non-characters in a row */
else if ((isChar(c) == 0) && (isChar(d) == 0)) {
c = d;
printf("%s\n",word);
}
/* if c and d are both characters */
else if ((isChar(c) == 1) && (isChar(d) == 1)) {
word[count] = c;
c = d;
count++;
printf("%s\n",word);
}
/* if c is character and d is space, return, tab, or new line */
else if ((isChar(c) == 1) && (isChar(d) == 0)) {
word[count] = c;
insert(&startPtr, word, count);
c = d;
count = 0;
printf("word: %s\n",word);
resetString();
}
/* if d is start of new word */
else if ((isChar(d) == 1) && (isChar(c) == 0)) {
c = d;
printf("%s\n",word);
}
}
fclose(fptr);
printList(startPtr);
}
void resetString(void) {
int i = 0;
for (i = 0;i < 50;i++) {
word[i] = '\0';
}
}
int isChar(char c) {
if ((c >= 33) && (c <= 126)) {
return 1;
}
return 0;
}
/* insert into list */
void insert(LISTNODEPTR *sPtr, char *value, int length)
{
LISTNODEPTR newPtr, previousPtr, currentPtr;
newPtr = malloc(sizeof(LISTNODE));
if (newPtr != NULL) { /* is space available */
newPtr->data = value;
newPtr->wordLength = length;
newPtr->nextPtr = NULL;
previousPtr = NULL;
currentPtr = *sPtr;
while (currentPtr != NULL && value currentPtr->data) {
previousPtr = currentPtr; /* walk to ... */
currentPtr = currentPtr->nextPtr; /* ... next node */
}
if (previousPtr == NULL) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else {
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else
printf("%s not inserted. No memory available.\n", value);
}
/* Print the list */
void printList(LISTNODEPTR currentPtr)
{
if (currentPtr == NULL)
printf("List is empty.\n\n");
else {
printf("The list is:\n");
while (currentPtr != NULL) {
printf("%s --", currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
printf("NULL\n\n");
}
} 7 2682
On Sat, 27 Oct 2007 01:59:08 -0000, ma************@gmail.com wrote in
comp.lang.c:
I'm having some trouble with a linked list of strings. After I insert
a number of items, I call a print function and every node in the
linked list has the value from the last item I entered into the linked
list. Any ideas? Code:
[snip]
It's a FAQ (a Frequently Asked Question), in particular:
"7.4 I'm reading lines from a file into an array, with this code:
char linebuf[80];
char *lines[100];
int i;
for(i = 0; i < 100; i++) {
char *p = fgets(linebuf, 80, fp);
if(p == NULL) break;
lines[i] = p;
}
Why do all the lines end up containing copies of the last line?"
This situation exactly matches your problem, and of course the answer
in the FAQ will help you fix it.
Always check a group's FAQ before you post, your question might
already be answered there.
There's a link to the FAQ for comp.lang.c in my signature, below.
While you're there, look at all of it. There is a lot of excellent
advice for new, and even not-so-new, C programmers.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
I looked at 7.4 (and 20.2 it references), but I still don't understand
how to apply it to my program. Do I need to allocate memory for each
character since I'm using fgetc? Would I allocate memory in the size
of word[count] right before I call the insert function? Wrapping my
head around all the pointers and memory allocation in this program is
extremely difficult.
<ma************@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
>I looked at 7.4 (and 20.2 it references), but I still don't understand
how to apply it to my program. Do I need to allocate memory for each
character since I'm using fgetc? Would I allocate memory in the size
of word[count] right before I call the insert function? Wrapping my
head around all the pointers and memory allocation in this program is
extremely difficult.
Here is what you are doing:
1. reading a string of characters into word
2. Giving the address of word to your list element.
3. Read a new string of characters into word
4. Giving the address of word to your list element.
etc.
So every time you copy a new word into the string word, it writes overtop of
the old string.
Here is what you should be doing:
1. Read a string of characters into word
2. Make a copy of the characters like this:
char *copy = malloc(strlen(word)+1);
if (copy) strcpy(copy, word);
point at the duplicate string.
etc.
--
Posted via a free Usenet account from http://www.teranews.com
Ah man I just noticed my reply didn't submit.
I looked at 7.4 and the other FAQ it referenced, and I still cannot
figure out how to fix this in my program. I understand the basic
concept of what I'm doing wrong, but somewhere among all the pointers
and memory allocations, I get completely lost.
Jack,
Sorry to email you directly but I've been trying to post a reply to my
post in comp.lang.c and it won't show up.
After looking at the FAQs, I understand the basic concept of what I'm
doing wrong, but somewhere amidst all the memory allocations and
pointers, I'm lost on how to address this issue in my program. Memory
allocation is very new to me and something I have not covered in
depth, so perhaps that is my weakness.
Mark
On Oct 26, 9:47 pm, mark.martin...@gmail.com wrote:
Jack,
Sorry to email you directly but I've been trying to post a reply to my
post in comp.lang.c and it won't show up.
After looking at the FAQs, I understand the basic concept of what I'm
doing wrong, but somewhere amidst all the memory allocations and
pointers, I'm lost on how to address this issue in my program. Memory
allocation is very new to me and something I have not covered in
depth, so perhaps that is my weakness.
/* insert into list */
void insert(LISTNODEPTR * sPtr, char *value, size_t length)
{
LISTNODEPTR newPtr,
previousPtr,
currentPtr;
newPtr = malloc(sizeof(LISTNODE));
if (newPtr != NULL) { /* is space available */
/
************************************************** ***********************/
newPtr->data = malloc(length); /* should be strlen(word)+1 */
if (newPtr->data == NULL) {
printf("Out of memory in %s at line %s\n", __FILE__,
__LINE__);
exit(EXIT_FAILURE);
}
strcpy(newPtr->data, value);
/
************************************************** ***********************/
newPtr->wordLength = length;
newPtr->nextPtr = NULL;
previousPtr = NULL;
currentPtr = *sPtr;
while (currentPtr != NULL && value currentPtr->data) {
previousPtr = currentPtr; /* walk to ... */
currentPtr = currentPtr->nextPtr; /* ... next node */
}
if (previousPtr == NULL) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
} else {
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
} else {
printf("%s not inserted. No memory available.\n", value);
exit(EXIT_FAILURE);
}
} ma************@gmail.com wrote:
Ah man I just noticed my reply didn't submit.
Yes it did. Google sucks, get a real news service.
Brian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Chris Ritchey |
last post by:
Hmmm I might scare people away from this one just by the title, or
draw people in with a chalange :)
I'm writting this program in c++, however I'm using char* instead of
the string class, I am...
|
by: Kent |
last post by:
Hi!
I want to store data (of enemys in a game) as a linked list, each node will
look something like the following:
struct node
{
double x,y; // x and y position coordinates
struct enemy...
|
by: Booser |
last post by:
// Merge sort using circular linked list
// By Jason Hall <booser108@yahoo.com>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//#define debug
|
by: Jonathan Bartlett |
last post by:
Just finished a new IBM DeveloperWorks article on linked lists, and
thought you all might be interested. It's not an introduction -- it
instead covers some of the more interesting aspects of...
|
by: s_subbarayan |
last post by:
Dear all,
1)In one of our implementation for an application we are supposed to
collate two linked lists.The actual problem is like this:
There are two singularly linked lists, the final output...
| |
by: MJ |
last post by:
Hi
I have written a prog for reversing a linked list
I have used globle pointer
Can any one tell me how I can modify this prog so that I dont have to
use extra pointer Head1.
When I reverse a LL...
|
by: Little |
last post by:
Could someone tell me what I am doing wrong here about declaring
mutiple double linked lists. This is what the information is for the
project and the code wil be below that. Thank your soo much for...
|
by: joshd |
last post by:
Hello,
Im sorry if this question has been asked before, but I did search
before posting and couldnt find an answer to my problem. I have two
classes each with corresponding linked lists, list1...
|
by: Dongsheng Ruan |
last post by:
with a cell class like this:
#!/usr/bin/python
import sys
class Cell:
def __init__( self, data, next=None ):
self.data = data
|
by: Joerg Schoen |
last post by:
Hi folks!
Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.)
For linked lists, mergesort is the typical choice.
While I was looking for a optimized implementation of mergesort...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |