//輸入學生信息後,凡是刪除第一個學生的信息程序都會崩潰??
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define LEN sizeof(struct Student)
struct Student //完全可以把結構當成類,只是訪問權限不一樣
{
long num;//學號
char name[20];//姓名
char sex[10];//性別
int age;//年齡
char phone[11];//電話
struct Student *next;
struct Student *pre;//雙向動態不熟悉?
};
struct Student *curr,*head=NULL,*last;
void creat(void)
{
while(1)
{
curr=(struct Student *)new Student();
cout<<"請輸入學生學號"<<endl;
cin>>curr->num;
cout<<"請輸入學生姓名"<<endl;
cin>>curr->name
cout<<"請輸入學生性別"<<endl;
cin>>curr->sex;
cout<<"請輸入學生年齡"<<endl;
cin>>curr->age;
cout<<"請輸入學生電話"<<endl;
cin>>curr->phone;
if(head==NULL){
head=curr;
last=curr;
curr->pre=NULL;
curr->next=NULL;
}else{
last->next=curr;
curr->next=NULL;
curr->pre=last;
last=curr;
}
cout<<"請選擇操作,1表示繼續錄入,2表示結束"<<endl;
int b;
cin>>b;
if(b==1)continue;
else break;
}
}
void print(int num)//鏈表的print函數,以結構體指針為參數
{
curr=head;
int a=0;
while(curr)
{
if(curr->num==num)
{
cout<<"學號"<<curr->num<<endl;
cout<<"姓名"<<curr->name<<endl;
cout<<"性別"<<curr->sex<<endl;
cout<<"年齡"<<curr->age<<endl;
cout<<"電話"<<curr->phone<<endl;
a=1;
break;
}
curr=curr->next;
}
if(a==0)
{
cout<<"查無此人"<<endl;
}
}
void Delete(int num)
{
curr=head;
while(curr)
{
if(curr->num==num)
{
(curr->pre)->next = curr->next;
free(curr);
}
else
{
curr=curr->next;
}
}
cout<<"已經刪除學生信息"<<endl;
}
int main()
{
// head=creat();//返回第一個結點的地址
while(1)
{
cout<<"請選擇你的操作"<<endl;
cout<<"1:學生信息錄入"<<endl;
cout<<"2:學生信息查詢"<<endl;
cout<<"3:請輸入刪除學生的學號"<<endl;
cout<<"4:退出"<<endl;
int choice=0;
cin>>choice;//輸入選擇
if(choice==1)
{
cout<<"請輸入學生的信息"<<endl;
creat();//返回第一個結點的地址
}
if(choice==2)
{
cout<<"請輸入你查詢的學生的學號"<<endl;
int a ;
cin>>a ;
print(a);
}
if(choice==3)
{
cout<<"請輸入你想要刪除學生的學號"<<endl;
int a;
cin>>a;
Delete(a) ;
}
if(choice==4)
break;
}
return 0;
};
//這是我最新修改後的代碼
//空出頭部和尾部,謝謝大家的指點啦!
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define LEN sizeof(struct Student)
struct Student //完全可以把結構當成類,只是訪問權限不一樣
{
long num;//學號
char name[20];//姓名
char sex[10];//性別
int age;//年齡
char phone[11];//電話
struct Student *next;
struct Student *pre;//雙向動態不熟悉?
};
struct Student *curr,*head=NULL,*last;
void creat(void)
{
while(1)
{
curr=(struct Student *)new Student();
cout<<"請輸入學生學號"<<endl;
cin>>curr->num;
cout<<"請輸入學生姓名"<<endl;
cin>>curr->name;
cout<<"請輸入學生性別"<<endl;
cin>>curr->sex;
cout<<"請輸入學生年齡"<<endl;
cin>>curr->age;
cout<<"請輸入學生電話"<<endl;
cin>>curr->phone;
if(head==NULL){
head=(struct Student*)new Student();//空出頭部
head->next=curr;
curr->pre=head;
last=curr;
}else{
last->next=curr;
curr->next=NULL;
curr->pre=last;
last=curr;
}
cout<<"請選擇操作,1表示繼續錄入,2表示結束"<<endl;
int b;
cin>>b;
if(b==1)continue;
else if(b==2)
{
last= (struct Student*)new Student();//空出尾部
last->pre=curr;
break;
}
}
}
void print(int num)//鏈表的print函數,以結構體指針為參數
{
curr=head;
int a=0;
while(curr)
{
if(curr->num==num)
{
cout<<"學號"<<curr->num<<endl;
cout<<"姓名"<<curr->name<<endl;
cout<<"性別"<<curr->sex<<endl;
cout<<"年齡"<<curr->age<<endl;
cout<<"電話"<<curr->phone<<endl;
a=1;
break;
}
curr=curr->next;
}
if(a==0)
{
cout<<"查無此人"<<endl;
}
}
void Delete(int num)
{
curr=head;
while(curr)
{
if(curr->num==num)
{
(curr->pre)->next = curr->next;
free(curr);
}
else
{
curr=curr->next;
}
}
cout<<"已經刪除學生信息"<<endl;
}
int main()
{
// head=creat();//返回第一個結點的地址
while(1)
{
cout<<"請選擇你的操作"<<endl;
cout<<"1:學生信息錄入"<<endl;
cout<<"2:學生信息查詢"<<endl;
cout<<"3:請輸入刪除學生的學號"<<endl;
cout<<"4:退出"<<endl;
int choice=0;
cin>>choice;//輸入選擇
if(choice==1)
{
cout<<"請輸入學生的信息"<<endl;
creat();//返回第一個結點的地址
}
if(choice==2)
{
cout<<"請輸入你查詢的學生的學號"<<endl;
int a ;
cin>>a ;
print(a);
}
if(choice==3)
{
cout<<"請輸入你想要刪除學生的學號"<<endl;
int a;
cin>>a;
Delete(a) ;
}
if(choice==4)
break;
}
return 0;
};