#include<stdio.h>#include<stdlib.h>#include<time.h>typedefstruct ListNode{int data;struct ListNode *next;}ListNode;typedefstruct List{
ListNode head;//变量,不是指针,方便链表节点的插入和删除,虚拟头节点(没有数据域)int length;//当前链表的长度信息}List;//初始化结点
ListNode *getNewNode(int);//初始化链表
List *getLinkList();//删除结点voidclear_node(ListNode *);//删除链表voidclear(List *);//链表的插入int insert (List *,int,int);//链表的删除interase(List *,int);//reversevoidreverse(List *l);//输出voidoutput(List *);
ListNode *getNewNode(int val){
ListNode *p=(ListNode *)malloc(sizeof(ListNode));
p->data=val;
p->next=NULL;return p;}
List *getLinkList(){
List *l=(List *)malloc(sizeof(List));
l->head.next=NULL;
l->length=0;return l;}intinsert(List *l ,int ind,int val){if(l==NULL)return0;if(ind<0||ind>l->length)return0;//p指向虚拟头节点的地址,防止在头结点插入
ListNode *p=&(l->head),*node=getNewNode(val);while(ind--) p=p->next;
node->next=p->next;
p->next=node;
l->length +=1;return1;}interase(List *l,int ind){if(l==NULL)return0;if(ind<0|| ind>=l->length)return0;
ListNode *p=&(l->head),*q;while(ind--){
p=p->next;}
q=p->next;
p->next=q->next;free(q);
l->length-=1;return1;}voidreverse(List *l){
ListNode *p=l->head.next,*q;
l->head.next=NULL;while(p){
q=p->next;
p->next=l->head.next;
l->head.next=p;
p=q;}return;}voidoutput(List *l){if(l==NULL)return;printf("List(%d)=[",l->length);for(ListNode *p=l->head.next;p;p=p->next){printf("%d->",p->data);}printf("NULL]\n");return;}voidclear_node(ListNode *node){if(node==NULL)return;free(node);return;}voidclear(List *l){if(l==NULL)return;
ListNode *p=l->head.next,*q;while(p){
q=p->next;clear_node(p);
p=q;}free(l);return;}intmain(){srand(time(0));#define max_op 20
List *l=getLinkList();for(int i=0;i<max_op;i++){int val=rand()%100;//模拟所有输出int ind=rand()%(l->length+3)-1;int op=rand()%5;//0,1,2,插入,3删除,4反转switch(op){case0:case1:case2:{printf("insert %d at %d to List = %d\n",val,ind,insert(l,ind, val));break;}case3:printf("erase iterm at %d from List = %d\n",ind ,erase(l,ind));break;case4:printf("reverse the List !\n");reverse(l);break;}output(l);printf("\n");}#undef max_op clear(l);return0;}
erase iterm at 1 from List =0List(0)=[NULL]
insert 9 at 1 to List =0List(0)=[NULL]
insert 32 at 0 to List =1List(1)=[32->NULL]
insert 62 at 1 to List =1List(2)=[32->62->NULL]
insert 26 at -1 to List =0List(2)=[32->62->NULL]
insert 76 at 2 to List =1List(3)=[32->62->76->NULL]
insert 33 at 4 to List =0List(3)=[32->62->76->NULL]
insert 29 at -1 to List =0List(3)=[32->62->76->NULL]
insert 75 at 2 to List =1List(4)=[32->62->75->76->NULL]
insert 88 at -1 to List =0List(4)=[32->62->75->76->NULL]
reverse the List !List(4)=[76->75->62->32->NULL]
insert 72 at 0 to List =1List(5)=[72->76->75->62->32->NULL]
insert 46 at 3 to List =1List(6)=[72->76->75->46->62->32->NULL]
insert 36 at 7 to List =0List(6)=[72->76->75->46->62->32->NULL]
erase iterm at 7 from List =0List(6)=[72->76->75->46->62->32->NULL]
insert 68 at 3 to List =1List(7)=[72->76->75->68->46->62->32->NULL]
erase iterm at 3 from List =1List(6)=[72->76->75->46->62->32->NULL]
insert 43 at 4 to List =1List(7)=[72->76->75->46->43->62->32->NULL]
erase iterm at 0 from List =1List(6)=[76->75->46->43->62->32->NULL]
insert 70 at 3 to List =1List(7)=[76->75->46->70->43->62->32->NULL]