创建双链表c语言(用C语言创建双向链表顺时针数到n删除一个节点,逆时针数到m删除一个节点,输出只有一个节点时节点数值)

2024-05-29 20:10:06 :19

创建双链表c语言(用C语言创建双向链表顺时针数到n删除一个节点,逆时针数到m删除一个节点,输出只有一个节点时节点数值)

大家好,关于创建双链表c语言很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于用C语言创建双向链表顺时针数到n删除一个节点,逆时针数到m删除一个节点,输出只有一个节点时节点数值的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决您的问题,还望关注下本站哦,希望对各位有所帮助!

本文目录

用C语言创建双向链表顺时针数到n删除一个节点,逆时针数到m删除一个节点,输出只有一个节点时节点数值

#include 《stdio.h》#include 《stdlib.h》struct cyclechain{    struct cyclechain* pre;    struct cyclechain* next;    unsigned int message;};typedef struct cyclechain CC;  //定义链表节点结构CC* entry=NULL;                //环链表入口void creatchain(unsigned int m) //建立自然数环链{    CC* lastnode;    unsigned int i;    if(entry==NULL){        entry=(CC*)malloc(sizeof(CC));        entry-》pre=entry;        entry-》next=entry;        entry-》message=1;    }    lastnode=entry;    for(i=2;i《=m;i++){        lastnode-》next=(CC*)malloc(sizeof(CC));        lastnode-》next-》pre=lastnode;        lastnode-》next-》message=i;        lastnode=lastnode-》next;    }    entry-》pre=lastnode;    lastnode-》next=entry; }

 

void posdel(unsigned int n,CC* entrynode,CC** presentnode) //正向删除,最后个参数代表删除后,节点位置{    while(--n){        entrynode=entrynode-》next;    }    *presentnode=entrynode-》pre;    printf("posdel message:%d\n",entrynode-》message);    entrynode-》pre-》next=entrynode-》next;    entrynode-》next-》pre=entrynode-》pre;    free(entrynode);}

void negdel(unsigned int k,CC* entrynode,CC** presentnode)//反向删除,最后个参数代表删除后,节点位置{    while(--k){        entrynode=entrynode-》pre;    }    *presentnode=entrynode-》next;    printf("negdel message:%d\n",entrynode-》message);    entrynode-》pre-》next=entrynode-》next;    entrynode-》next-》pre=entrynode-》pre;    free(entrynode);

}unsigned char checkchain(CC* entry)    //判定是否为链表最后一个元素{if(entry-》pre==entry) return 1;else    return 0;}

int main(){    CC* presentnode;     creatchain(6);   //创建链表     presentnode=entry;     //一直删除元素,直到只剩一个元素    while(1){        if(checkchain(presentnode)) //检测是否为最后个元素            break;        posdel(3,presentnode,&presentnode);        if(checkchain(presentnode))            break;        negdel(4,presentnode,&presentnode);        if(checkchain(presentnode))            break;    }     printf("result:%d\n",presentnode-》message);    free(presentnode);               //释放最后个节点的堆内存     return 0;}  没做函数参数安全性处理,楼主自己改改

如何创建一个空的c语言双向循环链表

至少需要一个元素,空的不能能建立数据结构。

1.循环链表 循环链表是与单链表一样,是一种链式的存储结构,所不同的是,循环链表的最后一个结点的指针是指向该循环链表的第一个结点或者表头结点,从而构成一个环形的链。循环链表的运算与单链表的运算基本一致。所不同的有以下几点:  1)在建立一个循环链表时,必须使其最后一个结点的指针指向表头结点,而不是象单链表那样置为NULL。此种情况还使用于在最后一个结点后插入一个新的结点。  2)在判断是否到表尾时,是判断该结点链域的值是否是表头结点,当链域值等于表头指针时,说明已到表尾。而非象单链表那样判断链域值是否为NULL。2.双向链表  双向链表其实是单链表的改进。  当我们对单链表进行操作时,有时你要对某个结点的直接前驱进行操作时,又必须从表头开始查找。这是由单链表结点的结构所限制的。因为单链表每个结点只有一个存储直接后继结点地址的链域,那么能不能定义一个既有存储直接后继结点地址的链域,又有存储直接前驱结点地址的链域的这样一个双链域结点结构呢?这就是双向链表。3.双向循环链表例程:

#include 《stdio.h》#include 《stdlib.h》typedef struct tagDbNode{ int data; struct tagDbNode * left; struct tagDbNode * right;} DbNode, * pdbNode;//创建结点pdbNode CreateNode(int data){ pdbNode pnode = (pdbNode)malloc(sizeof(DbNode)); pnode-》data = data; pnode-》left = pnode-》right = pnode; //创建新结点时,让其前驱和后继指针都指向自身  return pnode;}//创建链表pdbNode CreateList(int head)  //参数给出表头结点数据 (表头结点不作为存放有意义数据的结点){ pdbNode pnode = (pdbNode)malloc(sizeof(DbNode)); pnode-》data = head; pnode-》left = pnode-》right = pnode; return pnode;}//插入新结点,总是在表尾插入; 返回表头结点pdbNode InsertNode(pdbNode node, int data) // 参数1是链表的表头结点,参数2是要插入的结点(结点数据为data){ pdbNode pnode = CreateNode(data);  // 从左到右恢复链接 node-》left-》right = pnode; pnode-》right = node;  // 从右到左恢复链接 pnode-》left = node-》left; node-》left = pnode;  return node;}//查找结点,成功则返回满足条件的结点指针,否则返回NULLpdbNode FindNode(pdbNode node, int data) // 参数1是链表的表头结点,参数2是要查找的结点(其中结点数据为data){ pdbNode pnode = node-》right; while (pnode != node && pnode-》data != data) {  pnode = pnode-》right; } if (pnode == node)  return NULL; return pnode;}//删除满足指定条件的结点, 返回表头结点, 删除失败返回NULL(失败的原因是不存在该结点)pdbNode DeleteNode(pdbNode node, int data) // 参数1是链表的表头结点,参数2是要删除的结点(其中结点数据为data){ pdbNode pnode = FindNode(node, data); if (NULL == pnode) return NULL; pnode-》left-》right=pnode-》right; pnode-》right-》left=pnode-》left; free(pnode); return node;}//获取链表的长度int GetLength(pdbNode node) // 参数为链表的表头结点{ int nCount = 0; pdbNode pnode = node-》right; while (pnode!= node) {     pnode = pnode-》right;   nCount++; } return nCount;}//顺序打印整个链表void PrintList(pdbNode node) // 参数为链表的表头结点{ pdbNode pnode; if (NULL == node) return; pnode= node-》right; while (pnode != node) {  printf("%d   ", pnode-》data);  pnode = pnode -》right; } printf("\n");}//将链表反序打印void ReverPrint(pdbNode node) //参数为链表的表头结点{ pdbNode pnode; if (NULL == node) return; pnode= node-》left; while (pnode != node) {  printf("%d   ", pnode-》data);  pnode = pnode-》left; } printf("\n");}//删除链表void DeleteList(pdbNode node) //参数为链表表头结点{ pdbNode pnode = node-》right; pdbNode ptmp; if (NULL == node) return;  while (pnode != node) {  ptmp = pnode-》right;  free(pnode);  pnode = ptmp; } free(node);}//测试程序#include 《stdio.h》#include 《stdlib.h》#include "dblist.h"#define FALSE 0#define TRUE  1typedef unsigned int bool;void main(){ int nChoose; int data; bool bFlag = FALSE; pdbNode pnode; pdbNode list = CreateList(0);  while(bFlag == FALSE) {  printf("Main Menu\n");  printf("1.  Insert\n");  printf("2.  Delete Node\n");  printf("3.  Find\n");  printf("4.  Length\n");  printf("5.  Positive Print\n");  printf("6.  Negative Print\n");  printf("7.  Delete List\n");  printf("0.  quit\n\n");   scanf("%d", &nChoose);   switch(nChoose)  {  case 1:   printf("Input the data to insert:");   scanf("%d", &data);   list = InsertNode(list, data);   PrintList(list);   printf("\n");   break;  case 2:   printf("Input the data to delete: ");   scanf("%d", &data);   DeleteNode(list, data);   PrintList(list);   printf("\n");   break;  case 3:   printf("Input the data to find: ");   scanf("%d", &data);   pnode = FindNode(list, data);      if (NULL != pnode)   {    printf("Find succeed!\n"); printf("\n");   }   else   {    printf("Find failed!\n"); printf("\n");   }   break;  case 4:   printf("The list’s length is %d\n", GetLength(list));   printf("\n");   break;  case 5:   PrintList(list);   printf("\n");   break;  case 6:   ReverPrint(list);   printf("\n");   break;  case 7:   DeleteList(list);   printf("\n");   break;  case 0:   DeleteList(list);   bFlag = TRUE;  } }}

用C语言编写一个程序,建立双向循环链表,并实现它的插入操作、删除操作

望笑纳~ //CreateList_L.cpp//To create a LinkList#include 《stdlib.h》#include 《iostream.h》#include 《conio.h》# define TRUE 1# define FALSE 0# define OK 1# define ERROR 0# define INFEASIBLE -1# define OVERFLOW -2 typedef struct DuLNode { int data; struct DuLNode *prior; struct DuLNode *next;}DuLNode,*DuLinkList;// 初始条件:L已存在。操作结果:返回L中数据元素个数int ListLength(DuLinkList L){ int i=0; DuLinkList p=L-》next; // p指向第一个结点 while(p!=L) // p没到表头 { i++; p=p-》next; } return i;} // 由双链循环线性表L的头结点出发,正序输出每个数据元素void ListTraverse(DuLinkList L) { DuLinkList p=L-》next; while(p!=L) { cout《《p-》data《《"\t"; p=p-》next; } cout《《endl; }// 由双链循环线性表L的头结点出发,逆序输出每个数据元素 void ListTraverseBack(DuLinkList L) { DuLinkList p=L-》prior; while(p!=L) { cout《《p-》data《《"\t"; p=p-》prior; } cout《《endl; } //To Creatre a DuLinkList L with HeadNode void CreateList_DuL(DuLinkList &L) { int n; int i; DuLNode *p; L=(DuLinkList)malloc(sizeof(DuLNode)); L-》next=L-》prior=L; cout《《"CreateList_L"《《endl《《"================"《《endl; cout《《"Please input the Init DuLinkNode Number: 《eg. 5》 "; cin》》n; cout《《"Please input the data for DuLinkList Nodes: 《eg. 34,67,3,-9,45,...》"《《endl; for(i=n;i》0;--i) { p=(DuLinkList)malloc(sizeof(DuLNode)); cin》》p-》data; //Reverse order inputing for Creating a LinkList p-》prior=L; p-》next=L-》next; L-》next-》prior=p; L-》next=p; } if(n) { cout《《endl; cout《《"Success to Create a DuLinkList !"《《endl; ListTraverse(L); cout《《endl; } else cout《《"A NULL DuLinkList have been created !"《《endl;} //ListInsert_Dul()int ListInsert_DuL(DuLinkList &L) { DuLNode *p=L,*s; int j=0; int i; int e; cout《《"======"《《"before insert:"《《"======"《《endl; ListTraverse(L); cout《《"input the location you want to insert:"; cin》》i; while (i《1||i》ListLength(L)+1) { //i值不合法 cout《《"illeagle location,please input the correct location:"; cin》》i; } cout《《"input the number you want to insert:"; cin》》e; while(j《i) { p=p-》next; ++j; } if(!(s=(DuLinkList)malloc(sizeof(DuLNode)))) { cout《《endl《《"Allocate space failure ! " ; return (ERROR); } s-》data=e; s-》prior=p-》prior; s-》next=p; if(i==1) L-》next=s; p-》prior-》next=s; p-》prior=s; cout《《"has insert:"《《e《《endl; ListTraverse(L); cout《《"======"《《"after insert:"《《"======"《《endl《《endl; return (OK);} // 删除带头结点的双链循环线性表L的第i个元素,i的合法值为1≤i≤表长int ListDelete(DuLinkList L) { DuLinkList p; int j=1; // j为计数器 int e; int i; cout《《"input the location of the number you want to delete"《《endl; cin》》i; while(i《0||i》ListLength(L)) { //i值不合法 cout《《"illeagle location,please input the correct location:"; cin》》i; } p=L-》next; // p指向第一个结点 while(p!=L&&j《i) // 顺指针向后查找,直到p指向第i个元素或p指向头结点 { p=p-》next; j++; } if(p==L||j》i) // 第i个元素不存在 { cout《《"第i个元素不存在"《《endl; return ERROR; } else { cout《《"======"《《"before delete:"《《"======"《《endl; ListTraverse(L); e=p-》data; // 取第i个元素 if(!p) // p=NULL,即第i个元素不存在 return ERROR; e=p-》data; p-》prior-》next=p-》next; p-》next-》prior=p-》prior; free(p); cout《《"has delete:"《《e《《endl; ListTraverse(L); cout《《"======"《《"after delete:"《《"======"《《endl《《endl; return OK; } }void menu(int c){ cout《《"================================================="《《endl; cout《《"\t\t1----建立"《《endl; cout《《"\t\t2----插入"《《endl; cout《《"\t\t3----删除"《《endl; cout《《"\t\t4----逆置"《《endl; //cout《《"\t\t5----菜单"《《endl; cout《《"\t\t0----退出"《《endl; cout《《"================================================="《《endl; cout 《《endl《《"input you choice number:";}void main() { DuLinkList L; int c=1; while(c!=0) { switch(c) { case 0: break; case 1: CreateList_DuL(L); break; case 2: ListInsert_DuL(L); break; case 3: ListDelete(L); break; case 4: ListTraverseBack(L); break; default: cout《《"infeasible choice,input again:"《《endl; menu(c); cin》》c; } if(c!=0) { menu(c); cin》》c; } }}

关于C语言双链表的建立,急死了,在线等

可以直接移植,编号为0时结束,不懂Hi我#include 《stdio.h》#include 《string.h》#include 《stdlib.h》typedef struct node{ char num;/* 编号 */ char name;/* 姓名 */ float basicWage;/* 基本工资 */ float dedu;/* 扣款 */ float Wage;/* 应发工资 */ float taxes;/* 税金 */ float realWage;/* 实发工资 */ struct node *prior; struct node *next;}DouNode, *DouList;DouList Creat(){ DouList p1 = NULL; DouList p2 = NULL; DouList head = NULL; head = (DouList)malloc(sizeof(DouNode)); if (NULL == head) { printf("内存分配失败!\n"); exit(1); }//endif head-》next = NULL; p1 = p2 = (DouList)malloc(sizeof(DouNode)); if (NULL == p1) { printf("内存分配失败!\n"); exit(1); }//endif head-》next = p1; p1-》next = NULL; p2-》prior = NULL; printf("编号 :"); while (scanf("%s", p1-》num)) { if (strcmp(p1-》num, "0") == 0) { break; }//endif printf("性名 :"); scanf("%s", p1-》name); printf("基本工资 :"); scanf("%f", &(p1-》basicWage)); printf("扣款 :"); scanf("%f", &(p1-》dedu)); printf("税金 :"); scanf("%f", &(p1-》taxes)); p2 = p1; p1 = (DouList)malloc(sizeof(DouNode)); if (NULL == p1) { printf("内存分配失败!\n"); exit(1); }//endif p2-》next = p1; p1-》prior = p2; printf("编号 :"); }//endwhile p2-》next = NULL; return head;}void Output(DouList head){ DouList p = head-》next; while (NULL != p) { printf("%10s %10s %5.2f %5.2f %5.2f\n", p-》num, p-》name, p-》basicWage, p-》dedu, p-》taxes); p = p-》next; }//endwhile}void OpposOutput(DouList head){ DouList p = head-》next; while (NULL != p-》next) { p = p-》next; }//endwhile while (NULL != p) { printf("%10s %10s %5.2f %5.2f %5.2f\n", p-》num, p-》name, p-》basicWage, p-》dedu, p-》taxes); p = p-》prior; }//endwhile}void main(void){ DouList head = NULL; head = Creat(); printf("正序输出:\n"); Output(head); printf("反序输出:\n"); OpposOutput(head);}如果对你有所帮助,请记得采纳最佳答案,谢谢!

怎样用c语言实现一个双向链表

双向链表的相关操作实现功能:1.创建一个新链表。2.插入节点。3.删除节点。4.选择法排序链表(从小到大)。5.显示当前链表。6.退出程序详细代码见参考资料

怎么使用C语言创建双层链表

双层链表是什么?数组链表还是双链表,如果是后者,使用尾查发的时候多一行代码就OK,就是指向前面一个节点。1 #include《stdio.h》 2 #include《malloc.h》 3 struct nodeone 4 { 5 int data; 6 struct nodeone *next; 7 struct nodeone *down; 8 }; 9 10 struct nodetwo 11 { 12 int data; 13 struct nodetwo *two; 14 }; 15 void main() 16 { 17 int i = 0, j = 0; 18 struct nodeone *head, *p1, *p2, *p3; 19 p2 = head = (struct nodeone*)malloc(sizeof(struct nodeone)); 20 while(i 《 10) 21 { 22 p1 = (struct nodeone*)malloc(sizeof(struct nodeone)); 23 p1-》down = NULL; 24 while(j 《 5) 25 { 26 p3 = (struct nodetwo*)malloc(sizeof(struct nodetwo)); 27 p3-》next = p1-》down; 28 p1-》down = p3; j++; 29 } 30 p2-》next = p1; 31 p2 = p1; i++; //忘记了这两个计数变量,i和j。 32 } 33 p2-》next = NULL; 34 }~ 理论上是对的,Linux下编译通过。外层使用尾插,内层使用头插,主要是为了减少一个变量。我以前没有听过双层链表,不过记住这些名字还是有点用处。

C语言创建双向链表的问题

p-》next-》prior的值确实和p一样,这是说值,但是,它们地址是不一样的赋值的时候,等号左边,要研究的是它的地址。p的地址(不是说p中保存的地址,而是p本身所在的地址)是形参,形参在函数结束以后就没了,你知道的。p-》next的地址则不同,p-》next的地址是p所代表的节点里next成员的地址,同理p-》next-》prior的地址是”p的下一个节点中,保存的前一个节点的成员“的地址,给它赋值和给p赋值,是不同的

关于创建双链表c语言到此分享完毕,希望能帮助到您。

创建双链表c语言(用C语言创建双向链表顺时针数到n删除一个节点,逆时针数到m删除一个节点,输出只有一个节点时节点数值)

本文编辑:admin
Copyright © 2022 All Rights Reserved 威海上格软件有限公司 版权所有

鲁ICP备20007704号

Thanks for visiting my site.