根据一个链表中的元素删除另一个链表中重复的元素(c语言数据结构)

#include<stdio.h>
#include"slnklist.h"
int deletex (linklist head,int x){
    linklist p,q;
    q=head;
    p=head->next;
    
    while(p!=NULL&&p->data!=x){
        q=p;
        p=p->next;
    }
if(p==NULL){
return 0;
} else{
    q->next=p->next;
    linklist t=p;
    free(t);
return 1;
    }

}
void delelink(linklist L1,linklist L2){
    linklist p;
    p=L2->next;
    while(p){
        while(deletex(L1,p->data));
        p=p->next;
    }

int main(){
    linklist p,q;
    p=creatbyqueue();
    q=creatbyqueue();
delelink(p,q);
print(p);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表插入元素C语言实现,可以按照以下步骤进行: 1. 创建一个新节点,用于存储要插入的元素。 2. 遍历链表,找到要插入节点的位置。可以使用一个指针指向当前节点,然后依次往下遍历,直到找到插入位置。 3. 将新节点的指针指向当前节点的下一个节点,同时将当前节点的指针指向新节点。 下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 链表节点结构体 struct node { int data; struct node *next; }; // 在链表插入元素 void insert_node(struct node **head, int data, int index) { // 创建新节点 struct node *new_node = (struct node *)malloc(sizeof(struct node)); new_node->data = data; new_node->next = NULL; // 如果链表为空,直接将新节点作为头节点 if (*head == NULL) { *head = new_node; return; } // 如果插入位置是头节点之前,直接将新节点作为头节点 if (index <= 0) { new_node->next = *head; *head = new_node; return; } // 遍历链表,找到插入位置 struct node *current_node = *head; int i = 0; while (i < index - 1 && current_node->next != NULL) { current_node = current_node->next; i++; } // 如果插入位置超出了链表范围,直接将新节点作为尾节点 if (i < index - 1) { current_node->next = new_node; return; } // 插入新节点 new_node->next = current_node->next; current_node->next = new_node; } // 打印链表 void print_list(struct node *head) { printf("List: "); while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } int main() { // 创建链表 struct node *head = NULL; insert_node(&head, 1, 0); insert_node(&head, 2, 1); insert_node(&head, 3, 2); print_list(head); // 在链表插入元素 insert_node(&head, 4, 1); print_list(head); return 0; } ``` 输出结果为: ``` List: 1 2 3 List: 1 4 2 3 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值