C语言-指针练习-16

题目:

创建双链表

创建一个双链表,并将这个链表中数据输出到窗体上,输入要查找的学生姓名,将查找的姓名从链表中删除,并显示删除后的链表

源代码:

方法1:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
struct DATA
{
    char ch[20];
};
struct NODE
{
    struct DATA data;
    struct NODE * next;
    struct NODE * previous;
};
struct HEAD
{
    struct NODE * first;
    struct NODE * tail;
};
void initialization(struct HEAD * head);
void create_node(struct HEAD * head,char * ch);
void node_print(struct HEAD * head);
void node_del(struct HEAD * head,char * ch);
struct NODE * node_search(struct HEAD * head,char * ch);
int main()
{
    struct HEAD * head = (struct HEAD *) malloc (sizeof(struct HEAD));
    int student_num=1;
    char ch[20],ch_sta;
    initialization(head);
 
    while(1)
    {
        printf("请输入第 %d 个学生的名字: ",student_num++); 
        if((ch_sta = getchar()) == 10)  break;
        else ungetc(ch_sta,stdin);
        scanf("%s",ch);
        getchar();

        create_node(head,ch);
    }
    printf("现在这个双链表的元素有: ");
    node_print(head);
    printf("\n请输入你想要删除的名字: ");
    scanf("%s",ch);
    node_del(head,ch);
    printf("\n现在这个双链表的元素有: ");
    node_print(head);    
    return 0;
}
void initialization(struct HEAD * head)
{
    head->first = (struct NODE *) malloc(sizeof(struct NODE));
    head->first->data.ch[0] = '\0';
    head->first->next = NULL;
    head->first->previous = NULL;
}
void create_node(struct HEAD * head,char * ch)
{
    struct NODE * tmp_pointer = (struct NODE *) malloc (sizeof(struct NODE));
    strcpy(tmp_pointer->data.ch,ch);
    tmp_pointer->next = NULL;
    tmp_pointer->previous = NULL;
    if(head->first->next == NULL)
    {
        head->first->next = head->tail = tmp_pointer;
        tmp_pointer->previous = head->first;
    }
    else
    {
        head->tail->next = tmp_pointer;
        tmp_pointer->previous = head->tail;
        head->tail = tmp_pointer;
    }
    head->tail->next = head->first;
    head->first->previous = head->tail; 
}
void node_print(struct HEAD * head)
{
    struct NODE * tmp_head = head->first->next;
    while(tmp_head != head->first)
    {
        printf("%s ",tmp_head->data.ch);
        tmp_head = tmp_head->next;
    }
}   
void node_del(struct HEAD * head,char * ch)
{
    struct NODE * pointer = node_search(head,ch);
    if(pointer == NULL)
    {
        printf("没有这个名字\n");
        exit(0);
    }
    else
    {
        pointer->next->previous = pointer->previous;
        pointer->previous->next = pointer->next;
        free(pointer);
    }
}
struct NODE * node_search(struct HEAD * head,char * ch)
{
    struct NODE * node = head->first->next;
    while(node != head->first)
    {
        if(strcmp(node->data.ch,ch) == 0) return node;
        else    node = node->next;
    }
    return NULL;
}

方法2:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct DATA
{
    char name[20];
};
struct LNODE
{
    struct DATA data;
    struct LNODE * previous;
    struct LNODE * next;
};
struct PHEAD
{
    struct LNODE * head;
    struct LNODE * tail;
    int node_max;
};
void initialization_head(struct PHEAD * head_pointer);
void node_create(struct PHEAD * head_pointer,char * name);
struct LNODE * node_search(struct PHEAD * head_pointer,char * name);
void node_delete(struct PHEAD * head_pointer,char * name);
void print(struct PHEAD * head_pointer);
int main()
{
    struct PHEAD * head_pointer = (struct PHEAD *) malloc (sizeof(struct PHEAD));
    char num_data[20];
    initialization_head(head_pointer);
    printf("请输入学生的姓名(输入-1退出): \n");
    while(scanf("%s",num_data) && strcmp(num_data,"-1") != 0)
    {
        node_create(head_pointer,num_data);
    }
    printf("现在这个双链表是:\n");
    print(head_pointer);
    printf("请输入你要删除的姓名: ");
    scanf("%s",num_data);
    node_delete(head_pointer,num_data);
    printf("现在这个双链表是:\n");
    print(head_pointer);
    return 0;
}
void initialization_head(struct PHEAD * head_pointer)
{
    if(head_pointer->head != NULL) head_pointer->head = NULL;
    head_pointer->node_max = 0;
}
void node_create(struct PHEAD * head_pointer,char * name)
{
    struct LNODE * p = (struct LNODE *) malloc (sizeof(struct LNODE));
    strcpy(p->data.name,name);
    p->next = NULL;
    if(head_pointer->head == NULL)
    {
        head_pointer->head = head_pointer->tail = p;
        head_pointer->head->previous = head_pointer->tail;
        head_pointer->tail->next = head_pointer->head;
        head_pointer->node_max++;
    }
    else
    {
        head_pointer->tail->next = p;
        p->previous = head_pointer->tail;
        head_pointer->tail = p;
        head_pointer->tail->next = head_pointer->head;
        head_pointer->head->previous = head_pointer->tail;
        head_pointer->node_max++;
    }
}
struct LNODE * node_search(struct PHEAD * head_pointer,char * name)
{
    struct PHEAD tmp_head = *head_pointer;
    for(int i = 1;i <= tmp_head.node_max;i++)
    {
        if(strcmp(tmp_head.head->data.name,name) == 0) return tmp_head.head;
        tmp_head.head = tmp_head.head->next;
    }   
    return NULL;    
}
void node_delete(struct PHEAD * head_pointer,char * name)
{
    struct LNODE * p = node_search(head_pointer,name);
    if(p == NULL)
    {
        printf("查找不到相对应的名字\n");
        exit(1);
    }
    printf("正在删除名字 %s\n",p->data.name);
    p->next->previous = p->previous;
    p->previous->next = p->next;
    if(head_pointer->head == p) head_pointer->head = head_pointer->head->next;
    free(p);
    head_pointer->node_max--;
}
void print(struct PHEAD * head_pointer)
{
    struct PHEAD tmp_head = *head_pointer;
    for(int i = 1;i <= tmp_head.node_max;i++)
    {
        printf("%s ",tmp_head.head->data);
        tmp_head.head = tmp_head.head->next;
    }
    printf("\n");
}

演示效果:


如果朋友你感觉文章的内容对你有帮助,可以点赞关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是小天才哦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值