Создать два стека на одном массиве - C (СИ)
Формулировка задачи:
Задание такое: при реализации стека массивом располагать два стека в одном массиве. Один стек располагается в начале массива и растет к концу, а другой располагается в конце массива и растет к началу.
Задание я понял не очень, но что-то попытался реализовать, можете объяснить мне задпние подробнее.Вот, до чего додумался
Листинг программы
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_SIZE 1
- #define MULT 1
- #define STACK_OVERFLOW -100
- #define STACK_UNDERFLOW -101
- #define OUT_OF_MEMORY -102
- typedef struct Stack_tag
- {
- int *data;
- int size;
- int top;
- }stack_t, stack_d;
- //------------------------------stack_1-----------------------------
- stack_t *creat_stack()
- {
- stack_t *out = NULL;
- out = malloc(sizeof(stack_t));
- if(!out)
- exit(OUT_OF_MEMORY);
- out->size = MAX_SIZE;
- out->data = malloc(out->size*sizeof(int));
- if (out->data==NULL)
- {
- free(out);
- exit(OUT_OF_MEMORY);
- }
- out->top = 0;
- return out;
- }
- void delete_stack(stack_t **stack)
- {
- free((*stack)->data);
- free(*stack);
- *stack = NULL;
- }
- void resize(stack_t *stack)
- {
- stack->size += MULT;
- stack->data = realloc(stack->data,stack->size*sizeof(int));
- if (stack->data == NULL)
- exit(OUT_OF_MEMORY);
- }
- //-----------------------------------------------------------------------------
- void push(stack_t *stack, int value)
- {
- if (stack->top >= stack->size)
- resize(stack);
- stack->data[stack->top]= value;
- stack->top++;
- }
- void push_1(stack_d *stack, int value)
- {
- if (stack->top >= stack->size)
- resize(stack);
- stack->data[stack->top]= value;
- stack->top--;
- }
- int pop(stack_t *stack)
- {
- if(stack->top == 0)
- exit(STACK_UNDERFLOW);
- stack->top--;
- return (stack->data[stack->top]);
- }
- void print_stack_value(const int value)
- {
- printf("%d", value);
- }
- void print_stack(const stack_t *stack, void(*print_stack_value)(const int))
- {
- int len=stack->size-1;
- int i;
- for(i = len; i > 0; i--)
- {
- print_stack_value(stack->data[i]);
- printf(" | ");
- }
- if (stack->size != 0)
- {
- print_stack_value(stack->data[i]);
- }
- printf("\n");
- }
- int main()
- {
- stack_t *stack_1 = creat_stack();
- stack_t *stack_2 = creat_stack();
- int a;
- printf("Input quantyty items: ");
- scanf("%d",&a);
- push(stack_1, a);
- print_stack(stack_1, print_stack_value);
- printf("Input quantyty items: ");
- scanf("%d",&a);
- push_1(stack_2, a);
- print_stack(stack_2, print_stack_value);
- return 0;
- }
Вот, переделал, теперь что-то похожее. 2 стека в одном массиве, один растет от начала массива к концу, а другой от конца к началу, только вот когда кладу элементы в первый стек, вроде все нормально. а когда во второй, то ничего не работает, почему?
Листинг программы
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_SIZE 10
- #define MULT 1
- #define STACK_OVERFLOW -100
- #define STACK_UNDERFLOW -101
- #define OUT_OF_MEMORY -102
- typedef struct Stack_tag
- {
- int data[MAX_SIZE];
- int size;
- int top1, top2;
- }stack_t;
- //------------------------------stack_1-----------------------------
- void push(stack_t *stack, int value)
- {
- stack->data[stack->top1]= value;
- stack->top1++;
- }
- void push_1(stack_t *stack, int value)
- {
- stack->data[stack->top2]= value;
- stack->top2--;
- }
- int pop(stack_t *stack)
- {
- if(stack->top1 == 0)
- exit(STACK_UNDERFLOW);
- stack->top1--;
- return (stack->data[stack->top1]);
- }
- void print_stack_value(const int value)
- {
- printf("%d", value);
- }
- void print_stack_1(const stack_t *stack, void(*print_stack_value)(const int))
- {
- int len_1 = stack -> top1;
- int i = 0;
- for(i = len_1-1; i >= 0; i--)
- {
- print_stack_value(stack->data[i]);
- printf(" | ");
- }
- }
- void print_stack_2(const stack_t *stack, void(*print_stack_value)(const int))
- {
- int len_2 = stack -> top2;
- int j;
- for(j = len_2; j >= 0; j--)
- {
- print_stack_value(stack->data[j]);
- printf(" | ");
- }
- }
- int main()
- {
- int a;
- stack_t stack_1;
- stack_1.top1 = 0;
- stack_1.top2 = MAX_SIZE;
- printf("Input quantyty items: ");
- scanf("%d",&a);
- push(&stack_1, a);
- printf("Input quantyty items: ");
- scanf("%d",&a);
- push(&stack_1, a);
- print_stack_1(&stack_1, print_stack_value);
- printf("Input quantyty items: ");
- scanf("%d",&a);
- push_1(&stack_1, a);
- print_stack_2(&stack_1, print_stack_value);
- return 0;
- }
Решение задачи: «Создать два стека на одном массиве»
textual
Листинг программы
- #include <stdio.h>
- #include <stdlib.h>
- #define DEFAULT_STACK_SIZE (10)
- typedef int data_t;
- #define DATA_MASK "%d"
- typedef struct TWO_WAY_STACK {
- data_t * pData;
- data_t * pHead;
- data_t * pTail;
- size_t nSize;
- } twstack_t;
- twstack_t * twstack_new(size_t size) {
- twstack_t * stk = malloc(sizeof(twstack_t));
- if ( ! stk )
- return NULL;
- if ( ! ( stk->pData = malloc(sizeof(data_t) * size) ) ) {
- free(stk);
- return NULL;
- }
- stk->nSize = size;
- stk->pHead = stk->pData - 1;
- stk->pTail = stk->pData + stk->nSize;
- return stk;
- }
- int twstack_push_head(twstack_t * stk, const data_t data) {
- if ( stk->pHead + 1 >= stk->pTail )
- return -1;
- *(++(stk->pHead)) = data;
- return 0;
- }
- int twstack_pop_head(twstack_t * stk, data_t * dataHolder) {
- if ( stk->pHead < stk->pData )
- return -1;
- *dataHolder = *(stk->pHead--);
- return 0;
- }
- int twstack_push_tail(twstack_t * stk, const data_t data) {
- if ( stk->pTail - 1 <= stk->pHead )
- return -1;
- *(--(stk->pTail)) = data;
- return 0;
- }
- int twstack_pop_tail(twstack_t * stk, data_t * dataHolder) {
- if ( stk->pTail >= stk->pData + stk->nSize )
- return -1;
- *dataHolder = *(stk->pTail++);
- return 0;
- }
- void twstack_free(twstack_t * stk) {
- free(stk->pData);
- free(stk);
- }
- void flush_input(void) {
- char ch;
- while ( scanf("%c", &ch) == 1 && ch != '\n' )
- ;
- }
- int main(void) {
- twstack_t * stk;
- data_t data;
- int choose;
- if ( ! ( stk = twstack_new(DEFAULT_STACK_SIZE) ) ) {
- fprintf(stderr, "Memory error!\n");
- return 1;
- }
- while ( printf("[1 = help]> ") > 0 && scanf("%d", &choose) == 1 && choose ) {
- switch ( choose ) {
- case 1:
- printf("0 = exit\n"
- "1 = this help\n"
- "2 = push head\n"
- "3 = pop head\n"
- "4 = push tail\n"
- "5 = pop tail\n\n"
- );
- break;
- case 2:
- printf("Data: ");
- if ( scanf(DATA_MASK, &data) != 1 ) {
- flush_input();
- printf("Wrong input!\n");
- }
- else if ( twstack_push_head(stk, data) )
- printf("Stack is full!\n");
- else
- printf("OK.\n");
- break;
- case 3:
- if ( twstack_pop_head(stk, &data) )
- printf("Head part is empty!\n");
- else
- printf("Returned value: " DATA_MASK "\n", data);
- break;
- case 4:
- printf("Data: ");
- if ( scanf(DATA_MASK, &data) != 1 ) {
- flush_input();
- printf("Wrong input!\n");
- }
- else if ( twstack_push_tail(stk, data) )
- printf("Stack is full!\n");
- else
- printf("OK.\n");
- break;
- case 5:
- if ( twstack_pop_tail(stk, &data) )
- printf("Tail part is empty!\n");
- else
- printf("Returned value: " DATA_MASK "\n", data);
- break;
- default:
- printf("Unknown selection!\n");
- break;
- }
- }
- twstack_free(stk);
- return 0;
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д