欢迎来到天天文库
浏览记录
ID:17463551
大小:29.50 KB
页数:9页
时间:2018-09-01
《双向链表模版_by_jason》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、双向链表模版_BY_JASON#include#include#defineN10000#defineELEMTYPEint#defineNODETYPEstructnode#defineLISTTYPEstructlinklist#defineHEADmyList->head#defineASSIGN_ERRORprintf("failtoassignmemmory")NODETYPE{ELEMTYPEdata;NODETYPE*next;NODETYPE*pre;};LISTTYPE{NODETYPE*he
2、ad;};voidInit_List(LISTTYPE*myList,NODETYPE*myNodeHead,ELEMTYPEelem);voidInsert_2nd(LISTTYPE*myList,ELEMTYPEelem);voidInsert_Tail(LISTTYPE*myList,ELEMTYPEelem);voidInsert_Order(LISTTYPE*myList,ELEMTYPEelem);voidPrint_List(LISTTYPE*myList);ELEMTYPEDelete_Nth(LISTTYPE*myList,intn)
3、;voidDelete_Elem(LISTTYPE*myList,ELEMTYPEelem);intmain(){inti=0;LISTTYPE*myList=(LISTTYPE*)malloc(sizeof(LISTTYPE));NODETYPE*myNodeHead=(NODETYPE*)malloc(sizeof(NODETYPE));if(!(myList&&myNodeHead))ASSIGN_ERROR;Init_List(myList,myNodeHead,N);for(i=0;i4、andom()%N);}Print_List(myList);/*Delete_Nth(myList,1);Delete_Nth(myList,N);Delete_Nth(myList,N-10);Print_List(myList);Delete_Elem(myList,3);Print_List(myList);*/for(i=0;i5、;Delete_Nth(myList,1);Delete_Elem(myList,10);return0;}voidInit_List(LISTTYPE*myList,NODETYPE*myNodeHead,ELEMTYPEelem){//initialheadnode//myList=(LISTTYPE*)malloc(sizeof(LISTTYPE));//NODETYPE*newNode=(NODETYPE*)malloc(sizeof(NODETYPE));HEAD=myNodeHead;HEAD->data=elem;HEAD->next=N6、ULL;HEAD->pre=NULL;}voidInsert_2nd(LISTTYPE*myList,ELEMTYPEelem){//insertelemmentintothe2ndpositionNODETYPE*newNode=(NODETYPE*)malloc(sizeof(NODETYPE));if(!newNode)ASSIGN_ERROR;newNode->data=elem;newNode->pre=HEAD;newNode->next=HEAD->next;HEAD->next=newNode;}voidInsert_Tail(LIST7、TYPE*myList,ELEMTYPEelem){//insertelemmentintothelastpositionNODETYPE*p=HEAD;NODETYPE*newNode=(NODETYPE*)malloc(sizeof(NODETYPE));if(!newNode)ASSIGN_ERROR;while(p->next)//makesure*pisthelastnode{p=p->next;}newNode->data=elem;newNode->pre=p;newNode->next=p->next;p->next=newNode;}8、voidInsert_Order(LISTTYPE*myList,ELEMTYPEelem){
4、andom()%N);}Print_List(myList);/*Delete_Nth(myList,1);Delete_Nth(myList,N);Delete_Nth(myList,N-10);Print_List(myList);Delete_Elem(myList,3);Print_List(myList);*/for(i=0;i5、;Delete_Nth(myList,1);Delete_Elem(myList,10);return0;}voidInit_List(LISTTYPE*myList,NODETYPE*myNodeHead,ELEMTYPEelem){//initialheadnode//myList=(LISTTYPE*)malloc(sizeof(LISTTYPE));//NODETYPE*newNode=(NODETYPE*)malloc(sizeof(NODETYPE));HEAD=myNodeHead;HEAD->data=elem;HEAD->next=N6、ULL;HEAD->pre=NULL;}voidInsert_2nd(LISTTYPE*myList,ELEMTYPEelem){//insertelemmentintothe2ndpositionNODETYPE*newNode=(NODETYPE*)malloc(sizeof(NODETYPE));if(!newNode)ASSIGN_ERROR;newNode->data=elem;newNode->pre=HEAD;newNode->next=HEAD->next;HEAD->next=newNode;}voidInsert_Tail(LIST7、TYPE*myList,ELEMTYPEelem){//insertelemmentintothelastpositionNODETYPE*p=HEAD;NODETYPE*newNode=(NODETYPE*)malloc(sizeof(NODETYPE));if(!newNode)ASSIGN_ERROR;while(p->next)//makesure*pisthelastnode{p=p->next;}newNode->data=elem;newNode->pre=p;newNode->next=p->next;p->next=newNode;}8、voidInsert_Order(LISTTYPE*myList,ELEMTYPEelem){
5、;Delete_Nth(myList,1);Delete_Elem(myList,10);return0;}voidInit_List(LISTTYPE*myList,NODETYPE*myNodeHead,ELEMTYPEelem){//initialheadnode//myList=(LISTTYPE*)malloc(sizeof(LISTTYPE));//NODETYPE*newNode=(NODETYPE*)malloc(sizeof(NODETYPE));HEAD=myNodeHead;HEAD->data=elem;HEAD->next=N
6、ULL;HEAD->pre=NULL;}voidInsert_2nd(LISTTYPE*myList,ELEMTYPEelem){//insertelemmentintothe2ndpositionNODETYPE*newNode=(NODETYPE*)malloc(sizeof(NODETYPE));if(!newNode)ASSIGN_ERROR;newNode->data=elem;newNode->pre=HEAD;newNode->next=HEAD->next;HEAD->next=newNode;}voidInsert_Tail(LIST
7、TYPE*myList,ELEMTYPEelem){//insertelemmentintothelastpositionNODETYPE*p=HEAD;NODETYPE*newNode=(NODETYPE*)malloc(sizeof(NODETYPE));if(!newNode)ASSIGN_ERROR;while(p->next)//makesure*pisthelastnode{p=p->next;}newNode->data=elem;newNode->pre=p;newNode->next=p->next;p->next=newNode;}
8、voidInsert_Order(LISTTYPE*myList,ELEMTYPEelem){
此文档下载收益归作者所有