6_双向循环链表

来源:互联网 发布:java用正则表达式 编辑:程序博客网 时间:2024/04/30 13:12
#include <stdio.h>#include <stdlib.h>typedef int ElemType;#define  OVERFLOW -1#define  Status int#define  OK 1#define  ERROR 0typedef struct DuLNode{ElemType data;DuLNode *prior,*next;}DuLNode,*DuLinkList;void InitDoubleLinked(DuLinkList &pHead){pHead=(DuLinkList)malloc(sizeof(DuLNode));if (!pHead){exit(OVERFLOW);}else{pHead->prior=pHead->next=pHead;}}void DestroyList(DuLinkList &pHead){ DuLinkList p_next,p=pHead->next; while (p!=pHead) { p_next=p->next; free(p);; p=p_next; } free(pHead);}void ClearList(DuLinkList pHead){DuLinkList p_next,p=pHead->next;while (p!=pHead){p_next=p->next;free(p);p=p_next;}}Status ListEmpty(DuLinkList pHead){ if (pHead->next==pHead&&pHead->prior==pHead) { return OK; }else{ return ERROR; }}int ListLength(DuLinkList pHead){int i=0;DuLinkList pTail=pHead->next;while (pTail!=pHead){i++;pTail=pTail->next;}return i;}Status GetElem(DuLinkList pHead,int pos,ElemType &e){int cur_pos=1;DuLinkList pTail=pHead->next;while (pTail!=pHead&&cur_pos<pos)//pTail==pHead证明这个时候已经到最后一个了,cur_pos==pos{pTail=pTail->next;cur_pos++;}if (pTail==pHead||cur_pos>pos)//{return ERROR;}e=pTail->data;return OK;}int LocateElem(DuLinkList pHead,ElemType e){int i=0;DuLinkList pTail=pHead->next;while (pTail!=pHead){if (pTail->data==e){return i;}i++;}return 0;}Status PriorElem(DuLinkList pHead,ElemType cur_e,ElemType &pre_e){DuLinkList pTail=pHead->next->next;while (pTail!=pHead){if (pTail->data==cur_e){pre_e=pTail->prior->data;return OK;}pTail=pTail->next;}return ERROR;}Status NextElem(DuLinkList pHead,ElemType cur_e,ElemType &next_e){DuLinkList pTail=pHead->next;while (pTail!=pHead){ if (cur_e==pTail->data) { next_e=pTail->next->data; return OK; }pTail=pTail->next;}return ERROR;}DuLinkList GetElemP(DuLinkList pHead,int i){DuLinkList pTail=pHead;if (0<i||i>ListLength(pHead)){printf("ListInsert:输入参数不对,检查pos和pHead链表的长度\n");return NULL;}for (int j=1;j<i;j++){if (j==i){return pTail;}pTail=pTail->next;}return pTail;}Status ListInsert(DuLinkList pHead,int pos,ElemType data){if (pos<0||pos>ListLength(pHead)){printf("ListInsert:输入参数不对,检查pos和pHead链表的长度\n");return ERROR;}int cur_pos=1;DuLinkList pTail=pHead->next;while (pTail!=pHead&&cur_pos<pos){pTail=pTail->next;cur_pos++;}if (pTail==pHead||cur_pos>pos){return ERROR;}DuLinkList pNew=(DuLinkList )malloc(sizeof(DuLNode));pNew->data=data;DuLinkList pNext=pTail->next;pTail->next=pNew;pNew->prior=pTail;pNext->prior=pNew;pNew->next=pNext;return OK;}void addNode(DuLinkList pHead,ElemType data){DuLinkList pNew=(DuLinkList)malloc(sizeof(DuLNode));pNew->data=data;if (pHead->next==NULL){pHead->next=pNew;pHead->prior=pNew;pNew->next=pHead;pNew->prior=pHead;}else{DuLinkList pNext=pHead->next;pHead->next=pNew;pNew->prior=pHead;pNew->next=pNext;pNext->prior=pNew;}}void ListTraverse(DuLinkList L){ // 由双链循环线性表L的头结点出发,正序对每个数据元素调用函数visit()DuLinkList p=L->next; // p指向头结点while(p!=L){printf("%d\t",p->data);p=p->next;}printf("\n");}void ListTraverseBack(DuLinkList L){ // 由双链循环线性表L的头结点出发,逆序对每个数据元素调用函数visit()。另加DuLinkList p=L->prior; // p指向尾结点while(p!=L){printf("%d\t",p->data);p=p->prior;}printf("\n");}Status ListDelete(DuLinkList pHead,int pos,ElemType &data){if (pos<0||pos>ListLength(pHead)){printf("ListDelete:检查pos的值,和ListLength的长度对比");return ERROR;}int cur_pos=0;DuLinkList pTail=pHead->next;while (pTail!=pHead&&cur_pos<pos){pTail=pTail->next;cur_pos++;}if (pTail==pHead||cur_pos>pos){printf("ListDelete:检查pos的值,和ListLength的长度对比,pTail是否等于pHead");return ERROR;}//DuLinkList pCur=pTail;pTail->next->prior=pTail->prior;pTail->prior->next=pTail->next;data=pTail->data;free(pTail);    return OK;}Status merge(DuLinkList fHead,DuLinkList sHead){if (ListEmpty(fHead)||ListEmpty(sHead)){printf("merge:两个空链表,无法合并\n");return ERROR;}DuLinkList fpTail=fHead->next;DuLinkList spTail=sHead->next;while (fpTail->next!=fHead){fpTail=fpTail->next;}while (spTail->next!=sHead){spTail=spTail->next;}//第二个链表的最后一个节点,指向第一个节点的头DuLinkList fLast=fpTail;DuLinkList sLast=spTail;sLast->next=fHead;fHead->prior=sLast;//fLast->next=sHead->next;sHead->next->prior=fLast;delete sHead;}void buble_sort(DuLinkList pHead){DuLinkList fTail=pHead->next;DuLinkList sTail=fTail->next;while (fTail->next!=pHead){   sTail=fTail->next; while (sTail!=pHead) { if (sTail->data>fTail->data) { ElemType temp=sTail->data;sTail->data=fTail->data;fTail->data=temp; } sTail=sTail->next; }fTail=fTail->next;}}void main(){DuLinkList pHead=NULL;InitDoubleLinked(pHead);for (int i=0;i<5;i++){addNode(pHead,i);}ListTraverse(pHead);DuLinkList sHead=NULL;InitDoubleLinked(sHead);for (int i=5;i<10;i++){addNode(sHead,i);}ListTraverse(sHead);merge(pHead,sHead);ListTraverse(pHead);//sort_insertion(pHead);//DestroyList(pHead);//DestroyList(sHead);buble_sort(pHead);ListTraverse(pHead);system("pause");}

原创粉丝点击