双项链表的创建

                                                         双向链表的创建

创建一个双项链表,先从头到尾打印,然后从尾到头打印。

程序如下:

#include<stdio.h>
#include<stdlib.h>
struct NODE{

 struct NODE  *fwd;  //定义前驱指针
 struct NODE *bwd;  //定义后续指针
 int  value;
};

int main(){
 int  num = -1;

 struct NODE *head,*tail,*temp,*ptr;
        head = tail = temp = ptr = NULL;
 
 printf("please input the number of the doulb_list ends with 0:/n");
 scanf("%d ",&num);
 while(num!=0){
  

    temp = (struct NODE *)malloc(sizeof(struct NODE)); //申请空间
 if(head == NULL){
  temp->value = num;                //如果为头结点,那么head和tail的fwd和bwd
  head = tail = temp;               //都指向头结点
  head->fwd = head->bwd = head;
  tail->bwd = tail->fwd = head;
 }
    else{
  temp->value = num;     
  tail->bwd = temp;    //如果不是头结点,那么tail的后续指向temp
  temp->fwd = tail;    //temp的前驱指向tail
  temp->bwd = head;    //temp的后续指向头结点
  tail = temp;         //最后tail指向temp,也就是现在的尾部
  head->fwd = tail;    //最后head的前驱指向tail
 
 }
        scanf("%d ",&num);
}

    printf("the members in this list is:/n");
 for(ptr=head;ptr!=tail;ptr=ptr->bwd)   //从head到taill打印
  printf("%d ",ptr->value);
 printf("%d/n",tail->value);

 printf("the member in this list is:/n");
 for(ptr=tail;ptr!=head;ptr=ptr->fwd)    //从tail到head打印
  printf("%d ",ptr->value);
 printf("%d/n",head->value);

 return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单项链表删除节点的基本步骤是: 1. 找到待删除节点的前一个节点。 2. 将待删除节点从链表中断开。 3. 释放待删除节点的内存空间。 下面是使用指针来处理单项链表删除操作的示例代码: ```c #include <stdio.h> #include <stdlib.h> /* 定义单项链表节点 */ typedef struct node { int data; // 节点数据 struct node *next; // 指向下一个节点的指针 } Node; /* 删除节点 */ void delete_node(Node **head, int value) { Node *prev = NULL; // 待删除节点的前一个节点 Node *curr = *head; // 当前节点 // 遍历链表,找到待删除节点的前一个节点 while (curr != NULL && curr->data != value) { prev = curr; curr = curr->next; } // 如果链表中没有要删除的节点,直接返回 if (curr == NULL) { return; } // 如果待删除节点是头节点,更新头指针 if (prev == NULL) { *head = curr->next; } else { prev->next = curr->next; } // 释放待删除节点的内存空间 free(curr); } /* 打印链表 */ void print_list(Node *head) { while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } /* 测试 */ int main() { // 创建链表 1 -> 2 -> 3 -> 4 -> 5 Node *head = (Node*)malloc(sizeof(Node)); head->data = 1; head->next = (Node*)malloc(sizeof(Node)); head->next->data = 2; head->next->next = (Node*)malloc(sizeof(Node)); head->next->next->data = 3; head->next->next->next = (Node*)malloc(sizeof(Node)); head->next->next->next->data = 4; head->next->next->next->next = (Node*)malloc(sizeof(Node)); head->next->next->next->next->data = 5; head->next->next->next->next->next = NULL; // 删除节点 3 delete_node(&head, 3); // 打印链表 print_list(head); return 0; } ``` 在上面的代码中,`delete_node` 函数使用双重指针 `head` 来表示头指针的地址,这样可以在删除头节点时更新头指针。函数中通过遍历链表找到待删除节点的前一个节点,并将待删除节点从链表中断开。最后,函数释放待删除节点的内存空间。函数 `print_list` 用于打印链表。在 `main` 函数中,我们创建了一个链表 1 -> 2 -> 3 -> 4 -> 5,并删除了节点 3,然后打印剩余的链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值