Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define STACK_SIZE 5
- struct stackElement
- {
- int value;
- struct stackElement* prev;
- struct stackElement* next;
- };
- struct stackElement* pushStack(struct stackElement* stackPointer, int value) {
- if (stackPointer == NULL) {
- stackPointer = (struct stackElement*) malloc(sizeof(struct stackElement));
- stackPointer->prev = NULL;
- stackPointer->next = NULL;
- }
- else {
- struct stackElement* newElement;
- newElement = (struct stackElement*) malloc(sizeof(struct stackElement));
- stackPointer->next = newElement;
- newElement->prev = stackPointer;
- newElement->next = NULL;
- stackPointer = newElement;
- }
- stackPointer->value = value;
- return stackPointer;
- }
- struct stackElement* popStack(struct stackElement* stackPointer, int* value) {
- if (stackPointer->prev == NULL) {
- *value = stackPointer->value;
- free(stackPointer);
- stackPointer = NULL;
- }
- else {
- *value = stackPointer->value;
- stackPointer = stackPointer->prev;
- free(stackPointer->next);
- stackPointer->next = NULL;
- }
- return stackPointer;
- }
- void printStack(struct stackElement* stackPointer) {
- int counter = 0;
- while (stackPointer != NULL) {
- printf("Value[%d]: %d\n", counter, stackPointer->value);
- stackPointer = stackPointer->prev;
- counter++;
- }
- }
- int main() {
- puts("Source stack:");
- struct stackElement* stack = NULL;
- struct stackElement* posStack = NULL;
- struct stackElement* negStack = NULL;
- for (int i = 0; i < STACK_SIZE; i++) {
- int temp = 0;
- printf("Enter value: ");
- scanf_s("%d", &temp);
- fseek(stdin, 0, SEEK_SET);
- stack = pushStack(stack, temp);
- }
- for (int i = 0; i < STACK_SIZE; i++) {
- int temp = 0;
- stack = popStack(stack, &temp);
- if (temp < 0) negStack = pushStack(negStack, temp);
- if (temp > 0) posStack = pushStack(posStack, temp);
- }
- puts("");
- puts("Positive stack:");
- printStack(posStack); puts("");
- puts("Negative stack:");
- printStack(negStack); puts("");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement