{
struct Node *next
int NO
Node(int no):NO(no),next(NULL){}
}
class Deque
{
private:
Node *Head
Node *Tail
int Number
public:
Deque():Head(NULL),Tail(NULL),Number(0){}
~Deque(){
if(Number ! =0){
for(int i=0i<Number++i)Pop()
}
}
void Push()
{
if(!Number)
{
Head=new Node(++Number)
Tail=Head
}
else
{
Tail->next=new Node(++Number)
Tail=Tail->next
}
}
void Pop()
{
if(!Number)
{
cout<<"Error"<<endl
return
}
else
{
Node *p=Head
--Number
Head=Head->next
delete p
}
}
int GetNumber()const{return Number}
}
/*按照要求和提示我写了patient *Creat()、patient *Delete()、int InLine(patient *head)、patient *OutLine(patient *head)、int Search(patient *head)函数,因为不清楚你对程序控制的要求,我简单地写了个控制菜单int menu()函数,用来简单控制程序。你可以根据需要修改int menu()和int main(),在需要的地方声明链表头指针patient *head此程序在DEV-CPP测试通过*/
#include<stdio.h>
#include<string.h>
#define LEN sizeof(patient)
int IsExist
struct date
{
char name[20]
char sex[7]
int age
struct date *next
}
typedef struct date patient
patient *Creat() //创建链表
{
patient *head=(patient *)malloc(LEN)
if(head==NULL) return NULL //创建失败,返回0
printf("Input name,sex(\"male\" or \"female\"),age:")
scanf("%s %s %d",&head->name,&head->sex,&head->age)
head->next=NULL
IsExist=1//标记链表存在
return(head)
}
patient *Delete()
{
return NULL
}
int InLine(patient *head)//排队
{
patient *p1,*p2
p1=head
while(p1->next!=NULL) p1=p1->next//找链尾
p2=(patient *)malloc(LEN)
if(p2==NULL) return 0 //新增纳激元素失败,返回0
printf("Input name,sex(\"male\" or \"female\"),age:")
scanf("%s %s %d",&p2->name,&p2->sex,&p2->age)
p1->next=p2
p2->next=NULL
return 1
}
patient *OutLine(patient *head) //就诊病人离宴洞开
{
if(!IsExist) return 0 //链表不存在,返回0
patient *p1,*p2
p1=headp2=head->next
if(head->next==NULL) //链表只有一个元素,删除链表
{
IsExist=0
return(Delete())
}
if(p2!=NULL) return(p2)
}
int Search(patient *head) //查询自己当前位置
{
patient *p
p=head
int i=0
char sea_name[20]
if(!IsExist) return(i) //链表不存在,返回0
i++
printf("Input your name:")
scanf("%s",sea_name) //输入姓名并查找
while(strcmp(p->name,sea_name))
{
i++
p=p->next
}
return(i)//返回当前位置
}
int menu() //菜单函数
{
int quit=0,temp //quit变量用于控制退出程序
int select
static patient *head
printf("1.Line up\n2.Search my state\n3.Delete the first element\n4.Exit\nYou want to:")
scanf("%d",&select)
switch(select)
{
case 1:if(!(IsExist?InLine(head):(head=Creat()))) exit(0)return 0
//排队,如果链表不存在则创晌茄枯建,存在则增加长度, *** 作失败则终止程序
case 2:if(temp=Search(head)) printf("There are %d persons.\n",temp)return 0
case 3:head=OutLine(head)return 0
case 4:return 1
}
}
int main()
{
IsExist=0
int quit
do
{
quit=menu()
}while(!quit)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)