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: "Segmentati on 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 2149
Fix my mistake, but still have two errors here: - printf("%d ", start->number);
-
start = start->next;
Both lines of code are incorrect, why?
Laharl 849
Recognized Expert Contributor -
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 "Segmentati on 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.
Laharl 849
Recognized Expert Contributor
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, such as lists or objects, e.g.
a = , , , ]
for coord in a:
|
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 Opera7 (it works
fine in Firefox). Any ideas as to why and what I might do to fix it?
I've set up a little demonstration of the problem here:
http://www.frostjedi.com/terra/dev/test.html
It's set up to render in quirks mode to demonstrate that it...
|
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
-------------------------------------------------------------
#include <alloc.h>
#include <stdio.h>
#include <conio.h>
|
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 help me out with it.
Also is there a initial way to hide a list when the page is
loaded?
<html>
|
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
should be kept for references to previous element of list.
Here is my solution below:
struct node {
| |
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' received. Description:
Aborted.)
15 __kernel_vsyscall() 0xb7f25402
14 raise() 0x00646118
13 abort() 0x00647888
|
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 wrote:
list1=
def method1():
global list1
|
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 = "http://www.example.com/url.php?dummy=";
|
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 variable number of Sortables.
These are lists(ul's) that contain any number of items(li's), the li's can be dragged and dropped into other lists, so the user can basicly, drag and drop block-elements(the li's) into the different lists. Anyway, currently...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |