有两个链表a和b,设结点中包含学号和姓名,从a链表中删除和b链表中相同学号的结点

本文介绍了一个使用C语言实现的学生信息链表操作案例,包括创建链表、删除重复学号的学生信息等功能。通过具体代码展示了如何进行链表的基本操作。
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
#define NULL 0
int n=0;
struct student {
int num;
char name[20];
struct student *next;
};

struct student *creat(void)  //创建学生结构体链表
{
    struct student *head,*p1,*p2;
    p1=p2=(struct student *)malloc(LEN);
    scanf("%d %s",&p1->num,p1->name);
    head=NULL;

    while(p1->num!=0){
            n++;
       if(n==1) head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(LEN);
        scanf("%d %s",&p1->num,p1->name);
        }
    p2->next=0;
    n=0;
    return head;
}
struct student *deletesame(struct student *a,struct student *b){       //删除从a中删除b中相同学号的学员
struct student *head=a,*p1=a,*p2=b,*p3=a;

    int k=0,s=0;

    while(p1!=0){
        while(p2!=0){
            if(p1->num==p2->num)
            {
                if(k==0) {head=p1->next;                                //如果a链表头发现相同需要删除。
                    s++;
                    }

                    else {
                            p3->next=p1->next;
                            s++;
                        }
            }
            p2=p2->next;
                  }

        if(s>0){                                      //发生删除操作后的处理
            s=0;
            p2=b;
            p1=p1->next;

        }else{                                          // 未发生删除操作后的处理
            p2=b;
            k++;
            p3=p1;
            p1=p1->next;}
}
return head;
}

void print( struct student *p){
    while(p!=0)
      { printf("%d %s\n",p->num,p->name);
        p=p->next;
}
}
int main()
{
   struct student *a=creat();
    struct student *b=creat();
    struct student *head=deletesame(a,b);
    print(head);

    return 0;
}

以下是使用C语言代码实现目要求的示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义链表节点结构体 typedef struct Node { char id[10]; char name[20]; struct Node* next; } Node; // 创建新节点 Node* createNode(char id[], char name[]) { Node* node = (Node*)malloc(sizeof(Node)); strcpy(node->id, id); strcpy(node->name, name); node->next = NULL; return node; } // 添加节点到链表末尾 void addNode(Node** head, Node* node) { if (*head == NULL) { *head = node; } else { Node* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = node; } } // 打印链表 void printList(Node* head) { while (head != NULL) { printf("%s %s\n", head->id, head->name); head = head->next; } } // 从链表删除指定学号的节点 void deleteNode(Node** head, char id[]) { Node* temp = *head; Node* prev = NULL; while (temp != NULL && strcmp(temp->id, id) != 0) { prev = temp; temp = temp->next; } if (temp == NULL) { return; } if (prev == NULL) { *head = temp->next; } else { prev->next = temp->next; } free(temp); } // 从a链表删除b链表中有相同学号的节点 void deleteSameNodes(Node** a, Node* b) { while (b != NULL) { deleteNode(a, b->id); b = b->next; } } int main() { // 创建a链表 Node* a = NULL; addNode(&a, createNode("1", "Alice")); addNode(&a, createNode("2", "Bob")); addNode(&a, createNode("3", "Charlie")); addNode(&a, createNode("4", "David")); addNode(&a, createNode("5", "Eva")); printf("a链表:\n"); printList(a); // 创建b链表 Node* b = NULL; addNode(&b, createNode("2", "Bob")); addNode(&b, createNode("4", "David")); addNode(&b, createNode("6", "Frank")); printf("b链表:\n"); printList(b); // 从a链表删除b链表中有相同学号的节点 deleteSameNodes(&a, b); printf("删除b链表中有相同学号的节点后的a链表:\n"); printList(a); return 0; } ``` 运行结果: ``` a链表: 1 Alice 2 Bob 3 Charlie 4 David 5 Eva b链表: 2 Bob 4 David 6 Frank 删除b链表中有相同学号的节点后的a链表: 1 Alice 3 Charlie 5 Eva ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值