日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux 复制栈数据结构,算法-数据结构-堆栈

發布時間:2023/12/19 linux 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux 复制栈数据结构,算法-数据结构-堆栈 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

算法-數據結構-堆棧

發布時間:2005-09-09 17:46:22來源:紅聯作者:frog

/****************************************************************

Title : stack.c

Author : 張龍

Time : 200508

*****************************************************************/

#include "stdio.h"

#define STACK_SIZE 100

typedef int ElementType;

typedef struct _stacknode

{

ElementType data;

struct _stacknode *next;

} stacknode;

typedef struct _stack

{

stacknode *top,*bottom;

int count;

} stack;

/*========================functions declaration===================*/

void init(stack *s);

int IsEmpty(stack *s);

int IsFull(stack *s);

int GetStackSize();

int length(stack *s);

ElementType top(stack *s);

int push(stack *s,ElementType x);

int pop(stack *s);

/*====================function implementation================*/

void init(stack *s)

{

s->top=s->bottom=0;

s->count=0;

}

int IsEmpty(stack *s)

{

if(!s->count)

return 1;

else

return 0;

}

int IsFull(stack *s)

{

if(s->count==STACK_SIZE)

return 1;

else

return 0;

}

int GetStackSize()

{

return STACK_SIZE;

}

int length(stack *s)

{

return s->count;

}

ElementType top(stack *s)

{

return s->top->data;

}

int push(stack *s,ElementType x)

{

stacknode *node;

if(IsFull(s)){

printf("the stack is full,no new data can be inserted.\n");

return 0;

}

if(!s->bottom){

node=s->bottom=s->top=(stacknode *)malloc(sizeof(stacknode));

node->data=x;

}

else{

node=(stacknode *)malloc(sizeof(stacknode));

node->data=x;

s->top->next=node;

s->top=node;

}

s->count++;

return;

}

ElementType pop(stack *s)

{

int i,j;

ElementType data;

stacknode *node;

if(IsEmpty(s)){

printf("the stack is empty,no data is popped up\n");

return 0;

}

data=s->top->data;

free(s->top);

s->count--;

j=s->count;

if(j==1){

s->top=s->bottom;

s->top->next=0;

}

else{

for(i=0;iif(i==0){

node=s->bottom;

}

else{

node=node->next;

}

}

s->top=node;

s->top->next=0;

}

return data;

}

/*========================main function========================*/

int main(int argc,char*argv[])

{

int i,j;

stack *mystack;

mystack=(stack *)malloc(sizeof(stack));

init(mystack);

for(i=0;i<100;i++){

j=i+3;

push(mystack,j);

printf("%d ",top(mystack));

}

printf("\n");

printf("the size of stack is %d\n",GetStackSize());

printf("the current length of stack is %d\n",length(mystack));

for(i=0;i<50;i++){

printf("%d ",pop(mystack));

}

printf("\n");

printf("the size of stack is %d\n",GetStackSize());

printf("the current length of stack is %d\n",length(mystack));

return 1;

}

總結

以上是生活随笔為你收集整理的linux 复制栈数据结构,算法-数据结构-堆栈的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。