码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构之---c语言实现栈及其操作

时间:2015-05-18 10:59:25      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

//c语言实现栈及其基本操作
//杨鑫
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

typedef int ElementType;
typedef struct node {
    ElementType data;
    struct node *next;
}StackNode, *LinkStack;

void init_stack(LinkStack top)
{
    top->next = NULL;
}

int is_empty(LinkStack top)
{
    if(top->next == NULL) 
			return TRUE;
	return FALSE;
}
int push(LinkStack top, ElementType element) 
{
    StackNode *temp;
    temp = (StackNode *)malloc(sizeof(StackNode));
    if(temp == NULL) return FALSE;
    temp->data = element;
    temp->next = top->next;
    top->next = temp;
    return TRUE;
}

int pop(LinkStack top, ElementType *element) 
{
    if(is_empty(top)) return FALSE;
    StackNode *temp = top->next;
    *element = temp->data;
    top->next = temp->next;
    free(temp);
	return TRUE;
}
void get_top(LinkStack top, ElementType *element) 
{
    *element = top->next->data;
}


int main() 
{
    LinkStack s;
	int count = 0;
    s = (LinkStack)malloc(sizeof(StackNode));
    init_stack(s);
    int i = 0;
	printf("正在进行压栈操作,请稍后。\n");
	for(i=0; i<100; i++)
        push(s,i);
	printf("已经数据0 ~ 99存储到了栈中!\n");
    int rslt;
    printf("正在出栈\n");
	printf("将栈内元素依次输出!\n");
	printf("栈中元素如下:!\n");
	while (!is_empty(s))
   	{
		if(count % 5 == 0)
				printf("\n");
        pop(s,&rslt);
        printf("%d\t",rslt);
		count++;
    }
	return 0;
} 



技术分享

数据结构之---c语言实现栈及其操作

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/45815087

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!