c++ - why does this code produce a runtime error? -
contains # lt; Stdio.h & gt; #to & lt include, conio.h & gt; #to & lt include, windows.h & gt; Structure node {int a; Structure node * next; }; Zero constructivist (Structure node ** head) {struct node * p, * temp; Int n; Printf ("Enter number \ n"); Scanf ("% d", & amp; n); P = (straight node *) maulok (psychophag (strip node)); P-> = N; If (* head == 0) {* head = p; P-> Next = 0; Temporary = p; } And {temp-> next = p; Temporary = p; P-> Next = 0; }} Void frontbacksplit (Structure node ** head, Structure node ** Head1, Structure node ** Head 2) {int counter = 0, i; Structure node * temporary, * p; Temporary = * head; While (temporary! = 0) {counter ++; Temporary = temp-> Next; } Int n; If (counter% 2 == 0) {n = counter / 2; } And {n = (counter + 1) / 2; } Temp = * head; (I = 0; i & lt; n-1; i ++) for {if (* head1 == 0) {* head1 = temp; } Temp = temp- & gt; next; } P = temp; Temporary = temp-> Next; P-> Next = 0; For (i = n + 1; i a); Temporary = temp-> Next; } Printf ("\ n"); } Int main () {int n = 1, i, k; Structure node * Head 3, * Head 1, * Head 2; Head3 = 0; Head1 = 0; Head2 = 0; While (n == 1) {printf ("Enter \ n1- to add elements \ n2- to divide the list and front back \ n3- to display elements \ n"); Scanf ("% d", & amp; i); Switch (i) {Case 1: Creatitist (and Head 3); break; Case 2: Frontbackplit (And Head 3, End Head 1, and Head 2); break; Case 3: printf ("Enter \ n1-to display the front list \ n2- to display the previous list \ n"); Scanf ("% d", & amp; k); Switch (K) {Case 1: display (and head 1); break; Case 2: Performance (and Head 2); } break; Default: printf ("Please enter a valid option \ n"); } Printf ("Enter \ n1- to continue \ n to continue other numbers \ n"); Scanf ("% d", & amp; n); } Getch (); Return 0; } I have written this code for the front / back splitting of the linked list. For example if the list is [1 2 3 4 5] then this program divides the list into two lists. Is: the front part (1 2 3) and the previous part (4-5), if the number of elements is equal to both parts, the equal number of elements. Problem: When I try to add elements to the source list, first elements are added as usual but when I try to add another element then my The program shows runtime error. I think the indicator has problems with variable temp but for the construction of linked list almost a single code has worked properly. I am using dev c ++ ide on Windows 8.
Of course if you were not like asking about this question, then this is my first time.
The first time this line temp-> gt; Next = p in the createlist function is incorrect, temp is a local variable, although you indicate it for the element when the list is created for the first time If the value is lost after the createlist function, will you access junk addresses in a consistent call on createlist ? P>
Comments
Post a Comment