m0_63999697 2022-03-15 00:00 采纳率: 93.5%
浏览 20
已结题

从头开始删除单链表节点怎么做?我的delete函数出错了但不知道怎么改

输入样列:
OUT
IN 1000001 Ordinary
IN 2000003 VIP
IN 2000009 VIP
OUT
OUT
IN 2000008 VIP
LIST
QUIT
输出样列:
FAILED:
IN:1 1000001 Ordinary 0
IN:2 2000003 VIP 1
IN:3 2000009 VIP 2
OUT:1 1000001 Ordinary
OUT:2 2000003 VIP
IN:4 2000008 VIP 1
LIST:
3 200000 9VIP
4 2000008 VIP
GOOD BYE!


#include<bits/stdc++.h>
using namespace std;
int n=0;
typedef struct Student{
    int k;
    int wait;
    char id[20];
    char type[20];
    struct Student* next;
}Student;
Student* createList()
{
    Student *L;
    L=(Student *)malloc(sizeof(Student));
    L->next=NULL;
    return L;
}
void inputSingle(Student *s){
    cin>>s->id>>s->type;
    n++;
    s->k=n;
    s->wait=n-1;
}
void outputSingle(Student *s){
    cout<<s->k<<" "<<s->id<<" "<<s->type<<" "<<s->wait<<"\n";
}
void insertTail(Student *L,Student *s)
{
    Student *pre,*p;
    pre=L;
    p=L->next;
    while(p!=NULL){
        pre=p;
        p=p->next;
    }
    s->next=pre->next;
    pre->next=s;
}
void outputList(Student *L){
    Student *p;
    p=L->next;
    while(p!=NULL){
        outputSingle(p);
        p=p->next;
    }
}
int Delete(Student *L)
{
    Student *pre,*p;
    pre=L;
    p=L->next;
    while(p!=NULL){
        pre->next=p->next;
        free(p);
    }
    if(p==NULL){
        puts("FAILED");
        return 0;
    }
    return 1;
}
int main()
{
    Student *L,*s;
    L=createList();
    string order;
    while(1){
        cin>>order;
        if(order=="IN"){
            printf("IN:");
            s=(Student *)malloc(sizeof(Student));
            inputSingle(s);
            outputSingle(s);
            insertTail(L,s);
        }
        if(order=="LIST"){
            printf("LIST:\n");
            outputList(L);
        }
        if(order=="OUT"){
            Delete(L);
        }
        else if(order=="QUIT"){
            puts("GOOD BYE!");
            break;
        }
    }
    return 0;
}
 
 
 
  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-03-15 00:52
    关注

    修改处见注释,供参考:

    //#include<bits/stdc++.h>
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    int n=0;
    typedef struct Student{
        int k;
        int wait;
        char id[20];
        char type[20];
        struct Student* next;
    }Student;
    Student* createList()
    {
        Student *L;
        L=(Student *)malloc(sizeof(Student));
        L->next=NULL;
        return L;
    }
    void inputSingle(Student *s){
        cin>>s->id>>s->type;
        n++;
        s->k=n;
        s->wait=n-1;
    }
    void outputSingle(Student *s){
        cout<<s->k<<" "<<s->id<<" "<<s->type<<" "<<s->wait<<"\n";
    }
    void insertTail(Student *L,Student *s)
    {
        Student *pre,*p;
        pre=L;
        p=L->next;
        while(p!=NULL){
            pre=p;
            p=p->next;
        }
        s->next=pre->next;
        pre->next=s;
    }
    void outputList(Student *L){
        Student *p;
        p=L->next;
        while(p!=NULL){
            outputSingle(p);
            p=p->next;
        }
    }
    Student* Delete(Student *L) //修改
    {
        Student *pre,*p;
        pre=L;
        p=L->next;
        if(pre == NULL || p == NULL)//修改
           return NULL;             //修改
        pre->next = p->next;
        return p;                   //修改
    }
    int main()
    {
        Student *L,*s;
        L=createList();
        string order;
        while(1){
            cin>>order;
            if(order=="IN"){
                printf("IN:");
                s=(Student *)malloc(sizeof(Student));
                inputSingle(s);
                outputSingle(s);
                insertTail(L,s);
            }
            if(order=="LIST"){
                printf("LIST:\n");
                outputList(L);
            }
            if(order=="OUT"){
                s = Delete(L);    //修改
                if(s){            //修改
                   printf("OUT:");
                   outputSingle(s);//修改
                   free(s);
                }
                else{             //修改
                   puts("FAILED:");
                }
            }
            else if(order=="QUIT"){
                puts("GOOD BYE!");
                break;
            }
        }
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月23日
  • 已采纳回答 3月15日
  • 创建了问题 3月15日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改