c語言實現棧
❶ 用c語言實現棧的操作,包括創建空棧,PUSH,和POP。用標准C,就是能在TC上運行的。
...中國出的這些書太應試化了,如果真心想學買一本機械工業出版社的數據結構(C語言版)寫的很好,演算法都是有C描述的包括你需要的東西...我懶得打那麼長的演算法程序了...
❷ C語言,棧的實現~
你寫的太復雜,這個拿去用吧
// Stack node
struct Node
{
int data ;
Node* next ;
Node(int d, Node* p):data(d), next(p){}
};
class Stack
{
public:
Stack():top(NULL){}
void Push(int d)
{
top = new Node(d, top) ;
}
int Pop()
{
Node* temp = top ;
top = top->next ;
return temp->data ;
}
bool Empty()
{
return top == NULL ;
}
private:
Node* top ;
};
❸ c語言實現隊列和棧的方法
#define OVERFLOW -1
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define N 20
typedef char SElemType;
typedef int Status;typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;#include<stdio.h>
#include<stdlib.h>Status CreatStack(SqStack &S){
//創建棧
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}//CreatStackStatus Push(SqStack &S,SElemType e){
//插入e為新的棧頂元素
if(S.top-S.base>=S.stacksize){//棧滿,追加存儲空間
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)exit (OVERFLOW);//存儲空間分配失敗
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}//PushStatus Pop(SqStack &S ,SElemType &e){
//若棧不空,刪除棧頂元素,並用e返回其值
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}//Pop typedef char QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QNodePtr;typedef struct{
QNodePtr front;
QNodePtr rear;
}LinkQueue;Status CreatQueue(LinkQueue &Q){
//建立一個空的鏈式棧
Q.front=Q.rear=(QNodePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}Status EnQueue(LinkQueue &Q,QElemType e){ QNodePtr p;
p=(QNodePtr)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p->data=e;p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}Status DeQueue(LinkQueue &Q,QElemType &e){QNodePtr p;<br>if(Q.front==Q.rear) return ERROR;<br>p=Q.front->next; e=p->data;<br>Q.front->next=p->next;<br>if(Q.rear==p) Q.rear=Q.front;<br>free(p);<br>return OK;<br>}int stackPalindrom_Test()//判別輸入的字元串是否迴文序列,是則返回1,否則返回0
{
printf("棧練習,請輸入要判斷的字元串以#作為結束符,不要超過二十個字元\n");
SqStack S;
CreatStack(S);
char c;
SElemType a;
SElemType b[N];
int count = 0;
fgets( b, N, stdin );
while((b[count])!='#')
{
Push(S,b[count]); //進棧
count++;
}
int i = 0;
while(i < count)
{
Pop(S,a);
if(a!=b[i])
return ERROR;
i++;
}
return OK;}//stackPalindrom_Test int queuePalindrom_Test()//判別輸入的字元串是否迴文序列,是則返回1,否則返回0
{
printf("隊列練習,請輸入要判斷的字元串以#作為結束符,不要超過二十個字元\n");
LinkQueue Q;
CreatQueue(Q); char c;
SElemType a;
SElemType b[N];
int count = 0;
fgets( b, N, stdin );
while((b[count])!='#')
{
EnQueue(Q,b[count]);; //入列
count++;
} while(count-- > 0)
{
DeQueue(Q,a);
if(a!=b[count])
return ERROR;
}
return OK;
}//queuePalindrom_Test Status main(){ if(stackPalindrom_Test()==1)
printf("是迴文\n");
else printf("不是迴文\n"); if(queuePalindrom_Test()==1)
printf("是迴文\n");
else printf("不是迴文\n");
return OK;
}
❹ 數據結構 用C語言編程實現進棧出棧
希望如下對你有用:
/*棧的基本操作*/
#
define
stacksize
100
/*定義棧的最大存儲空間*/
#
define
LEN
sizeof(struct
stack)
static
size=0;
struct
stack
{
int
data;
int
*top[stacksize];
};
struct
stack
*sqstack;
struct
stack
*s;
static
e;
int
push()
/*將元素壓入棧*/
{
if
(size<=stacksize)
*
sqstack->top[size++]=e;
else
printf
("
the
stack
is
full:");
}
int
pop(struct
stack
*sqstack,int
location)
/*元素出棧*/
{
e=*(sqstack->top[location]);
return
(e);
}
main()
{
int
n,i,t,x=0;
int
element;
printf
(
"\n
create
the
stack
first
:");
scanf
("%d
",&n);
for
(i=1;i<=n;i++)
{
scanf
("%d",&e);
push
(e);
}
s=sqstack;
t=size;
printf
("\n
after
pushed
,
the
sqstack
is
:");
while
(t>=0)
{
*s->top[t]=*sqstack->top[t];
t--;
}
t=size;
while
(t!=0)
{
t--;
e=pop(s,t);
printf
("
%d->",e);
}
printf
("\n
which
element
you
want
to
pop
:");
scanf
("%d",&element);
while
(size!=0)
{
e=pop(sqstack,size--);
if
(element==e)
{
printf
("\n
%d
is
poped",element);
x=1;
}
}
if(x==0)
printf
("\n
%d
is
not
found
in
the
sqstack.\n",element);
}
❺ c語言 用外部變數實現棧
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define
STACK_SIZE
100
//定義了堆棧的大小
int
contents[STACK_SIZE];
//將堆棧定義為數組
int
top
=
0;
//棧頂為數組元素零,top是棧頂指針。
void
make_empty{void)
//設置棧為空,就是棧頂為數組元素零{
top
=
0;}
bool
is_empty(void)
//返回堆棧是否為空。
{
return
top
==
0;
}
bool
is_full(void)
//如果棧頂為STACK_SIZE,就是棧滿了,本函數返回是否滿
{
return
top
==
STACK_SIZE;
}
void
push(int
i)
//將i壓棧操作,如果滿了,就要調用溢出處理函數,否則將i加入棧頂並把棧頂指針加1
{
if
(is_full())
stack_overflow();
else
contents[top++]
=
i;
}
int
pop(void)
//出棧操作,如果棧空,則調用下溢處理程序。否則返回棧頂的內容,並將棧頂指針減1
{
if
(is_empty())
stack_underflow();
else
return
contents
[--top];
}
❻ C語言動態棧編寫
堆棧當然是動態分配的。
#define MAXNUM 888
/* 定義棧結構 -順序棧 */
typedef struct
{
int stack[MAXNUM]; /* 循序棧 */
int top; /* 棧指針 */
}STACK, * PSTACK;
/* 棧的初始化 */
int init_stack(PSTACK head)
{
head->top = -1;
return 0;
}
/* 入棧 */
int push_stack(PSTACK head, int x)
{
if(head->top >= MAXNUM-1)/* 棧滿, 無法入 */
return 0;
head->stack[++head->top] = x;
return 1;
}
/* 出棧 */
int pop_stack(PSTACK head)
{
if(head->top < 0)/* 空棧 */
return 0;
return head->stack[head->top--];
}
/* 取棧頂元素 */
int get_stack(PSTACK head)
{
if(head->top < 0)/* 空棧 */
return 0;
return head->stack[head->top];
}
❼ C語言怎麼用指針實現棧
設置一個頭結點
入棧操作就是將數據插入在原頭結點前面,生成一個新的頭結點,出棧操作就是刪除頭結點,下一個結點作為新的頭結點
❽ 棧的c語言實現基本操作
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#definestack_init_size20
#defineincreasesize10;
typedefintElemType;
typedefstructnode{
ElemType*base;
ElemType*top;
intsize;
}stack;
voidcreat(stack*s)
{
s->base=(ElemType*)malloc(sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->base=s->top;
s->size=stack_init_size;
}
voidpush(stack*s,ElemTypee)
{
if(s->top-s->base>=s->size)
{
s->base=(ElemType*)realloc(s->base,(s->size+increasesize)*sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->top=s->base+s->size;
s->size+=increasesize;
}
*(s->top)=e;
s->top++;
}
voidpop(stack*s,ElemType*e)
{
if(s->top==s->base)
{
return;
}
*e=*--(s->top);
}
intstacklen(stacks)
{
return(s.top-s.base);
}
intmain()
{
stacks;
intn;
ElemTypee1,e2,d;
inti=1,j=1;
push(&s,i);
push(&s,j);
scanf("%d",&n);
while(n--)
{
pop(&s,&e1);
pop(&s,&e2);
d=e1+e2;
push(&s,e2);
push(&s,d);
}
pop(&s,&d);
printf("%d",d);
return0;
}
❾ C語言棧的簡單實現
#include<stdio.h>
#include<malloc.h>
//enum
bool
{false,true};
typedef
struct
Node{
int
a;
int
Number;
//在棧中的序號,棧底為0
struct
Node
*next;
}Node,*LpNode;
typedef
struct
SqStack{
Node
*top;
Node
*prev;
Node
*base;
int
length;
}*LpSqStack;
//將e的能容復制到S中並將e摧毀
bool
Node_evaluation(LpNode
S,LpNode
e,bool
M)
{
//賦值操作
//S->Number
=
e->Number;
if(M
==
true)
free(e);
return
true;
}
bool
InitStack(LpSqStack
S)
{
S->length
=
0;
S->base
=
(LpNode)malloc(sizeof(Node));
if(!S->base)
return
false;
S->top
=
S->base;
S->prev
=
S->base;
S->base->Number
=
0;
return
true;
}
bool
StackEmpty(LpSqStack
S)
{
if(S->top
!=
S->base)
return
false;
return
true;
}
bool
GetTop(LpSqStack
S,LpNode
e)
{
if(S->top
==
S->base)
return
false;
e
=
S->top;
return
true;
}
bool
Push(LpSqStack
S,LpNode
e)
{
if(!Node_evaluation(S->top,e,true))
return
false;
S->top->Number
=
S->prev->Number
+
1;
S->prev
=
S->top;
S->top
=
(LpNode)malloc(sizeof(Node));
S->prev->next
=
S->top;
S->top->next
=
NULL;
return
true;
}
bool
Pop(LpSqStack
S,LpNode
e)
{
if(S->top
==
S->base)
return
false;
if(!Node_evaluation(e,S->top,true))
return
false;
S->top
=
S->prev;
S->top->next
=
NULL;
return
true;
}
bool
Vistit(LpSqStack
S,LpNode
e,int
i)
{
LpNode
p;
p
=
S->base;
for(int
j
=
0;
j
=
i;
j++)
{
if(p->next
==
NULL)
return
false;
p
=
p->next;
}
if(!Node_evaluation(p,e,false))
return
false;
return
true;
}
int
main()
{
SqStack
a;
InitStack(&a);
LpNode
b=new
Node;
LpNode
c=new
Node;
LpNode
d=new
Node;
//free(&b);這free了你下面又賦值。。。
b->a=1;
Push(&a,c);
GetTop(&a,c);
printf("%d",c->a);
return
0;
}
棧里的內存是不能free的,你要free你就自己在堆里分配。
❿ c語言實現進棧程序的全過程
/**************
sstack.h
***************/
/* 順序棧表示:類型和界面函數聲明 */
enum { MAXNUM = 20 /* 棧中最大元素個數,應根據需要定義 */
};
typedef int DataType; /* 棧中元素類型,應根據需要定義 */
struct SeqStack { /* 順序棧類型定義 */
int t; /* 棧頂位置指示 */
DataType s[MAXNUM];
};
typedef struct SeqStack SeqSack, *PSeqStack; /* 順序棧類型和指針類型 */
/*創建一個空棧;為棧結構申請空間,並將棧頂變數賦值為-1*/
PSeqStack createEmptyStack_seq( void );
/*判斷pastack所指的棧是否為空棧,當pastack所指的棧為空棧時,則返回1,否則返回0*/
int isEmptyStack_seq( PSeqStack pastack );
/* 在棧中壓入一元素x */
void push_seq( PSeqStack pastack, DataType x );
/* 刪除棧頂元素 */
void pop_seq( PSeqStack pastack );
/* 當pastack所指的棧不為空棧時,求棧頂元素的值 */
DataType top_seq( PSeqStack pastack );
/**************
sstack.c
***************/
/* 順序棧表示:函數定義 */
#include <stdio.h>
#include <stdlib.h>
#include "sstack.h"
/*創建一個空棧;為棧結構申請空間,並將棧頂變數賦值為-1*/
PSeqStack createEmptyStack_seq( void ) {
PSeqStack pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
if (pastack==NULL)
printf("Out of space!! \n");
else
pastack->t = -1;
return pastack;
}
/*判斷pastack所指的棧是否為空棧,當pastack所指的棧為空棧時,則返回1,否則返回0*/
int isEmptyStack_seq( PSeqStack pastack ) {
return pastack->t == -1;
}
/* 在棧中壓入一元素x */
void push_seq( PSeqStack pastack, DataType x ) {
if( pastack->t >= MAXNUM - 1 )
printf( "Stack Overflow! \n" );
else {
pastack->t++;
pastack->s[pastack->t] = x;
}
}
/* 刪除棧頂元素 */
void pop_seq( PSeqStack pastack ) {
if (pastack->t == -1 )
printf( "Underflow!\n" );
else
pastack->t--;
}
/* 當pastack所指的棧不為空棧時,求棧頂元素的值 */
DataType top_seq( PSeqStack pastack ) {
return pastack->s[pastack->t];
}