Okay, this is just my exercise in order to prepare for the coming assignment regarding the damned Linked List issue...
The task is simple and I am about to finish it. However, I couldn't go around one last bit: how to print out the elements?
Here is so far what I've got: - #include <stdio.h>
-
#include <stdlib.h>
-
-
struct intRecord
-
{
-
int number;
-
struct intRecord *next;
-
};
-
-
typedef struct intRecord intNode;
-
-
intNode *newNode(int number);
-
void copyArrayToList(intNode *start, int array[], int size);
-
void printList(intNode *start);
-
void freeList(intNode **start);
-
-
int main()
-
{
-
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
-
intNode *start = NULL;
-
-
copyArrayToList(start, array, 10);
-
printList(start);
-
return 0;
-
}
-
-
void copyArrayToList(intNode *start, int array[], int size)
-
{
-
/*Create a new linked ring list and copy the contents of array into it.*/
-
int i;
-
intNode *temp, *record;
-
-
/*Add the first item to the linked ring list.*/
-
temp = newNode(array[0]);
-
record = temp;
-
start = temp;
-
-
/*Add the following items, but not the last noe, to this linked ring list.*/
-
for(i=1; i<size-1; i++)
-
{
-
temp = newNode(array[i]);
-
start->next = temp;
-
start = temp;
-
}
-
-
/*Add the last item to this linked ring list.*/
-
temp = newNode(array[size-1]);
-
temp->next = record;
-
-
/*Need to change the start back to the address of the first item.*/
-
start = record;
-
-
}
-
-
intNode *newNode(int number)
-
{
-
/*Add a new item to the Linked ring list.*/
-
intNode *temp;
-
temp = (intNode *) malloc(sizeof(intNode));
-
if (temp == NULL)
-
{
-
printf("WARNING - Memory allocation error\n");
-
exit(EXIT_FAILURE);
-
}
-
temp->number = number;
-
temp->next = NULL;
-
-
return temp;
-
}
-
-
void printList(intNode *start)
-
{
-
/*Use a loop to print out the contents in the linked ring list.*/
-
-
intNode *record = start;
-
int i;
-
for (i=0; i<10; i++)
-
{
-
/*I GOT A PROBLEM HERE! BUT WHY?*/
-
printf("%d ", start->number);
-
start = start->next;
-
}
-
-
printf("\n");
-
-
}
It passed the gcc compiling proecss, but in runtime the system gave me an error: "Segmentation Fault (core dumped)"
I've located the error to these lines of code:
[code]printf("%d ", start->number);
start = start->next;/CODE]
It means to move on to the next address of item in the linked ring list. My logic must be fine, but I just cannot figure out how to achieve that. I could print out the first item, which was "0" of course on screen, but the pointer wouldn't move as what I wanted.
Thanks in advance!
Sincerely yours,
Matt
6 2114
Fix my mistake, but still have two errors here: - printf("%d ", start->number);
-
start = start->next;
Both lines of code are incorrect, why?
-
intNode *record = start;
-
int i;
-
for (i=0; i<10; i++)
-
{
-
printf("%d ", &start->number);
-
/*I GOT A PROBLEM HERE! BUT WHY?*/
-
start = start->next;
-
}
-
You've already got an intNode pointer here, so &start gives you a pointer to a pointer, so when you try to go from that double pointer straight to the data, it gives you an error because you need to dereference the extra pointer. Basically, just drop the & and you should be fine.
Yeah, I've correct that. But still I got this "Segmentation fault"
Here is my current code: -
#include <stdio.h>
-
#include <stdlib.h>
-
-
struct intRecord
-
{
-
int number;
-
struct intRecord *next;
-
};
-
-
typedef struct intRecord intNode;
-
-
intNode *newNode(int number);
-
void copyArrayToList(intNode *start, int array[], int size);
-
void printList(intNode *start);
-
void freeList(intNode **start);
-
-
int main()
-
{
-
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
-
intNode *start = NULL;
-
-
copyArrayToList(start, array, 10);
-
printList(start);
-
return 0;
-
}
-
-
void copyArrayToList(intNode *start, int array[], int size)
-
{
-
/*Create a new linked ring list and copy the contents of array into it.*/
-
int i;
-
intNode *temp, *record;
-
-
/*Add the first item to the linked ring list.*/
-
temp = newNode(array[0]);
-
record = temp;
-
start = temp;
-
/*TEST CODE*/
-
printf("%d ", temp->number);
-
-
/*Add the following items, but not the last noe, to this linked ring list.*/
-
for(i=1; i<size-1; i++)
-
{
-
temp = newNode(array[i]);
-
start->next = temp;
-
start = temp;
-
/*TEST CODE*/
-
printf("%d ", temp->number);
-
}
-
-
/*Add the last item to this linked ring list.*/
-
temp = newNode(array[size-1]);
-
temp->next = record;
-
/*TEST CODE*/
-
printf("%d\n", temp->number);
-
-
/*Need to change the start back to the address of the first item.*/
-
start = record;
-
-
}
-
-
intNode *newNode(int number)
-
{
-
/*Add a new item to the Linked ring list.*/
-
intNode *temp;
-
temp = (intNode *) malloc(sizeof(intNode));
-
if (temp == NULL)
-
{
-
printf("WARNING - Memory allocation error\n");
-
exit(EXIT_FAILURE);
-
}
-
temp->number = number;
-
temp->next = NULL;
-
-
return temp;
-
}
-
-
void printList(intNode *start)
-
{
-
/*Use a loop to print out the contents in the linked ring list.*/
-
-
intNode *record = start;
-
int i;
-
for (i=0; i<10; i++)
-
{
-
// printf("%d ", start->number);
-
/*I GOT A PROBLEM HERE! BUT WHY?*/
-
//start = start->next;
-
}
-
-
printf("\n");
-
-
}
-
There is nothing meaningful inside my printList function right now, I don't know how to fix that.
Got it. When you pass start to copyArrayToList, a local copy of that pointer is formed, which is then set to temp. When the function closes, however, the copy is removed and the original remains where it is, at NULL. To fix this, either set start outside the function, in the main, or use a double pointer (intNode **). You can call the function with copyArrayToList(&start, <other paramters>), since & in prefix gives you a pointer, you pass the pointer by pointer. Sounds odd, but it works.
- #include <stdio.h>
-
#include <stdlib.h>
-
-
struct intRecord
-
{
-
int number;
-
struct intRecord *next;
-
};
-
-
typedef struct intRecord intNode;
-
-
intNode *newNode(int number);
-
intNode *copyArrayToList(int array[], int size);
-
void printList(intNode *start);
-
void freeList(intNode **start);
-
-
int main()
-
{
-
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
-
intNode *start = NULL;
-
-
start = copyArrayToList(array, 10);
-
-
/*TEST CODE*/
-
printf("%d", start->number);
-
-
printList(start);
-
return 0;
-
}
-
-
intNode *copyArrayToList(int array[], int size)
-
{
-
/*Create a new linked ring list and copy the contents of array into it.*/
-
int i;
-
intNode *temp, *record, *pre;
-
-
/*Add the first item to the linked ring list.*/
-
temp = newNode(array[0]);
-
record = temp;
-
pre = temp;
-
/*TEST CODE*/
-
printf("%d ", temp->number);
-
printf("now record contains:%d ", record->number);
-
-
/*Add the following items, but not the last noe, to this linked ring list.*/
-
for(i=1; i<size-1; i++)
-
{
-
temp = newNode(array[i]);
-
pre->next = temp;
-
pre = temp;
-
/*TEST CODE*/
-
printf("%d ", temp->number);
-
}
-
-
/*Add the last item to this linked ring list.*/
-
temp = newNode(array[size-1]);
-
temp->next = record;
-
/*TEST CODE*/
-
printf("%d\n", temp->number);
-
-
/*Need to change the start back to the address of the first item.*/
-
return record;
-
-
-
}
-
-
intNode *newNode(int number)
-
{
-
/*Add a new item to the Linked ring list.*/
-
intNode *temp;
-
temp = (intNode *) malloc(sizeof(intNode));
-
if (temp == NULL)
-
{
-
printf("WARNING - Memory allocation error\n");
-
exit(EXIT_FAILURE);
-
}
-
temp->number = number;
-
temp->next = NULL;
-
-
return temp;
-
}
-
-
void printList(intNode *start)
-
{
-
-
}
-
Fixed some problems, it works now. But I am still trying to finish all functionalities. - 1bash-3.2$ gcc mycode.c
-
bash-3.2$ ./a.out
-
1 now record contains:1 2 3 4 5 6 7 8 9 10
-
1bash-3.2$
-
I don't know why I cannot edit my code, this is so far what I've done: -
#include <stdio.h>
-
#include <stdlib.h>
-
-
struct intRecord
-
{
-
int number;
-
struct intRecord *next;
-
};
-
-
typedef struct intRecord intNode;
-
-
intNode *newNode(int number);
-
intNode *copyArrayToList(int array[], int size);
-
void printList(intNode *start, int size);
-
void freeList(intNode **start, int size);
-
-
int main()
-
{
-
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
-
intNode *start = NULL;
-
-
start = copyArrayToList(array, 10);
-
-
/*TEST CODE*/
-
//printf("%d", start->number);
-
-
printList(start, 10);
-
freeList(&start, 10);
-
return 0;
-
}
-
-
intNode *copyArrayToList(int array[], int size)
-
{
-
/*Create a new linked ring list and copy the contents of array into it.*/
-
int i;
-
intNode *temp, *record, *pre;
-
-
/*Add the first item to the linked ring list.*/
-
temp = newNode(array[0]);
-
record = temp;
-
pre = temp;
-
/*TEST CODE*/
-
//printf("%d ", temp->number);
-
-
/*Add the following items, but not the last noe, to this linked ring list.*/
-
for(i=1; i<size-1; i++)
-
{
-
temp = newNode(array[i]);
-
pre->next = temp;
-
pre = temp;
-
/*TEST CODE*/
-
//printf("%d ", temp->number);
-
}
-
-
/*Add the last item to this linked ring list.*/
-
temp = newNode(array[size-1]);
-
pre->next = temp;
-
temp->next = record;
-
/*TEST CODE*/
-
//printf("%d\n", temp->number);
-
-
/*Need to change the start back to the address of the first item.*/
-
return record;
-
-
-
}
-
-
intNode *newNode(int number)
-
{
-
/*Add a new item to the Linked ring list.*/
-
intNode *temp;
-
temp = (intNode *) malloc(sizeof(intNode));
-
if (temp == NULL)
-
{
-
printf("WARNING - Memory allocation error\n");
-
exit(EXIT_FAILURE);
-
}
-
temp->number = number;
-
temp->next = NULL;
-
-
return temp;
-
}
-
-
-
-
void printList(intNode *start, int size)
-
{
-
/* Print the contents of the ring list.*/
-
-
int i;
-
-
for(i=0; i<size; i++)
-
{
-
printf("%d ", start->number);
-
start = start->next;
-
}
-
printf("\n");
-
}
-
-
void freeList(intNode **start, int size)
-
{
-
intNode *temp, *pre, *record = *start;
-
int i;
-
pre = *start;
-
temp = *start;
-
-
if(*start == NULL) return;
-
-
for (i=0; i<size; i++)
-
{
-
temp = pre->next;
-
free(pre);
-
pre = temp;
-
}
-
*start = NULL;
-
-
}
I hope everything is fine now...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Peter Ballard |
last post by:
Whew. I hope that title is descriptive!
Hi all,
The python tutorial tells me "It is not safe to modify the sequence
being iterated over in the loop". But what if my list elements are
mutable,...
|
by: yawnmoth |
last post by:
I'm trying to center list elements in a webpage I'm working on, and
setting margin-left to auto for ol (or ul) seems to prevent the number
(or bullet) from displaying in IE6 (strict mode) and...
|
by: Pradyut |
last post by:
hi,
i have written a function in adding a node to the linked list
but this only adds "1" to the list as i get in the output
I'm using borland c++ compiler
The code...
|
by: Cheddar |
last post by:
I'm back again with another problem.
What I want is for the user to click a checkbox and have a
list appear. I think the code is almost there but it doesnt
seem to be working, grrrr. Can anyone...
|
by: Eugen J. Sobchenko |
last post by:
Hi!
I'm writing function which swaps two arbitrary elements
of double-linked list. References to the next element of list
must be unique or NULL (even during swap procedure), the same condition...
|
by: Plissken.s |
last post by:
Hi
I have a problem which result in a "corrupted double-linked list
error", I would need some help in trouble shot this one:
Here is a stack track:
Thread (Suspended: Signal 'SIGABRT'...
|
by: python |
last post by:
after del list , when I use it again, prompt 'not defined'.how could i
delete its element,but not itself?
except list.remove(val) ,are there other ways to delete list 's
elements?
and , I...
|
by: Vv_vV |
last post by:
Hi all,
I try to call onchange a function and get "missing ) after argument
list" error
Probably systaxis issue
Thanks in advance for any help!
Code:
var html = "";
var myurl =...
|
by: Jezternz |
last post by:
Scriptaculous needs an official forum but does not have one so I am asking here :).
note you will need to be familiar with scriptaculous to help me here.
Basicly I have a script that has a...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
| |