Skip to content

Commit e8ac0d9

Browse files
Merge pull request #61 from jin13417/master
jinshaohui commit array stack.
2 parents d98625b + 8d5ebed commit e8ac0d9

File tree

8 files changed

+722
-0
lines changed

8 files changed

+722
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*************************************************************************
2+
> File Name: arrayStack.c
3+
> Author: jinshaohui
4+
5+
> Time: 18-10-12
6+
> Desc: 数组实现顺序栈
7+
************************************************************************/
8+
#include<stdio.h>
9+
#include<stdlib.h>
10+
#include<string.h>
11+
#include"./arrayStack.h"
12+
13+
/*创建并初始化顺序栈*/
14+
stArrayStack * arrayStack_create(int size)
15+
{
16+
stArrayStack *parrStack = NULL;
17+
18+
parrStack = (stArrayStack *)malloc(sizeof(stArrayStack));
19+
if (parrStack == NULL)
20+
{
21+
return NULL;
22+
}
23+
24+
parrStack->size = size;
25+
parrStack->pos = -1;
26+
parrStack->array = (int *)malloc(sizeof(int)*size);
27+
if(parrStack->array == NULL)
28+
{
29+
free(parrStack);
30+
return NULL;
31+
}
32+
33+
return parrStack;
34+
}
35+
/*销毁顺序栈*/
36+
void arrayStack_destory(stArrayStack * parrStack)
37+
{
38+
if(parrStack == NULL)
39+
{
40+
return;
41+
}
42+
43+
if (parrStack->array != NULL)
44+
{
45+
free(parrStack->array);
46+
}
47+
48+
free(parrStack);
49+
return;
50+
}
51+
/*出栈*/
52+
int arrayStack_pop(stArrayStack *parrStack)
53+
{
54+
int data = 0;
55+
56+
if(arrayStack_is_empty(parrStack))
57+
{
58+
return -1;
59+
}
60+
data = parrStack->array[parrStack->pos];
61+
parrStack->pos--;
62+
63+
return data;
64+
}
65+
/*入栈*/
66+
int arrayStack_push(stArrayStack *parrStack,int data)
67+
{
68+
if(arrayStack_is_full(parrStack))
69+
{
70+
return -1;
71+
}
72+
73+
parrStack->pos++;
74+
parrStack->array[parrStack->pos] = data;
75+
76+
return 0;
77+
}
78+
79+
int arrayStack_push_new(stArrayStack*parrStack,int data)
80+
{
81+
int *ptmp = NULL;
82+
83+
/*如果栈不满,直接插入*/
84+
if(!arrayStack_is_full(parrStack))
85+
{
86+
return arrayStack_push(parrStack,data);
87+
}
88+
89+
/*如果栈已经满,申请内存*/
90+
ptmp = (int *)malloc(2*parrStack->size*sizeof(int));
91+
if (ptmp == NULL)
92+
{
93+
return -1;
94+
}
95+
96+
memcpy(ptmp,parrStack->array,parrStack->size*sizeof(int));
97+
98+
free(parrStack->array);
99+
100+
parrStack->array = ptmp;
101+
parrStack->size = 2*parrStack->size;
102+
parrStack->pos++;
103+
parrStack->array[parrStack->pos] = data;
104+
105+
return ;
106+
}
107+
108+
void arrayStack_dump(stArrayStack *parrStack)
109+
{
110+
int i = 0;
111+
112+
if (arrayStack_is_empty(parrStack))
113+
{
114+
printf("\r\n arrayStack is empty.");
115+
return;
116+
}
117+
printf("\r\narrayStack size = %d,pos= %d,",
118+
parrStack->size,parrStack->pos);
119+
for(i = 0; i <= parrStack->pos; i++)
120+
{
121+
printf("\r\narry[%d] = %d",i,parrStack->array[i]);
122+
}
123+
}
124+
125+
int main()
126+
{
127+
int i = 0;
128+
int ret = 0;
129+
stArrayStack * parrStack = NULL;
130+
131+
printf("\r\n create size = 4 arrayStack.");
132+
133+
parrStack = arrayStack_create(4);
134+
if (parrStack == NULL)
135+
{
136+
printf("\r\n create size = 4 arrayStack faided.");
137+
return 0;
138+
}
139+
140+
for (i = 0; i < 5; i++)
141+
{
142+
ret = arrayStack_push(parrStack,i);
143+
if(ret != 0)
144+
{
145+
printf("\r\n push size = %d arrayStack faided.",i);
146+
147+
}
148+
}
149+
arrayStack_dump(parrStack);
150+
151+
ret = arrayStack_push_new(parrStack,4);
152+
if(ret != 0)
153+
{
154+
printf("\r\n push size = %d arrayStack faided.",4);
155+
}
156+
arrayStack_dump(parrStack);
157+
158+
arrayStack_destory(parrStack);
159+
160+
return;
161+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*************************************************************************
2+
> File Name: arrayStack.h
3+
> Author: jinshaohui
4+
5+
> Time: 18-10-12
6+
> Desc:
7+
************************************************************************/
8+
9+
#ifndef ARRAY_STACJ_H
10+
#define ARRAY_STACJ_H
11+
12+
typedef struct _array_stack
13+
{
14+
int size;/*栈的大小*/
15+
int pos;/*当前存储元素的个数,即栈顶元素下表*/
16+
int *array;/*数据存储区*/
17+
}stArrayStack;
18+
19+
#define arrayStack_size(arrayStack) (arrayStack->size)
20+
#define arrayStack_is_empty(arrayStack) (arrayStack->pos == -1)
21+
#define arrayStack_is_full(arrayStack) (arrayStack->pos == (arrayStack->size-1))
22+
23+
#endif
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*************************************************************************
2+
> File Name: linklist_stack.c
3+
> Author: jinshaohui
4+
5+
> Time: 18-10-12
6+
> Desc:
7+
************************************************************************/
8+
#include<stdio.h>
9+
#include<stdlib.h>
10+
#include<string.h>
11+
#include "./linklist_stack.h"
12+
13+
linklist_stack * stack_create()
14+
{
15+
linklist_stack * stack = NULL;
16+
17+
stack = (linklist_stack *)malloc(sizeof(linklist_stack));
18+
if (stack == NULL)
19+
{
20+
return NULL;
21+
}
22+
23+
stack->next = NULL;
24+
25+
return stack;
26+
}
27+
28+
void stack_destory(linklist_stack* stack)
29+
{
30+
linklist_stack * ptmp = NULL;
31+
32+
while(!stack_is_empty(stack))
33+
{
34+
ptmp = stack->next;
35+
stack->next = stack->next->next;
36+
37+
free(ptmp);
38+
}
39+
40+
free(stack);
41+
42+
return;
43+
}
44+
45+
int stack_push(linklist_stack *stack,int data)
46+
{
47+
linklist_stack * ptmp = NULL;
48+
49+
ptmp = (linklist_stack *)malloc(sizeof(linklist_stack));
50+
if (ptmp == NULL)
51+
{
52+
return -1;
53+
}
54+
55+
ptmp->data = data;
56+
ptmp->next = stack->next;
57+
stack->next = ptmp;
58+
59+
return 0;
60+
}
61+
62+
int stack_pop(linklist_stack*stack,int *data)
63+
{
64+
linklist_stack *ptmp = NULL;
65+
if (data == NULL)
66+
{
67+
return -1;
68+
}
69+
if(stack_is_empty(stack))
70+
{
71+
return -1;
72+
}
73+
*data = stack->next->data;
74+
ptmp = stack->next;
75+
stack->next = ptmp->next;
76+
free(ptmp);
77+
78+
return 0;
79+
}
80+
81+
82+
void stack_dump(linklist_stack *stack)
83+
{
84+
linklist_stack * ptmp = stack->next;
85+
86+
while(ptmp != NULL)
87+
{
88+
printf("\r\n data = %d",ptmp->data);
89+
ptmp = ptmp->next;
90+
}
91+
return;
92+
}
93+
94+
int main()
95+
{
96+
int i = 0;
97+
int ret = 0;
98+
int data = 0;
99+
linklist_stack * stack = NULL;
100+
101+
stack = stack_create();
102+
if (stack == NULL)
103+
{
104+
printf("\r\n stack create falied.");
105+
return 0;
106+
}
107+
108+
for (i = 0; i < 4; i++)
109+
{
110+
ret = stack_push(stack,i);
111+
if(ret != 0)
112+
{
113+
printf("\r\n stack push %d falied.",i);
114+
}
115+
}
116+
117+
stack_dump(stack);
118+
119+
for (i = 0; i < 5; i++)
120+
{
121+
ret = stack_pop(stack,&data);
122+
if(ret != 0)
123+
{
124+
printf("\r\n stack pop%d falied.", i);
125+
}
126+
else
127+
{
128+
printf("\r\n data = %d,",data);
129+
}
130+
}
131+
132+
stack_destory(stack);
133+
134+
return 0;
135+
136+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*************************************************************************
2+
> File Name: linklist_stack.h
3+
> Author: jinshaohui
4+
5+
> Time: 18-10-12
6+
> Desc:
7+
************************************************************************/
8+
9+
#ifndef STACK_LINK_LIST_H
10+
#define STACK_LINK_LIST_H
11+
12+
typedef struct _linkliststack
13+
{
14+
int data;
15+
struct _linkliststack *next;
16+
}linklist_stack;
17+
18+
19+
#define stack_is_empty(liststack) (liststack->next == NULL)
20+
21+
#endif
22+

0 commit comments

Comments
 (0)