c语言循环链表打印出来,C语言双向循环链表的生成,删除和打印

#include typedef struct dnode

{

int data;

struct dnode *prior;

struct dnode *next;

}dnode;

dnode *add(dnode *dhead);

dnode *del(dnode *dhead);

void printdnode(dnode *dhead);

int main()

{

int sel=0;

dnode *dhead=NULL;

while(1)

{

printf("\t1:添加\n\t2:删除\n\t3:查看\n\t4:退出\n");

scanf("%d",&sel);

switch(sel)

{

case 1:

dhead=add(dhead);

printdnode(dhead);

break;

case 2:

dhead=del(dhead);

printdnode(dhead);

break;

case 3:

printdnode(dhead);

break;

case 4:

exit(0);

default:

printf("reinput 1-4\n");

break;

}

}

return 0;

}

dnode *add(dnode *dhead)

{

int x=0;

dnode *s=NULL;

dnode *p=NULL;

printf("请输入节点数据:\n");

scanf("%d",&x);

s=(dnode *)malloc(sizeof(dnode));

s->data=x;

if(NULL==dhead)

{

dhead=s;

s->next=s;

s->prior=s;

}

else

{

p=dhead;

//先找到添加的位置

while(1)

{

if(s->data>p->data)

{

p=p->next;

if(p==dhead)

{

break;

}

}

else

{

break;

}

}

//插入到p节点的前面

p->prior->next=s;

s->prior=p->prior;

s->next=p;

p->prior=s;

}

return dhead;

}

dnode *del(dnode *dhead)

{

int x=0;

dnode *p=NULL;

if(NULL==dhead)

{

printf("dlist empty\n");

return NULL;

}

printf("请输入删除的数据:\n");

scanf("%d",&x);

p=dhead;

//找到需要删除数据的地址

while(1)

{

if(p->data==x)

{

break;

}

p=p->next;

if(p==dhead)

{

printf("no this data in dlist\n");

return dhead;

}

}

if(dhead==p)

{

dhead=dhead->next;

}

p->prior->next=p->next;

p->next->prior=p->prior;

if((dhead->next==dhead)&&(dhead->prior=dhead)&&(dhead->data==x))

{

free(dhead);

dhead=NULL;

p=NULL;

}

else

{

free(p);

p=NULL;

}

return dhead;

}

void printdnode(dnode *dhead)

{

if(NULL==dhead)

{

printf("dlist empty\n");

return;

}

dnode *p=NULL;

p=dhead;

while(1)

{

printf("%d ",p->data);

p=p->next;

if(p==dhead)

{

break;

}

}

printf("\n");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环链表是一种特殊的链表,它的尾节点指向头节点,形成一个环。按值删除节点需要遍历整个链表,找到要删除的节点,然后将其从链表删除。具体实现步骤如下: 1. 定义一个指向当前节点的指针p和一个指向上一个节点的指针pre,初始时p指向头节点,pre指向尾节点。 2. 遍历链表,如果当前节点的值等于要删除的值,则执行删除操作。 3. 删除操作包括将当前节点从链表删除,并将上一个节点的next指针指向当前节点的下一个节点。 4. 如果遍历完整个链表都没有找到要删除的节点,则输出提示信息。 以下是C语言循环链表按值删除节点的代码实现: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct node { int data; struct node *next; } Node; // 创建循环链表 Node *createList(int n) { Node *head = NULL, *p = NULL, *prev = NULL; int i, value; for (i = 1; i <= n; i++) { printf("请输入第%d个节点的值:", i); scanf("%d", &value); p = (Node *)malloc(sizeof(Node)); p->data = value; if (head == NULL) { head = p; } else { prev->next = p; } prev = p; } prev->next = head; // 将尾节点指向头节点,形成循环链表 return head; } // 按值删除节点 Node *deleteNode(Node *head, int value) { Node *p = head, *pre = head; while (p->data != value) { pre = p; p = p->next; if (p == head) { // 遍历完整个链表都没有找到要删除的节点 printf("链表中没有该节点!\n"); return head; } } if (p == head) { // 要删除的节点是头节点 head = head->next; pre->next = head; } else { pre->next = p->next; } free(p); // 释放要删除的节点的内存空间 printf("删除成功!\n"); return head; } // 输出链表 void printList(Node *head) { Node *p = head; printf("链表的值为:"); do { printf("%d ", p->data); p = p->next; } while (p != head); printf("\n"); } int main() { int n, value; printf("请输入链表的长度:"); scanf("%d", &n); Node *head = createList(n); printList(head); printf("请输入要删除的节点的值:"); scanf("%d", &value); head = deleteNode(head, value); printList(head); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值