编程论坛's Archiver

xinge21 发表于 2008-5-7 12:42

老大帮下我

请高手指点下
小弟刚学C++昨天写了一点链表方面的东西, 可运行时老是出现内存错误,但也可运行
哪位朋友给指点下,谢谢了,下面上传的有工作文件
代码如下:
#include<iostream.h>
#include<string.h>
struct student{
    char name[20];
    unsigned long no;
    student *next;};

    void main ()
    {cout<<"                 学生学号登记系统\n       ××××********************××××\n";
    cout<<"依次输入学生姓名、学号,以姓名“over”作为结束输入标志\n请输入:\n";
        student  *head,*n;
        char name[20] ;
        unsigned long no;
        for(;;)
        {cin>>name;
        if(!strcmp(name,"over")) break;
        cin>>no;
            n=new student;
            strcpy(n->name,name);
            n->no=no;
        if(head==NULL)
        {head=n;
        n->next=NULL;}
        else {
         n->next=head;
            head=n;}
        }
   
        cout<<"*******************************\n所有学生信息如下:\n";

        for(student *p=head;p!=NULL;p=p->next)
            cout<<p->name<<'\t'<<p->no<<endl;
        student *q;
    while(head)
    {
        q=head;head=head->next;delete q;

        }
            
        
   
    }

PcrazyC 发表于 2008-5-7 18:15

//将HEAD初始化为NULL

#include<iostream.h>
#include<string.h>
struct student
{
    char name[20];
    unsigned long no;
    student *next;
};

void main ()
{
        cout<<"                 学生学号登记系统\n       ××××********************××××\n";
    cout<<"依次输入学生姓名、学号,以姓名“over”作为结束输入标志\n请输入:\n";
        student  *head=NULL,*n;
        char name[20] ;
        unsigned long no;
        for(;;)
        {
                cin>>name;
                if(!strcmp(name,"over")) break;
                cin>>no;
                n=new student;
                strcpy(n->name,name);
                n->no=no;
                if(head==NULL)
                {
                        head=n;
                        n->next=NULL;
                }
                else
                {
                        n->next=head;
                        head=n;
                }
        }
   
        cout<<"*******************************\n所有学生信息如下:\n";
       
        for(student *p=head;p!=NULL;p=p->next)
                cout<<p->name<<'\t'<<p->no<<endl;
        student *q;
    while(head)
    {
        q=head;head=head->next;delete q;
               
        }
       
       
   
}

xinge21 发表于 2008-5-8 10:00

太感谢了,

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.