當前位置:首頁 » 編程語言 » c語言中如何使用stack
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言中如何使用stack

發布時間: 2023-01-31 22:25:34

c語言 stack STL應用

#include <iostream>
#include <Stack>
using namespace std;
typedef struct
{
int x;
int y;
}element;
void main()
{
stack<element> s;
element ele;
for( int i=0; i < 10; i++ )
{
ele.x = i;
ele.y = i;
s.push(ele);
}

while(!s.empty())
{
ele = s.top();
printf("%d %d ",ele.x,ele.y);
s.pop();
}
cout << "This stack has a size of " << s.size() << endl;
system("pause");
}


這樣?

Ⅱ C語言棧的調用。

可以。
使用STL的stack,例如:
#include<cstdio>
#include<stack>
using namespace std;
stack<int>s;
int main()
{
int a;
scanf("%d",&a);
getchar();
s.push(a);
printf("%d\n",s.top());
s.pop();
getchar();
return 0;
}
另外,這是C++,C標准庫沒有STL。
----
以下關於stack的成員,抄自MSDN:
Constructors
stack
Constructs a stack that is empty or that is a of a base container object.

Typedefs
container_type
A type that provides the base container to be adapted by a stack.

size_type
An unsigned integer type that can represent the number of elements in a stack.

value_type
A type that represents the type of object stored as an element in a stack.

Member Functions
empty
Tests if the stack is empty.

pop
Removes the element from the top of the stack.

push
Adds an element to the top of the stack.

size
Returns the number of elements in the stack.

top
Returns a reference to an element at the top of the stack.

Ⅲ C語言如何使用堆棧判斷迴文

#include <iosteam>
#include <string>
using namespace std;

#define EMPTY 0
#define FULL 10000
#define MAX 10000

typedef char data;

typedef struct elem {
data d;
struct elem *next;
}elem;

typedef struct stack {
int cnt;
elem *top;
}stack;

void initialize(stack *stk);
void push(data d, stack *stk);
data pop(stack *stk);
bool empty(const stack *stk);
bool full(const stack *stk); //棧操作函數

void initialize(stack *stk)
{
stk->cnt = 0;
stk->top = NULL;
}

bool empty(const stack *stk)
{
return stk->cnt == EMPTY;
}

bool full(const stack *stk)
{
return stk->cnt == FULL;
}

void push(data d, stack *stk)
{
elem *p;

if (!full(stk))
{
p = (elem *)malloc(sizeof(elem));
p->d = d;
p->next = stk->top;
stk->top = p;
stk->cnt++;
}

}

data pop(stack *stk)
{
data d;
elem *p;

if(!empty(stk))
{
d = stk->top->d;
p = stk->top;
stk->top = stk->top->next;
stk->cnt--;
delete p;
}

return d;
}

int main(void)
{
data input[MAX];
stack temp;
int i = 0;
int flag = 0;

initialize(&temp); //初始化臨時棧
cin>>&input; //輸入字元串

while (input[i] != '@')
{//字元串入棧
push(input[i], &temp);
i++;
}

while (!empty(&temp))
{//字元依次出棧和字元數組比較,判斷是否迴文數
if (temp.top->d == input[flag])
{
pop(&temp);
flag++;
}
else
{
cout<<"此字元序列不是迴文數!\n";
break;
}
}

if (empty(&temp))
cout<<"此字元序列是迴文數!\n";

return 1;
}

Ⅳ 那位大神能講下C語言中棧的使用啊

堆棧就是先入後出的數據結構。
如果用c語言來實現的話用個struct
先定義一個棧的節點
struct
node;
typedef
strcut
node
*
position;
typedef
position
stack;
stack
creat();
void
push(int,stack);
int
pop(stack);
int
top();
struct
node
{
int
element;
position
next;
}
stack
creat()
{
stack
s;
s=malloc(sizeof(struct
node));
s->next==NULL;
}
void
push(int
a,stack
s)
{
position
temp;
temp=malloc(sizeof(struct
node));
temp->element=a;
temp->next=s->next;:
s->next=temp;
}
int
pop(stack
s)
{
int
res=s->next->element;
position
temp=s->next;
s->next=temp->next;
free
temp;
}
int
top(stack
s)
{
return
s->next->element;
}
好啦,先creat()一個棧,再進行push
pop等。程序中忽略了麻煩的錯誤檢測給出了重點,當然還可以添加其他操作。。對了,頭文件也要加上。本人還是習慣用c++弄成一個類^_^

Ⅳ 請問c語言有沒有像c++一樣的stack庫函數用來直接使用棧

C語言中包含一些標準的庫函數,但是沒有像C++中的STL容器那部分全面的結構和函數。
在C語言中如果想使用棧,需要自己編寫代碼,如果是簡單的一次性應用,可以用數組模擬棧的功能,如果是在一個大項目中反復使用,可以自己寫一個stack的庫函數。
自定義的庫函數中,至少應該包含初始化、銷毀、入棧、出棧、取棧頂元素、判斷棧是否為空等操作。

Ⅵ C語言中使用stack頭文件

stack < item >s; //棧元素類型要定義成item
s.push(temp); //這樣才可以壓入結構體數據

Ⅶ 基於C語言堆棧push,pop,destroystack,isEmpty,isFull實現

以下代碼是基於C語言寫的堆棧的壓棧,出棧,清棧,讀棧指針等方法,在Visual studio 中,可直接使用,供學習者參考學習。

#include

#include

#include

#include

#include

#include

#define MAX_SIZE 100

typedef struct Stack

{

char *data;

int size;

int top;

};

void initStack(Stack *s); //init stack

void destroyStack(Stack *s);

bool push(Stack *s,char ch);

char pop(Stack *s);

char gettop(Stack *s);

bool isEmpty(Stack *s);

bool isFull(Stack *s);

void setNull(Stack *s);

#endif

void initStack(Stack *s)

{

s->data = (char*)malloc(MAX_SIZE * sizeof(char)); //分配最大內存空間

if (!s->data)

exit(OVERFLOW); //提前終止程序

s->size = MAX_SIZE;

s->top = -1;

}

void destroyStack(Stack *s)

{

free(s->data);

}

bool push(Stack *s, char ch)

{

if ((s->top + 1) != s->size)

{

s->data[++s->top] = ch;

return true;

}

else

return false;

}

char pop(Stack *s)

{

if (s->top != -1)

return s->data[s->top--];

}

char gettop(Stack *s)

{

return s->data[s->top];

}

bool isEmpty(Stack *s)

{

if (s->top == -1)

return true;

else

return false;

}

bool isFull(Stack *s)

{

if ((s->top + 1) == s->size)

return true;

else

return false;

}

void setNull(Stack *s)

{

s->top = -1;

}

int main()

{

char chd;

bool c;

Stack s1;

initStack(&s1);

c = push(&s1, 'a');

printf("Stack s1 push status is %d,s.data is %c,top value is %d ", c,s1.data[s1.top],s1.top);

c = push(&s1, 'b');

printf("Stack s1 push status is %d,s.data is %c,top value is %d ", c, s1.data[s1.top], s1.top);

chd = gettop(&s1);

printf("Stack s1->top data:%c,top value is %d ", chd, s1.top);

chd = pop(&s1);

printf("Stack 彈出 data:%c,top value is %d ", chd, s1.top);

chd = pop(&s1);

printf("Stack 彈出 data:%c,top value is %d ", chd, s1.top);

c = isEmpty(&s1);

printf("Stack s1 c bool:%d,top value is %d ", c, s1.top);

c = isFull(&s1);

printf("Stack s1 c bool:%d,top value is %d ", c, s1.top);

return 0;

}

Ⅷ C程序中如何使用堆棧

先從大家比較熟悉的棧說起,它是一種具有後進先出性質的數據結構,也就是說後存放的先取,先存放的後取。這就如同要取出放在箱子裡面底下的東西(放入的比較早的物體),首先要移開壓在它上面的物體(放入的比較晚的物體)。而堆就不同了,堆是一種經過排序的樹形數據結構,每個結點都有一個值。通常所說的堆的數據結構,是指二叉堆。堆的特點是根結點的值最小(或最大),且根結點的兩個子樹也是一個堆。由於堆的這個特性,常用來實現優先隊列,堆的存取是隨意,這就如同在圖書館的書架上取書,雖然書的擺放是有順序的,但是想取任意一本時不必像棧一樣,先取出前面所有的書,書架這種機制不同於箱子,可以直接取出想要的書。
下面就說說C語言程序內存分配中的堆和棧,這里有必要把內存分配也提一下,一般情況下程序存放在Rom或Flash中,運行時需要拷到內存中執行,內存會分別存儲不同的信息。
內存中的棧區處於相對較高的地址以地址的增長方向為上的話,棧地址是向下增長的,棧中分配局部變數空間,堆區是向上增長的用於分配程序員申請的內存空間。另外還有靜態區是分配靜態變數,全局變數空間的;只讀區是分配常量和程序代碼空間的;以及其他一些分區。來看一個網上很流行的經典例子:
main.cpp
int a = 0; 全局初始化區
char *p1; 全局未初始化區
main()
{
int b; 棧
char s[] = "abc"; 棧
char *p2; 棧
char *p3 = "123456"; 123456\0在常量區,p3在棧上。
static int c =0; 全局(靜態)初始化區
p1 = (char *)malloc(10); 堆
p2 = (char *)malloc(20); 堆
}
堆和棧的第一個區別就是申請方式不同:棧(英文名稱是stack)是系統自動分配空間的,例如定義一個 char a;系統會自動在棧上為其開辟空間。而堆(英文名稱是heap)則是程序員根據需要自己申請的空間,例如malloc(10);開辟十個位元組的空間。由於棧上的空間是自動分配自動回收的,所以棧上的數據的生存周期只是在函數的運行過程中,運行後就釋放掉,不可以再訪問。而堆上的數據只要程序員不釋放空間,就一直可以訪問到,不過缺點是一旦忘記釋放會造成內存泄露。

Ⅸ 如何在C語言里應用stack

就用一個函數就可以了

voidprint(SqStackp)
{
while(top>=0)
{
printf("%d",p[top]);
top--;
}
}