Home > other >  Please comment for single table insert problem
Please comment for single table insert problem

Time:10-10

using the following insert function to insert data times wrong singly linked lists, an error prompt for Thread 1: EXC_BAD_ACCESS (code=2, address=0 x10000000a) where an error is insert function of p - & gt; Next=s; Part, the environment for xcode, the use of c language, rookie a novice,, every brother please comment ~
 
Enum Status List_Insert (SqListPtr L, int pos, ElemType elem)
{
Enum Status Status=fail;
Ptr p, s;
P=(Ptr) malloc (sizeof (Node));
Status=List_Retrieve (L, pos - 1, & amp; P);

If (status==success) {
S=(Ptr) malloc (sizeof (Node));

S - & gt; Elem=elem;
S - & gt; Next=p - & gt; Next;
P - & gt; Next=s;
The status=success;

} the else status=range_error;

return status;
}
The head of the list file defined as
Typedef int ElemType;

Typedef enum Status
{
Success=1, fail=0, fatal=1, range_error=2
-} the Status;


Typedef struct Node
{
ElemType elem.
Struct Node * next;
} Node, * Ptr, * * SqListPtr;
The rest of the related functions for the
Enum Status List_Init SqListPtr (L)
{
The Status of s=fail;
Ptr p;
If (* L==NULL)
{
P=(Ptr) malloc (sizeof (Node));
If (p==NULL) return s;
* L=p;
(* L) - & gt; Next=NULL;
S=success;
}
Return s;

}


Enum Status List_Create (SqListPtr L, ElemType Data [], int Len)
{
Enum Status s;
Ptr p;
S=List_Init (L);
If (s==success) {
For (int I=Len - 1; i>=0; - I) {
P=(Ptr) malloc (sizeof (Node));
If (p) {
P - & gt; Elem=Data [I];
P - & gt; Next=(* L) - & gt; Next;
(* L) - & gt; Next=p;
}
The else {
S=fail;
break;
}
}
}
Return s;
}


Void List_Print SqListPtr (L)
{

Ptr p=* L;
While (p - & gt; Next) {
P=p - & gt; Next;
Printf (" % d \ n ", p - & gt; Elem);
}

}


Enum Status List_Retrieve (SqListPtr L, int pos, ElemType * elem)
{
Enum Status s=range_error;
Ptr p=(* L) - & gt; Next;
int i=1;
While (p & amp; & ii++;
P=p - & gt; Next;
}
If (p& & I=={pos)
* elem=p - & gt; Elem.
S=success;
}
Return s;
}
Int main () {

SqListPtr=L (SqListPtr) malloc (sizeof (Node));

Int len=5;

The int Data []={1, 2, 3, 4};

List_Create (L, Data, len);

Int delepos=3;
List_Insert (L, delepos, 23);

return 0;
}

CodePudding user response:

List_Retrieve function the last parameter is what ah, insert function does not match with you.
There is another function with the same?

The Create function, you should be up against the right, you put the head pointer is the last
This should be building a head pointer in the queue, the head pointer elem element is meaningless, articulated real list, behind
After you create a print look at to know

According to the front, transforming the two functions are as follows:
Enum Status List_Retrieve (SqListPtr L, int pos, Ptr * (p1)
{
Enum Status=s Status: : range_error;
Ptr p=(* L) - & gt; Next;
int i=1;
While (p & amp; & ii++;
P=p - & gt; Next;
}
if (p& & I=={pos)
S=success;
}
* p=p;

Return s;
}
Enum Status List_Insert (SqListPtr L, int pos, ElemType elem)
{
Enum Status Status=fail;
Ptr p, s;
//p=(Ptr) malloc sizeof (Node));
Status=List_Retrieve (L, pos - 1, & amp; P);


If (status==success) {
S=(Ptr) malloc (sizeof (Node));
S - & gt; Elem=elem;
S - & gt; Next=p - & gt; Next;
P - & gt; Next=s;
The status=success;

} the else status=status: : range_error;

return status;
}

Int main () {

SqListPtr=L (SqListPtr) malloc (sizeof (Node));

Int len=5;

The int Data []={1, 2, 3, 4};

List_Create (L, Data, len);
List_Print (L);
Int delepos=3;
List_Insert (L, delepos, 23);
List_Print (L);

return 0;
}

CodePudding user response:

The best line debugging, it's easy to see issues;
  • Related