赞
踩
//栈的结点设计
//单个结点设计,数据和下一个指针
typedef struct node
{
int data;
struct node *next;
} Node;
//利用上面的结点创建栈,分为指向头结点的top指针和计数用的count
typedef struct stack
{
Node *top;
int count;
} Link_Stack;
//入栈 push
Link_Stack *Push_stack(Link_Stack *p, int elem)
{
if (p == NULL)
return NULL;
Node *temp;
temp=(Node*)malloc(sizeof(Node));
temp->data = elem;
temp->next = p->top;
p->top = temp;
p->count++;
return p;
}
//出栈 pop Link_Stack *Pop_stack(Link_Stack *p) { Node *temp; temp = p->top; if (p->top == NULL) { printf("错误:栈为空"); return p; } else { p->top = p->top->next; free(temp); p->count--; return p; } }
//遍历栈:输出栈中所有元素 int show_stack(Link_Stack *p) { Node *temp; temp = p->top; if (p->top == NULL) { printf(""); printf("错误:栈为空"); return 0; } while (temp != NULL) { printf("%d\t", temp->data); temp = temp->next; } printf("\n"); return 0; }
#include <stdio.h> #include <stdlib.h> //栈的结点设计 //单个结点设计,数据和下一个指针 typedef struct node { int data; struct node *next; } Node; //利用上面的结点创建栈,分为指向头结点的top指针和计数用的count typedef struct stack { Node *top; int count; } Link_Stack; //创建栈 Link_Stack *Creat_stack() { Link_Stack *p; //p = new Link_Stack; p=(Link_Stack*)malloc(sizeof(Link_Stack)); if(p==NULL){ printf("创建失败,即将退出程序"); exit(0); } p->count = 0; p->top = NULL; return p; } //入栈 push Link_Stack *Push_stack(Link_Stack *p, int elem) { if (p == NULL) return NULL; Node *temp; temp=(Node*)malloc(sizeof(Node)); //temp = new Node; temp->data = elem; temp->next = p->top; p->top = temp; p->count++; return p; } //出栈 pop Link_Stack *Pop_stack(Link_Stack *p) { Node *temp; temp = p->top; if (p->top == NULL) { printf("错误:栈为空"); return p; } else { p->top = p->top->next; free(temp); //delete temp; p->count--; return p; } } //遍历栈:输出栈中所有元素 int show_stack(Link_Stack *p) { Node *temp; temp = p->top; if (p->top == NULL) { printf(""); printf("错误:栈为空"); return 0; } while (temp != NULL) { printf("%d\t", temp->data); temp = temp->next; } printf("\n"); return 0; } int main() { //用主函数测试一下功能 Link_Stack *p; p = Creat_stack(); int n = 5; int input[6] = {10,20,30,40,50,60}; /以依次入栈的方式创建整个栈// for(int i=0;i<n;i++){ Push_stack(p, input[i]); } show_stack(p); 出栈/// Pop_stack(p); show_stack(p); return 0; }
//链表中的节点结构
typedef struct lineStack{
int data;
struct lineStack * next;
}lineStack;
//链表中的节点结构 typedef struct lineStack{ int data; struct lineStack * next; }lineStack; //stack为当前的链栈,a表示入栈元素 lineStack* push(lineStack * stack,int a){ //创建存储新元素的节点 lineStack * line=(lineStack*)malloc(sizeof(lineStack)); line->data=a; //新节点与头节点建立逻辑关系 line->next=stack; //更新头指针的指向 stack=line; return stack; }
//栈顶元素出链栈的实现函数 lineStack * pop(lineStack * stack){ if (stack) { //声明一个新指针指向栈顶节点 lineStack * p=stack; //更新头指针 stack=stack->next; printf("出栈元素:%d ",p->data); if (stack) { printf("新栈顶元素:%d\n",stack->data); }else{ printf("栈已空\n"); } free(p); }else{ printf("栈内没有元素"); return stack; } return stack; }
#include <stdio.h> #include <stdlib.h> typedef struct lineStack{ int data; struct lineStack * next; }lineStack; lineStack* push(lineStack * stack,int a){ lineStack * line=(lineStack*)malloc(sizeof(lineStack)); line->data=a; line->next=stack; stack=line; return stack; } lineStack * pop(lineStack * stack){ if (stack) { lineStack * p=stack; stack=stack->next; printf("弹栈元素:%d ",p->data); if (stack) { printf("栈顶元素:%d\n",stack->data); }else{ printf("栈已空\n"); } free(p); }else{ printf("栈内没有元素"); return stack; } return stack; } int main() { lineStack * stack=NULL; stack=push(stack, 1); stack=push(stack, 2); stack=push(stack, 3); stack=push(stack, 4); stack=pop(stack); stack=pop(stack); stack=pop(stack); stack=pop(stack); stack=pop(stack); return 0; }
//元素elem进栈,a为数组,top值为当前栈的栈顶位置
int push(int* a,int top,int elem){
a[++top]=elem;
return top;
}
//数据元素出栈
int pop(int * a,int top){
if (top==-1) { //防止用户做 "栈中已无数据却还要数据出栈" 的错误操作
printf("空栈");
return -1;
}
printf("弹栈元素:%d\n",a[top]);
top--;
return top;
}
#include <stdio.h> //元素elem进栈 int push(int* a, int top, int elem) { a[++top] = elem; return top; } //数据元素出栈 int pop(int* a, int top) { if (top == -1) { printf("空栈"); return -1; } printf("弹栈元素:%d\n", a[top]); top--; return top; } int main() { int a[100]; int top = -1; top = push(a, top, 3); top = push(a, top, 1); top = push(a, top, 4); top = push(a, top, 5); top = pop(a, top); top = pop(a, top); top = pop(a, top); top = pop(a, top); top = pop(a, top); return 0; }
#include<stdio.h> #include<stdlib.h> #include<string.h> #define maxn 10000 //结点设计 typedef struct stack{ int data[maxn]; int top; }stack; //创建 stack *init(){ stack *s=(stack *)malloc(sizeof(stack)); if(s==NULL){ printf("分配内存空间失败"); exit(0); } memset(s->data,0,sizeof(s->data)); //memset操作来自于库文件string.h,其表示将整个空间进行初始化 //不理解可以查阅百度百科https://baike.baidu.com/item/memset/4747579?fr=aladdin s->top=0; //栈的top和bottom均为0(表示为空) return s; } //入栈push void push(stack *s,int data){ s->data[s->top]=data; s->top++; } //出栈pop void pop(stack *s){ if(s->top!=0){ s->data[s->top]=0; //让其回归0模拟表示未初始化即可 s->top--; } } //模拟打印栈中元素 void print_stack(stack *s){ for(int n=s->top-1;n>=0;n--){ printf("%d\t",s->data[n]); } printf("\n"); //习惯性换行 } int main(){ stack *s=init(); int input[5]={11,22,33,44,55}; //模拟五个输入数据 for(int i=0;i<5;i++){ push(s,input[i]); } print_stack(s); / pop(s); print_stack(s); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。