⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stacklist.cpp

📁 实现链栈的基本操作
💻 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 + -