Chinaunix首页 | 论坛 | 博客
  • 博客访问: 289715
  • 博文数量: 113
  • 博客积分: 446
  • 博客等级: 下士
  • 技术积分: 1229
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-09 16:01
个人简介

Let's go!!!!!

文章分类

全部博文(113)

文章存档

2019年(5)

2018年(4)

2017年(9)

2016年(5)

2015年(39)

2014年(6)

2013年(28)

2012年(17)

分类: C/C++

2012-12-31 09:51:02

//顺序栈

#include
#include
#include
 
#define stacksize  10
#define add         2

typedef struct
{
 int *top;
 int *base;
 int size;
}Stack;

void initStack(Stack *s)                          //初始化栈
{
s->base=(int *)malloc(stacksize * sizeof(int));
if(!s->base){
printf("Memery allocate fail!\n");
exit(1);
}
s->top = s->base;
s->size=stacksize;
}
                  
void push(Stack *s,int e)                          //入栈
{
if(s->top - s->base >=s->size){
s->base=(int *)realloc(s->base,(s->size + add) * sizeof(int));
if(!s->base){
printf("Memery allocate fail!\n");
exit(1);
}
s->top=s->base+s->size;
s->size+=add;

*s->top++=e; //*s->top=e; s->top++;
printf("%d  ",e);
}

int pop(Stack *s)                                //出栈
{
if(s->base==s->top) return 0;
else
return *(--s->top);
}

int getTop(Stack s)                              //获取栈顶元素
{
if(s.base==s.top) return 0;
else
return  *(s.top-1);
}

void stackTaverse(Stack s)                       //遍历
{
while(s.base!=s.top)
printf("%d ",*(s.base++));
printf("\n");
}

int stackEmpty(Stack s)                          //判断栈是否为空
{
if(s.base==s.top)
return 1;
else
return 0;
}


int stackDepth(Stack s)                          //栈的深度
{
int i=0;
while(s.base!=s.top){
s.base++;
i++;
}
return i;
}

void clearStack(Stack *s)                        //清空栈
{
while(s->base!=s->top)
s->top--;
}

void main()
{
Stack s;
int  i;
initStack(&s);                                       //初始化
printf("Push element into stack\n");
for(i=0;i<10;i++)
push(&s,i);                                          //入栈
printf("\n");
printf("Stack traverse:");
stackTaverse(s);                                     //遍历  
printf("Top element=%d\n",getTop(s));                //获取栈顶元素
printf("Pop element=%d\n",pop(&s));                  //出栈
printf("Stack traverse:");
stackTaverse(s);                                      //遍历
printf("%s\n",stackEmpty(s)?"Empty":"Not empty");     //判断栈是否为空
printf("Stack depth %d\n",stackDepth(s));             //栈的深度
clearStack(&s);                                       //清空栈
printf("%s\n",stackEmpty(s)?"Empty":"Not empty");
}


 

#include
#include
#include

#define init_size 10
#define add       2

typedef struct{
char name[10];
int age;
}Note;

typedef struct{
Note *base;
Note *top;
int size;
}Stack;

void initStack(Stack *s)
{
s->base=(Note *)malloc(init_size * sizeof(Note));
if(!s->base){
printf("Memery allocate fail\n");
exit(1);
}
s->top=s->base;
s->size=init_size;
}

void push(Stack *s,Note note)
{
 printf("address:%ld\n",s->top);
 if(s->top - s->base >= s->size){
 s->base=(Note *)realloc(s->base,(s->size + add) * sizeof(Note));
 if(!s->base){
 printf("Memery allocate fail\n");
 exit(1);
 }
 s->top=s->base + s->size;
 s->size+=add;
 }
    *s->top++=note;
}

void pop(Stack *s)
{
if(s->base!=s->top)
s->top--;
}

Note getTop(Stack s)
{
if(s.base!=s.top)
return *(s.top-1);
}

void stackTraverse(Stack s)
{
Note note;
while(s.base!=s.top)
{
note=*s.base++;
printf("name=%s,age=%d\n",note.name,note.age);
}
}

int main()
{  
    Stack  s;
 Note note;
 int i;
 initStack(&s);
    for(i=0;i<3;i++)
 {
  printf("Enter %dst person's name:",i+1);
  scanf("%s",note.name);
  printf("Enter %dst person's age:",i+1);
  scanf("%d",¬e.age);
        push(&s,note);
 }
 printf("Get the top element:\n");
 note=getTop(s);
 printf("num=%s,age=%d\n",note.name,note.age);
 printf("Have pop a element!!!\n");
 pop(&s);
 printf("Traverse the stack:\n");
 stackTraverse(s);
 return 0;
}

 

//链栈

#include
#include
#include

typedef struct{
char name[10];
int age;
}Data;

struct Note{
Data data;
struct Note *next;
};

struct Note *initStack(struct Note *head)
{
head=NULL;
return head;
}

struct Note *push(struct Note *head,Data data)
{
struct Note *temp;
temp=(struct Note *)malloc(sizeof(struct Note));
if(!temp){
printf("Malloc fail\n");
exit(1);
}
temp->data=data;
temp->next=head;
head=temp;
return head;
}

void getTop(struct Note *s)
{
Data data;
data=s->data;
printf("%s %d\n",data.name,data.age);
}

struct Note *pop(struct Note *s)
{
struct Note *temp;
temp=s->next;
free(s);
return temp;
}

void stackTraverse(struct Note *s)
{
Data note;
while(s!=NULL)
{
note=s->data;
printf("%s %d\n",note.name,note.age);
s=s->next;
}
}

int stackEmpty(struct Note *s)
{
if(s==NULL)
return 1;
else
return 0;
}

int main()
{  
struct Note *s;
Data data;
int i;
s=(struct Note *)malloc(sizeof(struct Note));
s=initStack(s);
for(i=0;i<3;i++)
{
 printf("Enter %dst person's name:",i+1);
 scanf("%s",data.name);
 printf("Enter %dst person's age:",i+1);
 scanf("%d",&data.age);
    s=push(s,data);
}
printf("Got the top element\n");
getTop(s);
printf("Pop a element\n");
s=pop(s);
printf("Taverse the stack\n");
stackTraverse(s);
printf("stack %s\n",stackEmpty(s)?"empty":"not empty");
return 0;
}

阅读(486) | 评论(0) | 转发(0) |
0

上一篇:C语言 链表

下一篇:C语言 队列

给主人留下些什么吧!~~