数据结构之双向循环链表
创新互联是一家专注于网站设计制作、成都网站制作与策划设计,榕城网站建设哪家好?创新互联做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:榕城等地区。榕城做网站价格咨询:13518219792实例代码:
#include <stdlib.h> #include <stdio.h> #include <malloc.h> typedef struct Node{ struct Node *pNext; int data; struct Node *prior; } NODE,*PNODE; PNODE CreatList(); void TreNode(PNODE pHead); bool isEmpty(PNODE pHead); int getCount(PNODE pHead); bool insertNode (PNODE pHead,int pos,int val); bool detNode (PNODE pHead,int pos,int *pVal); void sort(PNODE pHead); int main(){ int val; PNODE pHead = NULL; pHead = CreatList(); TreNode(pHead); printf("链表不为空!\n"); printf("链表不为空!\n"); if(insertNode(pHead, 4, 7)) printf("插入成功!插入的元素为:%d\n", 7); else printf("插入失败!\n"); //重新遍历链表 TreNode(pHead); //删除元素测试 if(detNode(pHead, 3, &val)) printf("元素删除成功!删除的元素是:%d\n", val); else printf("元素删除失败!\n"); TreNode(pHead); return 0 ; } PNODE CreatList(){ int i ; int val; int length; PNODE pHead = (PNODE)malloc(sizeof(NODE)); if(NULL==pHead) { printf("创建链表失败!\n"); exit(-1); } pHead->pNext = NULL; pHead->prior = NULL; PNODE pTail = pHead; printf("请输入您想要创建链表结点的个数:len = "); scanf("%d", &length); for(i = 0;i < length;i++){ printf("请输入第%d个结点的值:", i+1); scanf("%d", &val); PNODE pnew = (PNODE)malloc(sizeof(NODE)); if(NULL==pHead) { printf("创建链表失败!\n"); exit(-1); } pnew->data = val; // pTail->pNext = pnew; pTail->pNext = pnew; pnew->prior = pTail; pnew->pNext = pHead; pHead->prior = pnew; pTail = pnew; } return pHead; } void TreNode(PNODE pHead){ PNODE p = pHead->pNext; while(p!= pHead ){ printf("%d",p->data); p = p->pNext; } printf("\n"); return ; } bool isEmpty(PNODE pHead){ if(NULL==pHead->pNext) return true; else return false; } int getCount(PNODE pHead) { int i =0 ; PNODE p = pHead->pNext; while(NULL != p){ p = p->pNext; i++; } return i ; } bool insertNode (PNODE pHead,int pos,int val){ int i = 0; // PNODE p = pHead; PNODE p =pHead; while(NULL !=p && i<pos-1){ p = p->pNext; i++; } if(NULL == p || i>pos-1) { return false; } scanf("%d", &val); PNODE pNew = (PNODE)malloc(sizeof(NODE)); if(NULL==pNew) { printf("创建链表失败!\n"); exit(-1); } pNew ->data = val; PNODE q = p->pNext; p->pNext = pNew; pNew->prior = p; pNew->pNext = q; return true; } bool detNode (PNODE pHead,int pos,int *pVal){ int i = 0; // PNODE p = pHead; PNODE p = pHead; while(NULL !=p && i<pos-1){ p = p->pNext; i++; } if(NULL == p || i>pos-1) { return false; } *pVal = p->pNext->data; PNODE q = p->pNext; p->pNext = p->pNext->pNext; free(q); q = NULL; return true; } void sort(PNODE pHead){ int i,j,k,t; k = getCount(pHead); PNODE p,q; for(i = 0,p=pHead->pNext;i < k-1;i++,p = p->pNext) { for(j = i+1,q = p->pNext;j<k;j++,q = q->pNext){ if(p->data > q->data ){ t = p->data; p->data = q->data; q->data = t; } } } }
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享文章:C语言数据结构之双向循环链表的实例-创新互联
转载源于:https://www.cdcxhl.com/article4/dhghoe.html
成都网站建设公司_创新互联,为您提供小程序开发、云服务器、网站策划、Google、关键词优化、商城网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联