473,770 Members | 6,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Linked list]Got an error when print out the elements:(

121 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct intRecord
  5. {
  6.     int number;
  7.     struct intRecord *next;
  8. };
  9.  
  10. typedef struct intRecord intNode;
  11.  
  12. intNode *newNode(int number);
  13. void copyArrayToList(intNode *start, int array[], int size);
  14. void printList(intNode *start);
  15. void freeList(intNode **start);
  16.  
  17. int main()
  18. {
  19.     int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  20.     intNode *start = NULL;
  21.  
  22.     copyArrayToList(start, array, 10);
  23.     printList(start);
  24.     return 0;
  25. }
  26.  
  27. void copyArrayToList(intNode *start, int array[], int size)
  28. {
  29.     /*Create a new linked ring list and copy the contents of array into it.*/
  30.     int i;
  31.     intNode *temp, *record;
  32.  
  33.     /*Add the first item to the linked ring list.*/
  34.     temp = newNode(array[0]);
  35.     record = temp;
  36.     start = temp;
  37.  
  38.     /*Add the following items, but not the last noe, to this linked ring list.*/
  39.     for(i=1; i<size-1; i++)
  40.     {
  41.         temp = newNode(array[i]);
  42.         start->next = temp;
  43.         start = temp;
  44.     }
  45.  
  46.     /*Add the last item to this linked ring list.*/
  47.     temp = newNode(array[size-1]);
  48.     temp->next = record;
  49.  
  50.     /*Need to change the start back to the address of the first item.*/
  51.     start = record;
  52.  
  53. }
  54.  
  55. intNode *newNode(int number)
  56. {
  57.     /*Add a new item to the Linked ring list.*/
  58.     intNode *temp;
  59.     temp = (intNode *) malloc(sizeof(intNode));
  60.     if (temp == NULL)
  61.     {
  62.         printf("WARNING - Memory allocation error\n");
  63.         exit(EXIT_FAILURE);
  64.     }
  65.     temp->number = number;
  66.     temp->next = NULL;
  67.  
  68.     return temp;
  69. }
  70.  
  71. void printList(intNode *start)
  72. {
  73.     /*Use a loop to print out the contents in the linked ring list.*/
  74.  
  75.     intNode *record = start;
  76.     int i;
  77.     for (i=0; i<10; i++)
  78.     {
  79.         /*I GOT A PROBLEM HERE! BUT WHY?*/
  80.         printf("%d ", start->number);
  81.         start = start->next;
  82.     }
  83.  
  84.     printf("\n");
  85.  
  86. }
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
Oct 12 '07 #1
6 2149
mattmao
121 New Member
Fix my mistake, but still have two errors here:

Expand|Select|Wrap|Line Numbers
  1. printf("%d ", start->number);
  2.         start = start->next;
Both lines of code are incorrect, why?
Oct 12 '07 #2
Laharl
849 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. intNode *record = start;
  2.     int i;
  3.     for (i=0; i<10; i++)
  4.     {
  5.         printf("%d ", &start->number);
  6.         /*I GOT A PROBLEM HERE! BUT WHY?*/
  7.         start = start->next;
  8.     }
  9.  
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.
Oct 12 '07 #3
mattmao
121 New Member
Yeah, I've correct that. But still I got this "Segmentati on fault"

Here is my current code:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct intRecord
  5. {
  6.     int number;
  7.     struct intRecord *next;
  8. };
  9.  
  10. typedef struct intRecord intNode;
  11.  
  12. intNode *newNode(int number);
  13. void copyArrayToList(intNode *start, int array[], int size);
  14. void printList(intNode *start);
  15. void freeList(intNode **start);
  16.  
  17. int main()
  18. {
  19.     int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  20.     intNode *start = NULL;
  21.  
  22.     copyArrayToList(start, array, 10);
  23.     printList(start);
  24.     return 0;
  25. }
  26.  
  27. void copyArrayToList(intNode *start, int array[], int size)
  28. {
  29.     /*Create a new linked ring list and copy the contents of array into it.*/
  30.     int i;
  31.     intNode *temp, *record;
  32.  
  33.     /*Add the first item to the linked ring list.*/
  34.     temp = newNode(array[0]);
  35.     record = temp;
  36.     start = temp;
  37.     /*TEST CODE*/
  38.     printf("%d ", temp->number);
  39.  
  40.     /*Add the following items, but not the last noe, to this linked ring list.*/
  41.     for(i=1; i<size-1; i++)
  42.     {
  43.         temp = newNode(array[i]);
  44.         start->next = temp;
  45.         start = temp;
  46.         /*TEST CODE*/
  47.         printf("%d ", temp->number);
  48.     }
  49.  
  50.     /*Add the last item to this linked ring list.*/
  51.     temp = newNode(array[size-1]);
  52.     temp->next = record;
  53.     /*TEST CODE*/
  54.     printf("%d\n", temp->number);
  55.  
  56.     /*Need to change the start back to the address of the first item.*/
  57.     start = record;
  58.  
  59. }
  60.  
  61. intNode *newNode(int number)
  62. {
  63.     /*Add a new item to the Linked ring list.*/
  64.     intNode *temp;
  65.     temp = (intNode *) malloc(sizeof(intNode));
  66.     if (temp == NULL)
  67.     {
  68.         printf("WARNING - Memory allocation error\n");
  69.         exit(EXIT_FAILURE);
  70.     }
  71.     temp->number = number;
  72.     temp->next = NULL;
  73.  
  74.     return temp;
  75. }
  76.  
  77. void printList(intNode *start)
  78. {
  79.     /*Use a loop to print out the contents in the linked ring list.*/
  80.  
  81.     intNode *record = start;
  82.     int i;
  83.     for (i=0; i<10; i++)
  84.     {
  85.         // printf("%d ", start->number);
  86.         /*I GOT A PROBLEM HERE! BUT WHY?*/
  87.         //start = start->next;
  88.     }
  89.  
  90.     printf("\n");
  91.  
  92. }
  93.  
There is nothing meaningful inside my printList function right now, I don't know how to fix that.
Oct 12 '07 #4
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.
Oct 12 '07 #5
mattmao
121 New Member
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct intRecord
  5. {
  6.     int number;
  7.     struct intRecord *next;
  8. };
  9.  
  10. typedef struct intRecord intNode;
  11.  
  12. intNode *newNode(int number);
  13. intNode *copyArrayToList(int array[], int size);
  14. void printList(intNode *start);
  15. void freeList(intNode **start);
  16.  
  17. int main()
  18. {
  19.     int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  20.     intNode *start = NULL;
  21.  
  22.     start = copyArrayToList(array, 10);
  23.  
  24.     /*TEST CODE*/
  25.      printf("%d", start->number);
  26.  
  27.     printList(start);
  28.     return 0;
  29. }
  30.  
  31. intNode *copyArrayToList(int array[], int size)
  32. {
  33.     /*Create a new linked ring list and copy the contents of array into it.*/
  34.     int i;
  35.     intNode *temp, *record, *pre;
  36.  
  37.     /*Add the first item to the linked ring list.*/
  38.     temp = newNode(array[0]);
  39.     record = temp;
  40.     pre = temp;
  41.     /*TEST CODE*/
  42.     printf("%d ", temp->number);
  43.     printf("now record contains:%d ", record->number);
  44.  
  45.     /*Add the following items, but not the last noe, to this linked ring list.*/
  46.     for(i=1; i<size-1; i++)
  47.     {
  48.         temp = newNode(array[i]);
  49.         pre->next = temp;
  50.         pre = temp;
  51.         /*TEST CODE*/
  52.         printf("%d ", temp->number);
  53.     }
  54.  
  55.     /*Add the last item to this linked ring list.*/
  56.     temp = newNode(array[size-1]);
  57.     temp->next = record;
  58.     /*TEST CODE*/
  59.     printf("%d\n", temp->number);
  60.  
  61.     /*Need to change the start back to the address of the first item.*/
  62.     return record;
  63.  
  64.  
  65. }
  66.  
  67. intNode *newNode(int number)
  68. {
  69.     /*Add a new item to the Linked ring list.*/
  70.     intNode *temp;
  71.     temp = (intNode *) malloc(sizeof(intNode));
  72.     if (temp == NULL)
  73.     {
  74.         printf("WARNING - Memory allocation error\n");
  75.         exit(EXIT_FAILURE);
  76.     }
  77.     temp->number = number;
  78.     temp->next = NULL;
  79.  
  80.     return temp;
  81. }
  82.  
  83. void printList(intNode *start)
  84. {
  85.  
  86. }
  87.  
Fixed some problems, it works now. But I am still trying to finish all functionalities .

Expand|Select|Wrap|Line Numbers
  1. 1bash-3.2$ gcc mycode.c
  2. bash-3.2$ ./a.out
  3. 1 now record contains:1 2 3 4 5 6 7 8 9 10
  4. 1bash-3.2$ 
  5.  
Oct 12 '07 #6
mattmao
121 New Member
I don't know why I cannot edit my code, this is so far what I've done:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct intRecord
  5. {
  6.     int number;
  7.     struct intRecord *next;
  8. };
  9.  
  10. typedef struct intRecord intNode;
  11.  
  12. intNode *newNode(int number);
  13. intNode *copyArrayToList(int array[], int size);
  14. void printList(intNode *start, int size);
  15. void freeList(intNode **start, int size);
  16.  
  17. int main()
  18. {
  19.     int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  20.     intNode *start = NULL;
  21.  
  22.     start = copyArrayToList(array, 10);
  23.  
  24.     /*TEST CODE*/
  25.     //printf("%d", start->number);
  26.  
  27.     printList(start, 10);
  28.     freeList(&start, 10);
  29.     return 0;
  30. }
  31.  
  32. intNode *copyArrayToList(int array[], int size)
  33. {
  34.     /*Create a new linked ring list and copy the contents of array into it.*/
  35.     int i;
  36.     intNode *temp, *record, *pre;
  37.  
  38.     /*Add the first item to the linked ring list.*/
  39.     temp = newNode(array[0]);
  40.     record = temp;
  41.     pre = temp;
  42.     /*TEST CODE*/
  43.     //printf("%d ", temp->number);
  44.  
  45.     /*Add the following items, but not the last noe, to this linked ring list.*/
  46.     for(i=1; i<size-1; i++)
  47.     {
  48.         temp = newNode(array[i]);
  49.         pre->next = temp;
  50.         pre = temp;
  51.         /*TEST CODE*/
  52.         //printf("%d ", temp->number);
  53.     }
  54.  
  55.     /*Add the last item to this linked ring list.*/
  56.     temp = newNode(array[size-1]);
  57.     pre->next = temp;
  58.     temp->next = record;
  59.     /*TEST CODE*/
  60.     //printf("%d\n", temp->number);
  61.  
  62.     /*Need to change the start back to the address of the first item.*/
  63.     return record;
  64.  
  65.  
  66. }
  67.  
  68. intNode *newNode(int number)
  69. {
  70.     /*Add a new item to the Linked ring list.*/
  71.     intNode *temp;
  72.     temp = (intNode *) malloc(sizeof(intNode));
  73.     if (temp == NULL)
  74.     {
  75.         printf("WARNING - Memory allocation error\n");
  76.         exit(EXIT_FAILURE);
  77.     }
  78.     temp->number = number;
  79.     temp->next = NULL;
  80.  
  81.     return temp;
  82. }
  83.  
  84.  
  85.  
  86. void printList(intNode *start, int size)
  87. {
  88.    /* Print the contents of the ring list.*/
  89.  
  90.    int i;   
  91.  
  92.    for(i=0; i<size; i++)
  93.    {
  94.       printf("%d ", start->number);
  95.       start = start->next;
  96.    }
  97.    printf("\n");
  98. }
  99.  
  100. void freeList(intNode **start, int size)
  101. {
  102.     intNode *temp, *pre, *record = *start;
  103.     int i;
  104.     pre = *start;
  105.     temp = *start;
  106.  
  107.     if(*start == NULL) return;
  108.  
  109.     for (i=0; i<size; i++)
  110.     {
  111.         temp = pre->next;
  112.         free(pre);
  113.         pre = temp;
  114.     }
  115.     *start = NULL;
  116.  
  117. }
I hope everything is fine now...
Oct 12 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
5061
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:
3
6122
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...
1
486
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>
3
1932
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>
12
15099
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 {
1
5667
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
7
1356
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
5
2271
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=";
1
2810
Jezternz
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...
0
9592
marktang
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...
0
10230
Oralloy
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...
0
9870
tracyyun
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...
1
7416
isladogs
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...
0
6678
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();...
0
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
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
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.