c - Segmentation fault while doing malloc for a struct pointer -
I am trying to write the original linked list code. I have two functions to add data to the beginning of the list and the end of the list. The process of adding data at the beginning is all the time.
I am always experiencing a segmentation fault in the This link is my exact problem .. but I do not understand the solution completely I have an error on this block specifically P> This problem is the block generating. When you come out of the loop Failed with partition violation Change that block:. insert_end function, after
malloc after the structure has to reach
temp1 pointers I am getting an error while trying out. But this is the exact thing that I am also doing in the
insert_first function, but works every time. I tried googling it and tried it in the forums, no answer. Please help.
structure node * temp1, * trav; Templ = (straight node *) Molok (Akarf (node) ();) Trav = (structure node *) Molok (Saikof (structure node)); Data = input, the line ********** temp1- & gt if (! Temp1 = null) {// ****************** division mistake ; } #include & lt; Stdio.h & gt; #to & lt include, stdlib.h & gt; // ******* To capture the list associated with the structure ******** // Structure node {Int data; Structure node * next; } * Heads, * Temporary; // ********** Function to display everything ********* // zero performance (struct node * head) {struct node * trav; Trav = (Structure node *) Molec (Psychophag (Structure node)); Printf ("Entering the Display \ n"); If (head == faucet) {printf ("Oh my God, the list is empty \ n"); } Else {trav = head; While (! Trav = zero) {printf ("The value is stored in [% p] [% d] \ n", travel, trav-> data); Trav = trav- & gt; next; }}} // ********** start ******** // struct node * insert_first (struct node * head, integer input) {temporary For work node *) Molok (formation (structure node)); Temp- & gt; Data = input; Printf ("Entering Enter \ \ Before"); If (head == faucet) {head = temporary; Head-> gt; Next = null; } And {temp-> Next = head; Head = temporary; } Return head; } // insert ************** function end ****************** // struct node * insert_last (struct node * Head, int input) {struct node * temp1, * trav; Templ = (straight node *) Molok (Akarf (node) ();) Trav = (structure node *) Molok (Saikof (structure node)); If (temp1! = NULL) {temp1-> Data = input; } Other {printf ("blank"); } Printf ("Enter \ ninclude entry"); If (head == tap) {head = temp1; Head-> gt; Next = null; } Else {trav = head; While (trav! = NULL) {trav = trav-> next; } Trav-> Next = temp1; } Return head; } / / ************************************************************************************************ ************************************************** ************************************************** ************* = Faucet; While (1) {printf ("\ N ****** Please enter your choice **** \ n1. To insert \ n2 at the beginning \ n2 to insert. \ N 4. To display \ N5 to remove \ n0. \ N ") to exit; Scanf ("% d", & amp; option); Switch (like) {case 1: printf ("enter value to add \ n"); Scanf ("% d", & amp; value); Head = insert_first (head, neck); break; Case 2: printf ("Enter value to add \ n"); Scanf ("% d", & amp; value); Head = insert_last (head, value); break; Case 5: Performance (head); break; Case 0: Return 0; Default: printf ("is a bad choice \ n"); break; "Text" itemprop = "text">
and {trav = head; While (trav! = NULL) {trav = trav-> next; } Trav-> Next = temp1;
trav the
NULL This makes the line
trav-> gt; Next = temp1;
else {trav = head; While (trav-> Next! = NULL) {trav = trav-> next; } Trav-> Next = temp1;
Comments
Post a Comment