#include
# define OK 1
# define the ERROR 0
# define NULL 0
Typedef int the Status;
Typedef int Elemtype;
Typedef struct Lnode
{
Elemtype data;//node data fields
Struct Lnode * next;//node pointer domain
} Lnode * Linklist; The head//Linklist pointer
//initialize the singly linked list
The Status InitLinklist (Linklist & amp; L) {
L=(Linklist) malloc (sizeof (Lnode));
if(! L)
Return the ERROR;
L - & gt; Next=NULL;
return OK;
}
//1. Create a singly linked list
The Status CreateLinklist (Linklist & amp; L, int a [], int n) {
Linklist p=L;//???
for(int i=0; i
if(! S)
Return the ERROR;
S - & gt; data=https://bbs.csdn.net/topics/a [I];
S - & gt; Next=NULL;
P - & gt; Next=s;//???
P=s;
}
return OK;
}
//2. Print singly linked list
Void DispLinklist Linklist (L) {
Linklist p=L - & gt; Next;
While (p) {
Printf (" % d \ \ t t ", p - & gt; data);
P=p - & gt; Next;
}
printf("\n");
}
//3. Look for in the list e
Elemtype Status LocateLinklist (Linklist L, e) {
Linklist p=L - & gt; Next;
Int I=1;
While (p) {
If (p - & gt; data=https://bbs.csdn.net/topics/=e)
{
Printf (" its position is: % d ", I);
printf("\n");
break;
} else {
P=p - & gt; Next;
i++;
}
}
Return the ERROR;
}
//4. Insert a number in list
The Status InsertLinklist (Linklist & amp; L, Elemtype e, int I) {
Linklist p=L - & gt; Next;
int j=1;
While (p & amp; & jP=p - & gt; Next;
j++;
}
Linklist s=(Linklist) malloc (sizeof (Lnode));
if(! S)
Return the ERROR;
S - & gt; data=https://bbs.csdn.net/topics/e;
S - & gt; Next=p - & gt; Next;
P - & gt; Next=s;
return OK;
}
//5. Delete elements in the list
The Status DeleteLinklist (Linklist & amp; L, int I) {
Elemtype e;
Linklist p=L - & gt; Next, q;
int j=1;
While (p & amp; & jQ=p;
P=p - & gt; Next;
j++;
}
if(! P | | j> I)
Return the ERROR;
E=p - & gt; data;
Printf (" delete elements is: % d ", e);
q-> Next=p - & gt; Next;
Free (p);
return OK;
}
Int main () {
Int a []=,3,5,8,9 {2};
Linklist L;
InitLinklist (L);
CreateLinklist (L, a, sizeof (a)/sizeof (a [0]));
DispLinklist (L);
LocateLinklist (L, 8);
InsertLinklist (L, 4, 3);
DispLinklist (L);
DeleteLinklist (L, 5);
DispLinklist (L);
}
CodePudding user response:
Too long didn't write C, good unfamiliar, I head, change and perfect the others,
The unit LinkList;
Interface
Const
OK=1;
ERROR=0;
NULL=0;
Type
The Status=Integer;
Elemtype=Integer;
PLnode=^ Lnode;
Lnode=record
Data: Elemtype;//node data fields
Next: PLnode;
end;
The function InitLinklist (L: PLnode) : the Status;
The function CreateLinklist (L: PLnode; A: an array of Integer; N: Integer) : the Status;
Implementation
//initialize the singly linked list
The function InitLinklist (L: PLnode) : the Status;
The begin
GetMem (L, sizeof (PLnode));
If (L=nil) then
The begin
Result:=ERROR;
exit;
end;
L ^. Next:=nil;
Result:=OK;
end;
//1. Create a singly linked list
The function CreateLinklist (L: PLnode; A: an array of Integer; N: Integer) : the Status;
Var
I: integer;
P, s: PLnode;
The begin
P:=L;//points to the chain first
Do the for I:=0 to n - 1
The begin
GetMem (s, sizeof (PLnode));
If (s=nil) then
The begin
Result:=ERROR;
exit;
end;
S ^. Data:=a, [I].
S ^. Next:=nil;
P ^. Next:=s;//new nodes on a list
P:=s;
end;
Result:=OK;
end;
End.