473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

121 100+
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: "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
Oct 12 '07 #1
6 2114
mattmao
121 100+
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 Expert 512MB
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 100+
Yeah, I've correct that. But still I got this "Segmentation 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 Expert 512MB
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 100+
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 100+
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
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,...
3
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...
1
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...
3
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...
12
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...
1
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'...
7
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...
5
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 =...
1
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...
0
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,...
0
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$) { } ...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
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,...
0
jinu1996
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 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.