int i;
LNode *p;
int array[LIST_INIT_LENGTH]; L=(LinkList)malloc(sizeof(LNode)); L->next=NULL;
printf(\ for(i=0;i for(i=0;i } //end of CreateList_L() function void Contray(LinkList &head) //Contray() function { //Delete the NO.i element of LinkList and return by variable e LNode *p,*q; p=head; head=NULL; while(p) { q=p; p=p->next; q->next=head; head=q; } //end of while cout< void main() //main() function { LinkList L; LNode *p; int i,LNodeNum; //j is just a counter for cycle cout<<\ cout<<\ cin>>LNodeNum; CreateList_L(L,LNodeNum); p=L; 5 cout< cout< cout<<\ Contray(L); //call function Contray(); cout< cout<<\ getch(); }//end of main() function 6、实现单向线性链表插入 # include # define INIT_LENGTH 10 # define OK 1 # define ERROR 0 typedef struct LNode //define LNode structure { int data; struct LNode *next; 6 }LNode,*Linklist; int ListInsert_L(Linklist &L,int i,int e) {//在带头结点的单链线性表L中第i个位置之前插入元素e LNode *p=L; int j=0; while(p&&j if(!p||j>i-1) //i小于1或i大于表长 { cout<<\错误!这个位置不存在!\ return (ERROR); } LNode *s; s=(Linklist)malloc(sizeof(LNode));//生成新结点 s->data=e; s->next=p->next; p->next=s; return (OK); } //ListInsert_L() end void main() //main() function { int i,j,e; LNode node[10]; LNode *L,*p; int array[INIT_LENGTH+1]={5,8,12,18,25,30,37,46,51,89}; L=node; L=(Linklist)malloc(sizeof(LNode)); L->next=NULL; for (i=10;i>0;i--) { p=(Linklist)malloc(sizeof(LNode)); p->data=array[i-1]; p->next=L->next; L->next=p; } p=L; cout< cout < 7 cout< cout<<\请输入要插入的元素: \ cin>>e; if(ListInsert_L(L,j,e)) { cout < cout< 7、实现单向线性链表删除 #include typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList; void CreateList_L(LinkList &L,int n) //CreateList_L() function { //创建一个带头结点的单链表L int i; LNode *p; L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; for(i=0;i 8 cin>>p->data; p->next=L->next; L->next=p; }//结束for }//end of CreateList() function int ListDelete_L(LinkList &L,int i,int &e) //ListDelete_L() function { //在带头结点的单链线性表L中,删除第i个元素,并由e返回其值 LNode *p,*q; int j=0; p=L; while(p->next&&j if(!p||j>i-1) //删除位置不合理 { cout<<\位置\的数据不存在!\ getch(); return(0); } q=p->next; //用指针q指向被删除的结点 p->next=q->next; //删除第i个结点 e=q->data; //取出第i个结点数据域值 free(q); //释放第i个结点 return (e); }//结束删除元素 void main() //main() function { LinkList L; LNode *p; int e; //e can be Every DataType int i,j; //j is just a counter for cycle int LListNodeNum; cout<<\ cout<<\请输入创建的单链表的结点个数: \ cin>>LListNodeNum; cout< cout< while(p) //输出创建的单链表 { cout< 9