On-line: гостей 0. Всего: 0 [подробнее..]
Программисты всех стран, объединяйтесь!

АвторСообщение



ссылка на сообщение  Отправлено: 04.07.15 21:31. Заголовок: Stack overflow. delete head.


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.

Спасибо: 0 
ПрофильЦитата Ответить
Ответов - 9 [только новые]





ссылка на сообщение  Отправлено: 04.07.15 21:32. Заголовок: pop is delete head. ..


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

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 04.07.15 21:54. Заголовок: I think that the ini..


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

MY_STACK stack = NULL;


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 04.07.15 23:59. Заголовок: I made some changes ..


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.

 
#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;
}

The program output is
!dlroW ,olleH

Try it and say what you think about it.

Спасибо: 1 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 05.07.15 00:19. Заголовок: I have to initialize..


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

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 05.07.15 00:27. Заголовок: I already posted one..


I already posted one more message. See the demonstrative program.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 05.07.15 00:28. Заголовок: ok so here is what h..


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.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 05.07.15 00:32. Заголовок: no wait. IT WORKS. i..


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

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 05.07.15 00:55. Заголовок: hammodi пишет: IT W..


hammodi пишет:

 цитата:
IT WORKS



Are you speaking about the demonstrative program I showed?

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 05.07.15 01:06. Заголовок: well. i made a cpl c..


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.

Спасибо: 0 
ПрофильЦитата Ответить
Ответ:
1 2 3 4 5 6 7 8 9
большой шрифт малый шрифт надстрочный подстрочный заголовок большой заголовок видео с youtube.com картинка из интернета картинка с компьютера ссылка файл с компьютера русская клавиатура транслитератор  цитата  кавычки моноширинный шрифт моноширинный шрифт горизонтальная линия отступ точка LI бегущая строка оффтопик свернутый текст

показывать это сообщение только модераторам
не делать ссылки активными
Имя, пароль:      зарегистрироваться    
Тему читают:
- участник сейчас на форуме
- участник вне форума
Все даты в формате GMT  3 час. Хитов сегодня: 3
Права: смайлы да, картинки да, шрифты да, голосования нет
аватары да, автозамена ссылок вкл, премодерация откл, правка нет