棧的C數組實現,C數組實現

來源:互聯網
上載者:User

棧的C數組實現,C數組實現

棧是一種先進後出的資料結構.

棧的基本操作包括:入棧,出棧,初始化棧,清空棧,遍曆棧.


C代碼如下:

#include <stdio.h>#define MaxSize 20typedef int ElemType;typedef struct stack{    ElemType Data[MaxSize];    int top;}Stack;//初始化棧void InitStack(Stack *S){    S->top=-1;}//入棧void PushStackValue(Stack *S){    printf("Input the Value of stack member:\n(0-exit)\n");    int value;    printf("Please input the 1st value of stack:\n");    scanf("%d",&value);    S->Data[++S->top]=value;    while(value)    {        S->top++;        printf("Please input the %dst value of stack:\n",S->top+1);        scanf("%d",&value);        S->Data[S->top]=value;    }}//出棧void PopStackValue(Stack *S){    if(S->top>=0)    {        printf("the stack %dst value pop out: %d\n",S->top+1,S->Data[--S->top]);    }    else    {        printf("The Stack is empty\n");    }}//判斷棧空void IsEmpty(Stack *S){    if(S->top==-1)    {        printf("The Stack is empty.\n");    }    else    {        printf("The stack is not empty.\n");    }}//清空棧void ClearStack(Stack *S){    S->top=-1;}//遍曆棧void ScanStack(Stack *S){    int i;    int len=S->top-1;    int StackArray[len];    for(i=len;i>0;i--)    {        StackArray[i]=S->Data[i--];    }    printf("The all stack member(from top to bottom) is:\n");    while(len>=0)    {        printf("%d ",S->Data[len--]);    }    printf("\n");}void main(){    Stack S;    InitStack(&S);    PushStackValue(&S);    ScanStack(&S);    IsEmpty(&S);    PopStackValue(&S);    PopStackValue(&S);    PopStackValue(&S);    PopStackValue(&S);}


運行結果如下:




轉載請註明作者:小劉


用數組實現棧與隊列的資料結構

#include <stdlib.h>
#include <stdio.h>

class Stack
{
private:
int ele[100];
int top;
public:
Stack() {
top = 0;
}
void push(int v) {
ele[top++] = v;
}
int pop() {
if(isEmpty()) {
printf("The stack is Empty.\n");
return -1;
}
return ele[--top];
}
bool isEmpty() {
return top == 0;
}
~Stack() {
}
};

#define SIZE 100
class Queue
{
private:
int ele[SIZE];
int head;
int tail;
public:
Queue() {
head = 0;
tail = 0;
}
bool isFull() {
return (tail+1) % SIZE == head;
}
bool isEmpty() {
return head == tail;
}
void enqueue(int val) {
if(isFull()) {
printf("The queue is full, can not enqueue for: %d\n", val);
return;
}
ele[tail] = val;
tail = (tail+1) %SIZE;
}
int dequeue() {
if(isEmpty()) {
printf("The queue is empty, can not dequeue.\n");
return -1;
}
int value = ele[head];
head = (head+1)%SIZE;
return value;
}
};

int main()
{
Stack s;
s.push(123);
s.push(12);
while(!s.isEmpty()) {
printf("%d ", s.pop());
}
printf("\n");

Queue q;
q.enqueue(88);
q.enqueue(13);
q.enqueue(123);
while(!q.isEmpty()) printf("%d ", q.dequeue());
printf("\n");
system("pause");
}...餘下全文>>
 
用數組實現棧的功可以的C語言代碼?

棧在處理數組上面真的很方便,這是棧的基礎方法函數
//順序棧的實現stack.cpp
#include "stack.h"
Status SqStack::InitStack(SqStack **S)
{ (*S)=(SqStack *) malloc(sizeof(SqStack));
(*S)->base=(SElemType *)malloc(STACKSIZE *sizeof(SElemType));
if(!(*S)->base) exit(OVERFLOW);
(*S)->top=(*S)->base;
(*S)->stacksize=0;
return 1;}

Status SqStack::DestroyStack()
{free(base);return 1;}

Status SqStack::ClearStack()
{stacksize=0;return 1;}

bool SqStack::StackEmpty()
{ if(stacksize==0) return true;
else return false;
}
int SqStack::StackLength()
{ return stacksize;}
SElemType SqStack::GetTop()
{ if(top==base)
{cerr<<"空棧!\n";exit(1);}
return *(top-1);
}
Status SqStack::Push(SElemType e)
{ *(top++)=e;stacksize++;
return 1;
}
SElemType SqStack::Pop(SElemType *e)
{ if(top==base)
{cerr<<"空棧!\n";exit(1);}
*e=*--top;
stacksize--;
return *e;
}
void SqStack::StackTraverse(void (*visit)(SElemType *))
{while(top!=base){
stacksize--;visit(--top);}}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.