本文目錄一覽:
- 1、棧的基本操作的實現(c語言),高手速來!!
- 2、數據結構實驗(用c語言寫) 棧的基本操作
- 3、用C語言實現棧的基本操作(數制的轉換)
- 4、c語言 棧的操作
- 5、棧的c語言實現基本操作
- 6、棧的基本操作的C語言程序
棧的基本操作的實現(c語言),高手速來!!
/*程序錯誤太多*/ #include”stdio.h”
#include”stdlib.h”
#include”time.h”
#include”malloc.h”
#define
STACK_INIT_SIZE
10
//棧容量 typedef
struct
SqStack
{
int
top;
//棧頂當前指針
int
*base;
//棧空間數組
}SqStack; void
InitStack(SqStack
S);
//構造空棧S
int
Push(SqStack
S,int
e);
//入棧(棧地址,入棧數據)
返回0對,-1錯
int
Pop(SqStack
S);
//出棧
(棧地址)返回棧頂數據
int
StackLength(SqStack
S);
//返回站的元素個數,即求棧長
void
Print_S(SqStack
S);
//顯示棧內數據 int
main()
{
SqStack
S;
int
i=0;
int
a,e;
InitStack(S);
srand((unsigned)time(NULL));
//srand((unsigned)time(NULL))以time函數值(當前時間)作為種子
printf(“隨機填充5個元素為:
“);
while(
i5)
{
a
=
rand()%100;
printf(“%d
“,
a);
Push(S,a);
i++;
}
Print_S(S);
printf(“請輸入要插入棧頂的元素:”);
scanf(“%d”,e);
Push(S,e);
Print_S(S);
printf(“再彈出的棧頂元素為:%d
\n”,Pop(S));
printf(“棧的長度為:%d
\n”,StackLength(S));
Print_S(S);
return
0;
} void
InitStack(SqStack
S)
//構造空棧S
{
S.base
=
(int
*)malloc(STACK_INIT_SIZE
*
sizeof(int));
//分配組數空間,長度STACK_INIT_SIZE
if
(S.base==NULL)
{
printf(“內存分配失敗!\n”);
return;
}
S.top=-1;
} int
Push(SqStack
S,int
e)
{
if(S.top=STACK_INIT_SIZE)
{
printf(“棧空間已滿,入棧失敗!\n”);
return
-1;
}
else
{
S.base[++S.top]=e;
return
0;
}
} int
Pop(SqStack
S)
//返回棧頂數據
{
if
(S.top=0)
//棧內有數據
{
return
S.base[S.top–];
}
else
{
printf(“空棧,無數據彈出!\n”);
return
-1;
}
} int
StackLength(SqStack
S)
{
return
S.top+1;
} void
Print_S(SqStack
S)
{
printf(“\n出棧顯示:”);
if(S.top
==
-1)
printf(“棧內無數據!\n”);
else
{
while(S.top=0
)
printf(“%d
“,Pop(S));
putchar(‘\n’);
}
}
數據結構實驗(用c語言寫) 棧的基本操作
//順序棧
#include
#include
#include
#define
STACK_INIT_SIZE
100;
#define
STACKINCREMENT
10;
typedef
struct
{
int
*base;
int
*top;
int
stacksize;
}SqStack;
typedef
int
ElemType;
int
InitStack(SqStack
S)
//為棧S分配存儲空間,並置S為空棧
{
int
size
=
STACK_INIT_SIZE;
S.base=(int
*)malloc(size*sizeof(ElemType));
if(!S.base)
return
0;
S.top=S.base;
//置棧S為空棧
S.stacksize=STACK_INIT_SIZE;
return
1;
}
int
GetTop(SqStack
S,int
e)
//若棧不空,則用e返回S的棧頂元素
{
if(S.top==S.base)
return
0;
e=*(S.top-1);
return
1;
}
int
Push(SqStack
S,
int
e)
/*進棧函數,將e插入棧S中,並使之成為棧頂元素*/
{
if(S.top-S.base=S.stacksize)
/*棧滿,追加存儲空間*/
{
int
stackinvrement
=
STACKINCREMENT;
S.base=(ElemType
*)
realloc(S.base,(S.stacksize+stackinvrement)*sizeof(ElemType));
if(!S.base)
return
0;
/*存儲分配失敗*/
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return
1;
}
int
Pop(SqStack
S,int
e)/*出棧函數,若棧S不空,則刪除S的棧頂元素,用e返回其值*/
{
if(S.top==S.base)
return
0;
e=*–S.top;
return
1;
}
void
OutputStack(SqStack
S)
{int
*q;
q=S.top-1;
for(int
i=0;i
#include
typedef
struct
SNode
{
int
data;
struct
SNode
*next;
}SNode,*LinkStack;
LinkStack
top;
LinkStack
PushStack(LinkStack
top,int
x)
//入棧
{
LinkStack
s;
s=(LinkStack)malloc(sizeof(SNode));
s-data=x;
s-next=top;
top=s;
return
top;
}
LinkStack
PopStack(LinkStack
top)
//退棧
{
LinkStack
p;
if(top!=NULL)
{
p=top;
top=top-next;
free(p);
printf(“退棧已完成\n”);
return
top;
}
else
printf(“棧是空的,無法退棧!\n”);
return
0;
}
int
GetStackTop(LinkStack
top)
//取棧頂元素
{
return
top-data;
}
bool
IsEmpty()//bool取值false和true,是0和1的區別,bool只有一個位元組,BOOL為int型,bool為布爾型
{
return
top==NULL
?
true:false;
}
void
Print()
{
SNode
*p;
p=top;
if(IsEmpty())
{
printf(“The
stack
is
empty!\n”);
return;
}
while(p)
{
printf(“%d
“,
p-data);
p=p-next;
}
printf(“\n”);
}
void
main()
{
int
x,a,b;
char
m;
do
{
printf(“\n”);
printf(“###############鏈棧的基本操作##################\n”);
printf(“××××××××1.置空棧××××××××××\n”);
printf(“××××××××2.進棧×××××××××××\n”);
printf(“××××××××3.退棧×××××××××××\n”);
printf(“××××××××4.取棧頂元素××××××××\n”);
printf(“××××××××5.退出程序×××××××××\n”);
printf(“##############################################\n”);
printf(“\n請選擇一個字符:”);
scanf(“%c”,m);
switch(m){
case
‘1’:
top=NULL;
printf(“\n棧已置空!”);
break;
case
‘2’:
printf(“\n請輸入要進棧的元素個數是:”);
scanf(“%d”,a);
printf(“\n請輸入要進棧的%d個元素:”,a);
for(b=0;b
評論
加載更多
用C語言實現棧的基本操作(數制的轉換)
//順序棧以及基本操作如下:
#includeiostream.h
enum
{
MAX_SIZE=20
};
typedef struct
{
int* base;
int* top;
int stacksize;
}SqStack;
void InitStack(SqStack S)
{
S.base=new int[MAX_SIZE];
S.top=S.base;
S.stacksize=MAX_SIZE;
}
bool Push(SqStack S,int e)
{
if(S.top-S.base=S.stacksize)
return false;
*S.top=e;
S.top++;
return true;
}
bool Pop(SqStack S,int e)
{
if(S.top==S.base)
return false;
S.top–;
e=*S.top;
return true;
}
void DestroyStack(SqStack S)
{
if(S.base)
delete S.base;
S.top=S.base=NULL;
S.stacksize=0;
}
bool StackEmpty(SqStack S)
{
return (S.top==S.base);
}
void Print(SqStack S)
{
int i=0;
while(iS.top-S.base)
{
coutS.base[i++]endl;
}
}
c語言 棧的操作
#include
#include
#define Max 100
typedef char T;
typedef struct MyStack
{
T aa[Max];
unsigned int p;
} stack;
//創建空棧
stack* createEmptyStack()
{
stack* st = (stack *)malloc(sizeof(stack));
int i=0;
for(i=0;iMax;i++)
st-aa[i]=0;
st-p=0;
return st;
};
//棧判空
int isEmpty(const stack* st)
{
if(st-p==0) return 1;
else return 0;
};
//求棧的大小
unsigned int size(const stack* st)
{
return st-p;
};
//push操作
void push(stack* st,const T a)
{
st-p=st-p+1;
if(st-p==Max)
{
printf(“棧滿\n”);
st-p–;
return;
}
st-aa[st-p]=a;
};
//pop操作
T pop(stack* st)
{
if(isEmpty(st))
{
printf(“棧空”);
return NULL;
}
char t=st-aa[st-p];
st-p=st-p-1;
printf(“%c “,t);
return t;
};
//棧銷毀
void destroy(stack* st)
{
free(st);
};
int main()
{
stack* st = createEmptyStack();
if(isEmpty(st)) printf(“MyStack is empty\n”);
else printf(“MyStack is not empty\n”);
push(st,’a’);
push(st,’b’);
push(st,’c’);
push(st,’d’);
push(st,’e’);
printf(“%d\n”,size(st));
while(!isEmpty(st)) pop(st);
destroy(st);
system(“pause”);
return 0;
}
棧的c語言實現基本操作
寫了一個鏈式棧,你看看
# include stdio.h
# include malloc.h
# include stdlib.h
typedef struct Node
{
int data;
struct Node *pNext;
}NODE, *PNODE;
typedef struct Stack
{
PNODE pTop;
PNODE pBottom;//pBottem是指向棧底下一個沒有實際意義的元素
}STACK, *PSTACK;
void init( PSTACK );
void push( PSTACK, int );
void traverse( PSTACK );
int pop( PSTACK, int * );
int empty( PSTACK pS );
int main( void )
{
STACK S;//STACK等價於struct Stack
int val;
init( S );//目的是造出一個空棧
push( S, 1 );//壓棧
push( S, 2 );
push( S, 3 );
push( S, 4 );
push( S, 5 );
push( S, 6 );
push( S, 7 );
traverse( S );//遍歷輸出
// clear( S ); //清空數據
// traverse( S );//遍歷輸出
if( pop( S, val ) )
{
printf( “出棧成功,出棧的元素是%d\n”, val );
}
else
{
printf( “出棧失敗” );
}
traverse( S );//遍歷輸出出棧之後的元素
return 0;
}
void init( PSTACK pS )
{
pS-pTop = ( PNODE )malloc( sizeof( NODE ) );
if( NULL == pS-pTop )
{
printf( “動態內存分配失敗!\n” );
exit( -1 );
}
else
{
pS-pBottom = pS-pTop;
pS-pTop-pNext = NULL;//或是pS-pBottom = NULL;
}
}
void push( PSTACK pS, int val )
{
PNODE pNew = ( PNODE )malloc( sizeof( NODE ) );
pNew-data = val;
pNew-pNext = pS-pTop;//pS-Top不能改為pS-pBottom
pS-pTop = pNew;
}
void traverse( PSTACK pS )
{
PNODE p = pS-pTop;
while( p != pS-pBottom )
{
printf( “%d “, p-data );
p = p-pNext;
}
printf( “\n” );
}
int empty( PSTACK pS )
{
if( pS-pTop == pS-pBottom )
return 1;
else
return 0;
}
//把pS所指向的棧出棧一次,並把出棧的元素存入pVal形參所指向的變量中,如果出棧失敗,則返回false,否則true
int pop( PSTACK pS, int *pVal)
{
if( empty( pS ) )//pS本身存放的就是S的地址
{
return 0;
}
else
{
PNODE r = pS-pTop;
*pVal = r-data;
pS-pTop = r-pNext;
free( r );
r = NULL; //為什麼要把r賦給NULL呢??
return 1;
}
}
//clear清空
void clear( PSTACK pS )
{
if( empty( pS ) )
{
return ;
}
else
{
PNODE p = pS-pTop;
PNODE q = p-pNext;
while( p != pS-pBottom )
{
q = p-pNext;
free( p );
p = q;
}
pS-pTop = pS-pBottom;
}
}
棧的基本操作的C語言程序
#include stdio.h
#include stdlib.h
#define MAX 1024 ///棧使用數組模擬,MAX是最大元素個數
typedef int DataType; ///數據域使用整形
typedef struct _stack
{
DataType data[MAX]; ///存放數據
int top; ///棧頂指針
}stack;
///初始化
int initStack(stack (*s))
{
return emptyStack(s);
}
///數據壓棧,成功返回1,失敗返回0
int push(stack (*s), DataType d)
{
if ((*s).top = MAX – 1) //棧已滿
{
printf(“棧已滿,不能壓棧\n”);
return 0;
}
//數據壓棧
(*s).top++;
(*s).data[(*s).top] = d;
return 1;
}
///數據出棧,成功返回1,d指向的區域存儲彈出的數據,失敗返回0
int pop(stack (*s), DataType *d)
{
if ((*s).top = -1)
{
printf(“棧為空,不能出棧\n”);
return 0;
}
//出棧
*d = (*s).data[(*s).top];
(*s).top–;
return 1;
}
///清空棧
int emptyStack(stack (*s))
{
(*s).top = -1;
return 1;
}
int main(int argc, char** argv)
{
stack s;
int i, d;
initStack(s);
//壓棧
for (i = 0; i 1025; i++)
{
push(s, i);
}
//清空
emptyStack(s);
for (i = 0; i 10; i++)
{
push(s, i);
}
//出棧
for (i = 0; i 11; i++)
{
pop(s, d);
printf(“%d\n”, d);
}
return 0;
}
原創文章,作者:EOKP,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/149413.html