链表元素的删除、插入

需要判断要删除元素在链表中的位置(重要)

//用链表的结构建立一条公交线路的站点信息,从键盘依次输入从起点到终点的各站站名,以单个“#”字符作为输入结束,统计站的数量并输出这些站点

#include<stdio.h>
#include<stdlib.h>  //malloc所需头文件
#include<string.h>  //memset所需头文件
struct station{
    char name[20];
    struct station *nextPtr;
};
struct station *create(struct station *p);//创建站点
void print_station(struct station *p);//打印站点
struct station *del_station(struct station *p);//删除站点
struct station *insert_station(struct station *p);//插入站点(头部插入、中间插入、尾部插入)
int main()
{
    struct station *head;
//    head =null;null空指针必须大写。。。。。。
    head=NULL;
    head=create(head);
    print_station(head);
    head=insert_station(head);
    print_station(head);
    return 0;
}
struct station *create(struct station *p){
    struct station *p1,*p2;
    p1=p2=(struct station *)malloc(sizeof(struct station));
    if(p2){
        printf("请输入站名,以单个“#”作为结束标志:\n");
        scanf("%s",p2->name);
        p2->nextPtr=NULL;
        p=p2;
    }
    while(p2->name[0]!='#'){
        p1=p2;
        p2=(struct station *)malloc(sizeof(struct station));
        scanf("%s",p2->name);
        p2->nextPtr=NULL;
        p1->nextPtr=p2;
    }
    printf("输入完毕!\n");
    return p;
}
void print_station(struct station *p){
    int count=0;
    if(!p){
        printf("站名链表为空!");
    }
    else{
        while(p){
            count++;
            printf("站名:%s\n",p->name);
            p=p->nextPtr;
        }
        printf("总站数:%d\n",count-1);
    }
}
struct station *del_station(struct station *p){
    char a[20]="";
    struct station *p1,*p2;
    p1=p;
    printf("请输入要删除的站名:\n");
    scanf("%s",a);
    if(!p){
        printf("此链表为空表!\n");
        return p;
    }
    if(strcmp(a,p1->name)==0){//需要删除的元素是链表的头部
        printf("删除节点操作完成!\n");
        return p1->nextPtr;
    }else{
        while((strcmp(a,p1->name)!=0)){
            p2=p1;
            p1=p1->nextPtr;
            if(!p1){
                printf("没有此站点!\n");
                return p;
            }
        }
        if(p1->nextPtr!=NULL){//需要删除的元素是链表的中部
            p2->nextPtr=p1->nextPtr;
            free(p1);
            p1=NULL;
            printf("删除节点操作完成!\n");
            return p;
        }
        else{//需要删除的元素是链表的最后一个元素
            p2->nextPtr=NULL;
            free(p1);
            p1=NULL;
            printf("删除节点操作完成!\n");
            return p;
        }
    }
}
struct station *insert_station(struct station *p){
    struct station *p1;
    p1=p;
    char a[20]="";//接受新站名
    char b[20]="";//接受旧站名(新站名之前的一站)
    printf("输入新站名:\n");
    scanf("%s",a);
    printf("输入新站名之前的一站:\n");
    scanf("%s",b);
    if(!p){
        printf("此链表为空表!\n");
        return p;
    }
    if(strcmp(b,p1->name)==0){//在头部添加
        struct station *newhead;
        newhead=(struct station *)malloc(sizeof(struct station));
        if(newhead)
            strcpy(newhead->name,a);
            newhead->nextPtr=p;
        return newhead;
    }else{
        while((strcmp(b,p1->name)!=0)){
            p1=p1->nextPtr;
            if(!p1){
                printf("没有此站点!\n");
                return p;
            }
        }
        if(p1->nextPtr!=NULL){//需要插入的元素是链表的中部
            struct station *newmiddle;
            newmiddle=(struct station *)malloc(sizeof(struct station));
            if(newmiddle){
                strcpy(newmiddle->name,a);
                newmiddle->nextPtr=p1->nextPtr;
                p1->nextPtr=newmiddle;
            }
            return p;
        }
        else{//需要插入的元素是链表的最后一个元素
            struct station *newend;
            newend=(struct station *)malloc(sizeof(struct station));
            if(newend){
                strcpy(newend->name,a);
                newend->nextPtr=NULL;
                p1->nextPtr=newend;
            }
            return p;
        }
    }


}

 

posted @ 2018-08-22 15:17  阳刚猛男  阅读(257)  评论(0编辑  收藏  举报