Achieve a single list

Because in the transfer function parameters, the use of references, so that the program is a C ++ program

#include <stdio.h>
#include <stdlib.h>
#define ElemType int
typedef struct LNode{
ElemType data;
struct LNode *next;
int length;
}LNode, *LinkList;
int CreatList(LinkList &L){
L=(LinkList)malloc(sizeof(LNode));
if(L==NULL){printf("创建失败");return 0;}
L->next=NULL;
return 1;
}//创建一个链表的头结点
void ListTailInsert(LinkList &L,ElemType e){
LinkList r,s;
r=L;
s=(LinkList)malloc(sizeof(LNode));
s->data=e;
s->next=NULL;
while(r->next!=NULL){
r=r->next;
}
r->next=s;
}//尾插法填充节点
void ListPrint(LinkList &L){
LinkList p;
0 = I int;
P = L-> Next;
the printf ( "list is:");
the while (! P = NULL) {
the printf ( "% D", p-> Data);
P = p-> Next;
I ++ ;
}
the printf ( "\ n-");
L-> I = length;
the printf ( "length of list:% D \ n-", L-> length);
} // print list
LNode * getElem (LinkList & L, int i) {
int J =. 1;
LinkList L-P => Next;
IF (i <. 1 || i> L-> length) {
the printf ( "find position i invalid");
}
! the while (P = NULL ) {
IF (J == i) P {return;}
P = p-> Next;
J ++;
}
} // find the i-th node
LNode LocateElem * (L & LinkList, elemType E) {
LinkList = L-P> Next;
int In Flag = 0;
the while (! P = NULL) {
IF (p->data==e){flag=1;return p;}
p=p->next;
}
IF (In Flag == 0) {the printf ( "the node is not found");}
} // look-up table according to the value of the node and the node returns
void ListInsert (LinkList & L, I int, elemType E) {
LinkList S, P;
IF (i <0 || i> L-> length) {the printf ( "inserted at position i invalid");}
S = (LinkList) the malloc (the sizeof (LNode));
S-> Data = E;
IF (I ==. 1) {
S-> Next = L-> Next;
L-> Next = S;
} // a special case of the first node insertion
else {p = getElem (L, i-1) ;
S-> Next = p-> Next;
p-> Next = S;}
} // e is a value of the node
void ListDelete (LinkList L &, int I) {
LinkList P;
IF (I <0 || I > L-> length) {printf ( " invalid position i deleted");}
IF (i ==. 1) {
L-> Next = L-> next-> Next;
} // at the first node remove the special case of
the else {getElem P = (L, I-. 1);
p-> Next = p->next-> Next;}
} // remove the i-th node
int main(){
LinkList L;
LinkList q;
LinkList p;
CreatList(L);
ListTailInsert(L,1);
ListTailInsert(L,2);
ListTailInsert(L,3);
ListTailInsert(L,4);
ListTailInsert(L,5);
ListPrint(L);
q=LocateElem(L,2);
printf("%d\n",q->data);
p=GetElem(L,3);
printf("%d\n",p->data);
ListInsert(L,1,6);
ListInsert(L,3,7);
ListPrint(L);
ListDelete(L,1);
ListDelete(L,5);
ListPrint(L);
return 0;
}

The following operating results for the

Guess you like

Origin www.cnblogs.com/Yshun/p/11140924.html