Форум » C/C++ для начинающих (C/C++ for beginners) » Stack overflow. delete head. » Ответить

Stack overflow. delete head.

hammodi: hey. thanks for helping me btw this is my stack #include <stdio.h> #include <stdlib.h> #include "my_stack.h" struct node; typedef struct node Node; typedef Node* Node_ptr; int data; void destroy (MY_STACK* phMy_stack); Status push(MY_STACK* hMy_stack, char item); Status pop (MY_STACK* hMy_stack); char top(MY_STACK hMy_stack); Bool empty(MY_STACK hMy_stack); void init_functions(MY_STACK stack); struct node { void(*destroy)(MY_STACK* phMy_stack); Status(*push)(MY_STACK* hMy_stack, char item); Status(*pop)(MY_STACK* hMy_stack); char(*top)(MY_STACK hMy_stack); Bool(*empty)(MY_STACK hMy_stack); char data; Node_ptr next; }; void init_functions(MY_STACK stack){ stack->destroy = destroy; stack->empty = empty; stack->pop = pop; stack->push = push; stack->top = top; } MY_STACK my_stack_init_default(void){ MY_STACK temp; temp = (MY_STACK)(malloc(sizeof(Node))); if (temp == NULL){ exit(1); } ((Node_ptr)temp)->data = NULL; ((Node_ptr)temp)->next = NULL; init_functions(temp); return temp; } Status push(MY_STACK* head, char item) { Node_ptr temp; //create a new node temp = (Node_ptr)malloc(sizeof(Node)); //allocates the space if (temp == NULL) { printf("Failed to allocate node\n"); return FAILURE; } //initialize the new node temp->data = item; temp->next = (Node_ptr)head; //insert the new node at the head. (head insert) init_functions(temp); *head = temp; return SUCCESS; } void destroy(MY_STACK* head){ Node_ptr* temp_head = (Node_ptr*)head; Node_ptr temp; while (temp_head != NULL) { temp = *temp_head; *temp_head = (*temp_head)->next; free(temp); } } Status pop(MY_STACK* head){ Node_ptr* temp; printf("%c a", ((Node_ptr)head)->data); if (*head != NULL) { temp = &((Node_ptr)head); ((Node_ptr)head) =((Node_ptr)head)->next; free(*temp); return SUCCESS; } return FAILURE; } Bool empty(MY_STACK hMy_stack){ Node_ptr temp = (Node_ptr)hMy_stack; if (temp==NULL){ return TRUE; } return FALSE; } char top(MY_STACK hMy_stack){ Node_ptr temp = (Node_ptr) hMy_stack; if (temp>empty(hMy_stack)){ return temp->data; } return NULL; } this is the header file f or it. #ifndef MY_STACK_H #define MY_STACK_H #include "my_status.h" enum boolean {FALSE, TRUE}; typedef enum boolean Bool; struct my_stack_public; typedef struct my_stack_public* MY_STACK; struct my_stack_public { void (*destroy)(MY_STACK* phMy_stack); Status (*push)(MY_STACK* hMy_stack, char item); Status (*pop)(MY_STACK* hMy_stack); char (*top)(MY_STACK hMy_stack); Bool (*empty)(MY_STACK hMy_stack); }; MY_STACK my_stack_init_default(void); #endif for this assignment i must leave the header file UNCHANGED.

Ответов - 9

hammodi: pop is delete head. push is insert head. i am inserting first. is a stack.

Сыроежка: I think that the initial value of the stack should be a NULL pointer. In the code above you did not show how you declare the stack. I think that you should write simply [pre2]MY_STACK stack = NULL;[/pre2]

Сыроежка: I made some changes of the design of the stack. It can be discussed. Here is a demonstrative program. I did not split it into headers and modules. [pre2] #include <stdio.h> #include <stdlib.h> enum boolean {FALSE, TRUE}; typedef enum boolean Bool; typedef enum { SUCCESS = 0, FAILURE = -1 } Status; struct my_stack_public; typedef struct my_stack_public* MY_STACK; struct my_stack_public { void ( *destroy )( MY_STACK stack ); Status ( *push )( MY_STACK stack, char item ); Status ( *pop )( MY_STACK stack ); char ( *top )( MY_STACK stack ); Bool ( *empty )( MY_STACK stack ); }; MY_STACK my_stack_init_default(void); /*============> End of header Stack,h <============*/ struct node; typedef struct node Node; typedef Node* Node_ptr; struct node { char data; Node_ptr next; }; struct head_node; typedef struct head_node Head_node; typedef Head_node *Head_ptr; struct head_node { struct my_stack_public methods; Node_ptr head; }; void destroy( MY_STACK stack ); Status push( MY_STACK stack, char item ); Status pop( MY_STACK stack ); char top( MY_STACK stack ); Bool empty( MY_STACK stack ); void init_functions( MY_STACK stack ) { stack->destroy = destroy; stack->empty = empty; stack->pop = pop; stack->push = push; stack->top = top; } MY_STACK my_stack_init_default( void ) { Head_ptr head; head = malloc( sizeof( Head_node ) ); if ( head != NULL ) { head->head = NULL; init_functions( ( MY_STACK)head ); } return ( MY_STACK )head; } void destroy( MY_STACK stack ) { if ( stack == NULL ) return; Head_ptr head = ( Head_ptr )stack; for ( Node_ptr current = head->head; current != NULL; ) { Node_ptr tmp = current; current = current->next; free( tmp ); } head->head = NULL; } Status push( MY_STACK stack, char item ) { Node_ptr tmp; if ( stack == NULL || ( tmp = malloc( sizeof( Node ) ) ) == NULL ) { return FAILURE; } tmp->data = item; tmp->next = ( ( Head_ptr )stack )->head; ( ( Head_ptr )stack )->head = tmp; return SUCCESS; } Status pop( MY_STACK stack ) { Head_ptr head = ( Head_ptr )stack; if ( head == NULL || head->head == NULL ) { return FAILURE; } Node_ptr tmp = head->head; head->head = head->head->next; free( tmp ); return SUCCESS; } char top( MY_STACK stack ) { Head_ptr head = ( Head_ptr )stack; if ( head == NULL || head->head == NULL ) return '\0'; return head->head->data; } Bool empty( MY_STACK stack ) { Head_ptr head = ( Head_ptr )stack; return head == NULL || head->head == NULL; } int main( void ) { MY_STACK stack = my_stack_init_default(); if ( stack == NULL ) exit( 1 ); char s[] = "Hello, World!"; for ( char *p = s; *p; ++p ) stack->push( stack, *p ); while ( !stack->empty( stack ) ) { printf( "%c", stack->top( stack ) ); stack->pop( stack ); } printf( "\n" ); stack->destroy( stack ); free( stack ); return 0; } [/pre2] The program output is [pre2]!dlroW ,olleH[/pre2] Try it and say what you think about it.


hammodi: I have to initialize the stacks function i cant just say stack=null.

Сыроежка: I already posted one more message. See the demonstrative program.

hammodi: ok so here is what happened. what am doing is a problem called parentheses balance from UVA online judge. here is what its asking https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=614 I've reached the same point i reached earlier. when i try to output the first item. head after i delete the list i get a space for some reason. and empty returns FALSE. which doesn't make any sense.

hammodi: no wait. IT WORKS. i fixed it. i was passing in pointers pointers instead of just pointers. when i fixed that. it works. thanks a lot. you mind if i ask you about other things if i have questions. i would greatly appreciate it

Сыроежка: hammodi пишет: IT WORKS Are you speaking about the demonstrative program I showed?

hammodi: well. i made a cpl changes to match my program. but my main is a lot different to make the problem work. i have functions that read in characters and look for matches and then deletes them. so your changes + what i had at main.



полная версия страницы