实现stack

实现stack

  • stack.h
    #include<stdlib.h>
    #include <stdio.h>
    #include<memory.h>
    #define N 100
    
    struct  stack
    {
    	int data[N];
    	int top;//标识栈顶
    }; 
    
    typedef struct  stack  Stack;//Stack别名
    
    void init(Stack * p);//初始化
    int  isempty(Stack * p);//判定栈是否空
    int isfull(Stack * p);//判定栈溢出
    int  gettop(Stack * p);//获取栈顶
    void push(Stack * p, int key);//插入数据
    void pop(Stack * p);//吐出
    void  show(Stack * p);//显示栈
    
  • stack.c
    #include "stack.h"
    void init(Stack * p)//初始化
    {
    	p->top = -1;//代表为空
    	memset(p->data, 0, sizeof(int)*N);//数据清零
    
    }
    int  isempty(Stack * p)//判定栈是否空
    {
    	if (p->top==-1)
    	{
    		return 1;//1为空
    	} 
    	else
    	{
    		return 0;//0不为空
    	}
    
    
    }
    int isfull(Stack * p)//判定栈溢出
    {
    	if (p->top==N-1)
    	{
    		return 1;///溢出
    	} 
    	else
    	{
    		return 0;//还能再喝点
    	}
    
    
    }
    int  gettop(Stack * p)//获取栈顶
    {
    	return p->data[p->top];//获取栈顶
    }
    void push(Stack * p, int key)//插入数据
    {
    	if (isfull(p)==1)
    	{
    		return;
    	} 
    	else
    	{
    		p->top += 1;
    		p->data[p->top] = key;//压入数据
    
    
    	}
    
    
    }
    void pop(Stack * p)//吐出
    {
    	if (isempty(p)==1)
    	{
    		return;
    	} 
    	else
    	{
    		p->top -= 1;//出栈
    	}
    
    
    
    }
    void  show(Stack * p)
    {
    
    
    
    	if (isempty(p) == 1)
    	{
    		return;
    	}
    	else
    	{
    		printf("\n栈的数据是\n");
    		for (int i = 0; i <= p->top;i++)
    		{
    			printf("%4d", p->data[i]);//打印栈的数据
    		}
    		printf("\n");
    
    
    	}
    
    
    }
    
  • main.c
    #define  _CRT_SECURE_NO_WARNINGS
    #include"stack.h"
    
    //10   %2  0
    //5    %2  1
    //2    %2  0
    //1    %2  1
    //0
    
    void to2(int num)
    {
    	if (num==0)
    	{
    		return;
    	} 
    	else
    	{
    	
    		to2(num / 2);
    		printf("%d", num % 2);
    	}
    
    }
    
    void  main5()
    {
    	int num;
    	scanf("%d", &num);
    	Stack mystack;
    	init(&mystack);//初始化
    
    	while (num)
    	{
    		push(&mystack, num % 2);
    		num /= 2;
    
    	}
    	while (!isempty(&mystack))
    	{
    		printf("%d", gettop(&mystack));
    		pop(&mystack);
    
    	}
    
    	system("pause");
    
    
    
    
    
    
    }
    
    void main7()
    {
    
    	int num;
    	scanf("%d", &num);
    	Stack mystack;
    	init(&mystack);//初始化
    
    	while (num)
    	{
    		push(&mystack, num % 2);
    		num /= 2;
    		printf("%d", gettop(&mystack));
    		pop(&mystack);
    
    	}
    	
    	system("pause");
    }
    
    
    void main4()
    {
    	int num;
    	scanf("%d", &num);
    	to2(num);
    
    	system("pause");
    
    
    }
    
    
    
    void main3()
    {
    	int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    
    	Stack mystack;
    	init(&mystack);//初始化
    	for (int i = 0; i < 10; i++)
    	{
    		push(&mystack, i);
    		printf("%d", gettop(&mystack));
    		pop(&mystack);
    	}
    	system("pause");
    }
    void main2()
    {
    	int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    
    	Stack mystack;
    	init(&mystack);//初始化
    	for (int i = 0; i < 10;i++)
    	{
    		push(&mystack, i);
    	}
    	while (!isempty(&mystack))
    	{
    		printf("%d", gettop(&mystack));
    		pop(&mystack);
    	}
    	system("pause");
    }
    
    
    void main1()
    {
    	struct MyStruct
    	{
    		int a[10];
    		int i;
    
    	};
    	//结构体意义,将多个数据整合在一起
    	struct MyStruct  my1 = { { 1, 2, 3 }, 1 };//初始化
    	printf("\n%d", my1.i);//访问结构体内部成员
    	printf("\n%d", (&my1)->i);
    	printf("\n%d", (*(&my1)).i);
    
    
    	for (int j = 0; j < 10;j++)
    	{
    		printf("\n%d ", my1.a[j]);//可以用.引用
    
    	}
    	system("pause");
    
    
    }
    
     //111 12 21 3
    //11 2  1
    //1 1 1
    int  get(int n)
    {
    	 if (n==1)
    	 {
    		 return 1;
    	 } 
    	 else if (n==2)
    	 {
    		 return 2;
    	 } 
    	 else if (n==3)
    	 {
    		 return 4;
    	 } 
    	 else
    	 {
    		 int  temp = get(n - 1) + get(n - 2) + get(n - 3);
    		 return temp ;
    	 }
    
    
    }
    
    void run(int i)
    {
    	if (i==0)
    	{
    		return;
    	} 
    	else
    	{
    		system("notepad");
    		run(--i);
    	}
    }
    
    void main()
    {
    
    	printf("\n%d", get(10));
    	int a[10] = { 1, 2, 4 };
    	for (int i = 3; i < 10;i++)
    	{
    		a[i] = a[i-1] + a[i-2] + a[i-3];
    	}
    	printf("\n%d", a[9]);
    
    	system("pause");
    }
    
posted @ 2016-11-15 22:31  呉语伦比  阅读(175)  评论(0)    收藏  举报