#include"stdlib.h"
#include"time.h"
#include"malloc.h"
#define
STACK_INIT_SIZE
10
//栈容量 typedef
struct
SqStack
{
int
top
//栈顶当前指针
int
*base
//栈空间数组
}SqStackvoid
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(
i<5)
{
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出的栈顶元素为:%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("空栈,无数据d出!\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')
}
}
#define STACK_SIZE 100#define PUSH_POP_SUCCESS 1
#define PUSH_POP_ERROR 0
struct _stackbuf {
int _collection[STACK_SIZE]
int _top
}
typedef struct _stackbuf S_STACK
typedef unsigned int u_int_f
// 入栈
u_int_f push(S_STACK *stack, int d){
if (stack->_top >= STACK_SIZE) return PUSH_POP_ERROR
stack->_collection[stack->_top++] = d
return PUSH_POP_SUCCESS
}
// 出栈
u_int_f pop(S_STACK *stack, int *e){
if (!stack->_top) return PUSH_POP_ERROR
*e=stack->_collection[--(stack->_top)]
return PUSH_POP_SUCCESS
}
int main(){
S_STACK stack = { {0},0 }
push(&stack, 1)
push(&stack, 2)
push(&stack, 3)
int gv = 0
pop(&stack, &gv)
printf("%d\n", gv)
system("PAUSE")
return 0
}
如下:
#include "stdio.h"struct stackNode{
int data
struct stackNode *nextPtr
}
typedef struct stackNode LISTSTACK
typedef LISTSTACK *STACKNODEPTR
void push(STACKNODEPTR *,int)
int pop(STACKNODEPTR *)
int isEmpty(STACKNODEPTR)
void printStack(STACKNODEPTR)
void instruct()
int main()
{
int item
int choice
STACKNODEPTR sPtr=NULL
instruct()
printf("choose your choice\n")
scanf("%d",&choice)
while(choice!=3)
{
switch(choice)
{
case 1:
printf("please input an integer!\n")
scanf("%d",&item)
//printf("%d\n",item)
push(&sPtr,item)
printStack(sPtr)
break
case 2:
if(!isEmpty(sPtr))
{
printf("deleting element of top stack\n")
pop(&sPtr)
printStack(sPtr)
}
else{
printf("no element in the stack\n")
}
break
default:
printf("invalid input,check your input!\n")
break
}
printf("pleace choose your choice ")
instruct()
scanf("%d",&choice)
}
}
void instruct()
{
printf("Following the instruction below:\n"
"1:insert new elment into the stack\n"
"2:delete the top element of the stack\n"
"3:to end of run\n")
}
int isEmpty(STACKNODEPTR sPtr)
{
return sPtr==NULL
}
void printStack(STACKNODEPTR sPtr)
{
if(sPtr==NULL)
{
printf("The stack is empty!\n")
}
else{
printf("The elements of the stack:\n")
while(sPtr!=NULL)
{
printf("%d-->",sPtr->data)
sPtr=sPtr->nextPtr
}
printf("NULL\n\n")
}
}
void push(STACKNODEPTR *topPtr,int value)
{
STACKNODEPTR newPtr
newPtr=malloc(sizeof(STACKNODEPTR))
if(newPtr!=NULL)
{
newPtr->data=value
newPtr->nextPtr=*topPtr
*topPtr=newPtr
}
else
{
printf("%d is not inserted into stack.No memory is availiable\n")
}
}
int pop(STACKNODEPTR *topPtr)
{
STACKNODEPTR newPtr
int topValue
newPtr=*topPtr
*topPtr=(*topPtr)->nextPtr
free(newPtr)
topValue=(*topPtr)->data
printf("deleting--- %d\n",topValue)
return topValue
}
数据结构:
是计算机存储、组织数据的方式。数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率。数据结构往往同高效的检索算法和索引技术有关。
常用数据结构:
数组 (Array)、栈 (Stack)、队列 (Queue)、链表 (Linked List)、树 (Tree)、图 (Graph)、堆 (Heap)、散列表 (Hash)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)