?? stacklist.cpp
字號:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
typedef struct _Stu{
char Name[10];
int Age;
char Adree[30];
}Student;
typedef struct Stack{
Student Date;
struct Stack *next;
}XjNode,* StackList;
void InitStack(StackList &L);
void Push(StackList &L,Student &e);
void Input(Student *a);
int Pop(StackList &L,Student &e);
int GetTop(StackList &L,Student &e);
int Getlen(StackList &L);
void Free(StackList &L);
int main()
{
StackList A;int x,y,i; Student a;
while(x!=0)
{
printf("1.初始化 2.進棧 3.顯示棧頂信息 4.出棧 0.退出\n");
printf("請輸入數字\n",x);
scanf("%d",&x);
printf("*******************************\n");
switch(x){
case 1:
InitStack(A);
if(A!=NULL)
{
printf("初始化失敗\n");
printf("*******************************\n");
}
else
printf("初始化成功\n");
printf("*******************************\n");
break;
case 2:
printf("請輸入個數:");scanf("%d",&y);
for(i=1;i<=y;i++)
{
Push(A,a);
}
Getlen(A);
break;
case 3:
GetTop(A,a);
break;
case 4:
for(i=1;i<=2;i++)
{
Pop(A,a);
}
printf("*******************************\n");
break;
case 0:
exit(0);
}
}
Free(A);
return 0;
}
void Push(StackList &L,Student &e)
{
StackList s; Student a;
s=(StackList)malloc(sizeof(XjNode));
Input(&a);
strcpy(s->Date.Name,a.Name);
s->Date.Age=a.Age;
strcpy(s->Date.Adree,a.Adree);
s->next=NULL;
if(L==NULL)
L=s;
else
{
s->next=L;
L=s;
}
}
void Input(Student *a)
{
printf("Name:");scanf("%s",a->Name);
printf("Age:"); scanf("%d",&a->Age);
printf("Adree:");scanf("%s",a->Adree);
printf("********************************\n");
}
int GetTop(StackList &L,Student &e)
{
if(L==NULL)
return 0;
else
{
e.Age=L->Date.Age;
strcpy(e.Name,L->Date.Name);
strcpy(e.Adree,L->Date.Adree);
printf("%s %d %s\n",e.Name,e.Age,e.Adree);
printf("********************************\n");
return 1;
}
}
int Pop(StackList &L,Student &e)
{
StackList p;
p=L;
if(L==NULL)
return 0;
else
{
e.Age=L->Date.Age;
strcpy(e.Name,L->Date.Name);
strcpy(e.Adree,L->Date.Adree);
printf("%s %d %s \n",e.Name,e.Age,e.Adree);
L=L->next;
free(p);
return 1;
}
printf("********************************\n");
}
void InitStack(StackList &L)
{
// L=(StackList)malloc(sizeof(StackList));
L=NULL;
}
int Getlen(StackList &L)
{
StackList p; int count=0;
p=L->next;
while(p!=NULL)
{
count++;
p=p->next;
}
return count;
}
void Free(StackList &L)
{
StackList p,q;
p=L;
while(p!=NULL)
{
q=p->next;
free(p);
p=q;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -