- #include
- #include
- //这是定义顺序链表的第一种方法,采用指针方式的来标注底部指针和顶部指针
- typedef struct sqStack{
- char *top;
- char *base;
- int stackSize;
- }sqStack;
- //这是定义顺序链表的第一种方法,采用数组下标的方式来标注底部指针和顶部指针
- //typedef struct sqStack{
- // int top;
- // int base;
- // int stackSize;
- //}sqStack;
- //初始化顺序表
- void initStack(char **chars,sqStack *stack,int stackSize){
- // char chars[stackSize]={}; //对于数组初始化,也就是分配内存可以这样,也可以使用malloc函数
- (*chars)=(char*)malloc(sizeof(char)*stackSize);
- if(!chars){
- printf("内存分配失败!");
- exit(0);
- }
- stack->stackSize=stackSize;
- stack->top=(*chars);
- stack->base=(*chars);
- }
- //入栈
- void push(sqStack *stack,char value){
- if(stack->top-stack->base==stack->stackSize){
- //满栈了
- printf("栈上溢出操作!");
- }else{
- *(stack->top)=value;
- stack->top+=1;
- }
- }
- //出栈
- char pop(sqStack *stack){
- if(stack->top==stack->base){
- printf("栈下溢出操作!");
- return ' ';
- }else{
- --(stack->top);
- return *stack->top;
- }
- }
- //判断栈是否为空
- int isEmpty(sqStack *stack){
- return stack->base==stack->top?1:0;
- }
- //获取栈的大小
- int getSize(sqStack *stack){
- return stack->top-stack->base;
- }
- //清空栈
- void clearStack(sqStack *stack){
- stack->top=stack->base;
- }
- //销毁栈
- void destroyStack(char *chars,sqStack *stack){
- if(chars){
- free(chars);
- stack->base=NULL;
- stack->top=NULL;
- stack->stackSize=0;
- }
- }
-
-
- int main(){
- sqStack stack;
- char *chars;
- initStack(&chars,&stack,100);
- push(&stack,'d');
- push(&stack,'e');
- push(&stack,'t');
- push(&stack,'o');
- printf("%c\n",pop(&stack));
- system("pause");
- return 0;
- }
- package linearList;
- /*
- * 实现一个顺序栈
- * */
- public class sqStack
{ - private int top;
- private int base;
- private T[] stackArray;
-
- public sqStack(int stackSize) {
- this.top =0;
- this.base =0;
- stackArray=(T[]) new Object[stackSize];
- }
- //压栈操作
- sqStack
push(T value){ - if (this.top-this.base!=stackArray.length){
- stackArray[this.top]=value;
- ++this.top;
- }else{
- System.out.println("满栈操作!");
- }
- return this;
- }
- //出栈操作
- T pop(){
- if (this.top==this.base){
- System.out.println("下溢出操作!");
- return null;
- }
- return stackArray[--this.top];
- }
- //判断栈是否为空
- boolean isEmpty(){
- return this.base==this.top?true:false;
- }
- //获取栈的大小
- int getSize(){
- return this.top-this.base;
- }
- //清空栈
- void clearStack(){
- this.top=this.base;
- }
- }
- class sqStackTest{
- public static void main(String[] args) {
- sqStack
stringsqStack = new sqStack(3); - stringsqStack.push("huhua").push("sdasd").push("sdasd");
- System.out.println(stringsqStack.pop());
- System.out.println(stringsqStack.pop());
- System.out.println(stringsqStack.pop());
- System.out.println(stringsqStack.pop());
- }
- }
- #include
- #include
-
- typedef struct stackNode{
- char value;
- struct stackNode* next;
- }*linkStack,stackNode;
-
- typedef struct lqStack{
- linkStack top;
- }lqStack;
- //初始化栈
- void initStack(lqStack *stack){
- stack->top=NULL;
- }
- //入栈
- void push(char value,lqStack *stack){
- stackNode *node=(stackNode*)malloc(sizeof(stackNode));
- node->value=value;
- node->next=stack->top;
- stack->top=node;
- }
- //出栈
- char pop(lqStack *stack){
- if(stack->top){
- stackNode *node=stack->top;
- char value=node->value;
- stack->top=node->next;
- free(node);
- return value;
- }else{
- printf("链栈下溢操作!");
- return ' ';
- }
- }
- //判断链栈是否为空
- int isEmpty(lqStack *stack){
- return stack->top?0:1;
- }
-
- int main(){
- lqStack stack;
- linkStack node;
- initStack(&stack);
- push('d',&stack);
- push('q',&stack);
- push('r',&stack);
- printf("%c\n",pop(&stack));
- printf("%c\n",pop(&stack));
- printf("%c\n",pop(&stack));
- printf("%c\n",pop(&stack));
- system("pause");
- return 0;
- }
- package linearList;
- /*
- * 实现一个链栈
- * */
- class stackNode
{ - T value;
- stackNode
next; -
- public stackNode(T value, stackNode
next) { - this.value = value;
- this.next = next;
- }
- }
- public class lqStack
{ - stackNode
top=null; -
- //入栈
- lqStack push(T value){
- stackNode
newNode = new stackNode<>(value,this.top); - this.top=newNode;
- return this;
- }
- //出栈
- T pop(){
- if (this.top!=null){
- T value = this.top.value;
- this.top=this.top.next;
- return value;
- }else{
- System.out.println("栈下溢出!");
- return null;
- }
- }
- //判断链栈是否为空
- boolean isEmpty(){
- return this.top==null?true:false;
- }
- }
- class lqStackTest{
- public static void main(String[] args) {
- lqStack
Stack = new lqStack<>(); - System.out.println(Stack.isEmpty());
- Stack.push("asas").push("asda").push("ASdsa");
- System.out.println(Stack.pop());
- System.out.println(Stack.pop());
- System.out.println(Stack.pop());
- System.out.println(Stack.pop());
- }
- }