Đề nghị muốn test phải viết mục đích các hàm của cậu là gì ?
Ý nghĩa các tham số truyền vào. Và giá trị ra là gì? ý nghĩa của giá trị đầu ra.
Anh em xem hộ con nay của mình viết stack sao khi test với popStack toàn ra kết quả sai
Header File : stdafx.h
Visual C++ Code:
#pragma once #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #include <stdio.h> #include <tchar.h> typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; void showMenu(void); STACK* createStack(void); bool pushStack(STACK* stack, void* dataInPtr); void* popStack(STACK* stack); void* stackTop(STACK* stack); bool emptyStack(STACK* stack); bool fullStack(STACK* stack); int stackCount(STACK* stack); STACK* destroyStack(STACK* stack);
Cấu trúc header File : stdafx.cpp
Visual C++ Code:
#include "stdafx.h" #include <malloc.h> #include <stdio.h> // createStack() STACK* createStack(void) { STACK* stack; stack = (STACK*)malloc(sizeof(STACK)); if(stack) { stack->count=0; stack->top=NULL; } return stack; } // pushStack bool pushStack(STACK* stack, void* dataInPtr) { STACK_NODE* newPtr; newPtr = (STACK_NODE*)malloc(sizeof(STACK_NODE)); if(!newPtr) return false; newPtr->dataPtr=dataInPtr; newPtr->link=stack->top; stack->top=newPtr; (stack->count)++; return true; } // popStack void* popStack(STACK* stack) { void* dataOutPtr; STACK_NODE* temp; if(stack->count==0) dataOutPtr=NULL; else { temp=stack->top; dataOutPtr=stack->top->dataPtr; stack->top=stack->top->link; free(temp); (stack->count)--; } return dataOutPtr; } // stackTop void* stackTop(STACK* stack) { if(stack->count==0) return NULL; else return stack->top->dataPtr; } // emptyStack bool emptyStack(STACK* stack) { return (stack->count==0); } // fullStack bool fullStack(STACK* stack) { STACK_NODE* temp; if((temp=(STACK_NODE*)malloc(sizeof(*(stack->top))))) { free(temp); return false; } return true; } // stackCount int stackCount(STACK* stack) { return stack->count; } // destroyStack STACK* destroyStack(STACK* stack) { STACK_NODE* temp; if(stack) { while(stack->top!=NULL) { free(stack->top->dataPtr); temp=stack->top; stack->top=stack->top->link; free(temp); } free(stack); } return NULL; } // showMenu(void) void showMenu(void) { printf("================STACK MENU================\n"); printf(" A. Push data into stack\n"); printf(" B. Pop and print data\n"); printf(" C. Print data at top of stack\n"); printf(" D. Print entire stack ( top of base )\n"); printf(" E. Print stack status : Empty\n"); printf(" F. Print stack status : Full\n"); printf(" G. Print number of elements in stack\n"); printf(" H. Destroy stack and quit\n"); printf("==========================================\n"); }
Main File : Stack_Application.cpp
Visual C++ Code:
// Stack_Application.cpp : Defines the entry point for the console application. // /* Preprocessor */ #include <stdio.h> #include "stdafx.h" #include <malloc.h> #include <conio.h> /* Main() Unicode */ int _tmain(int argc, _TCHAR* argv[]) { /* Variables Declaration */ int* dataPtr; char key=NULL; int inNum, cntStack; STACK* stack; stack=createStack(); // Create Stack First showMenu(); // Show Menu do { /* Get Input Command */ printf("->Enter command :"); key = _getch(); switch(key) { /* Push Data Into Stack */ case 'a': case 'A': // fine printf("\nCommand<A> "); printf("::Input data <integer> : "); scanf_s("%d",&inNum); dataPtr = &inNum; if (!pushStack(stack,dataPtr)) printf("::Out of memory ! \n"); break; /* Pop Data Out Of Stack */ case 'b': case 'B': printf("\nCommand<B> "); dataPtr = (int*)popStack(stack); if ( dataPtr != NULL ) printf("::Output data : %d\n",*dataPtr); else printf("::Data Empty .\n"); break; /* Get Top Of The Stack */ case 'c': case 'C': printf("\nCommand<C> "); if ((dataPtr = (int*)stackTop(stack)) != NULL ) printf("::Top data : %d\n", *dataPtr); else printf("::No such a data ! \n"); break; /* Get Entire Data From Stack */ case 'd': case 'D': printf("\nCommand<D> "); dataPtr = (int*)popStack(stack); if ( dataPtr == NULL ) printf("::No data ."); else { printf("::Entire data :"); while ( dataPtr != NULL ) { printf(" %d",*dataPtr); dataPtr = (int*)popStack(stack); } printf("\n"); } break; /* Check Empty Stack */ case 'e': case 'E': // fine printf("\nCommand<E> "); if(emptyStack(stack)) printf("::Stack Status : Empty\n"); else printf("::Stack Status : No empty .\n"); break; /* Check Empty Stack */ case 'f': case 'F': // fine printf("\nCommand<F> "); if(fullStack(stack)) printf("::Stack Status : Full\n"); else printf("::Stack Status : No Full .\n"); break; /* Check Number Of Elements In Stack */ case 'g': case 'G': // fine printf("\nCommand<G> "); cntStack = stackCount(stack); printf("::Elements in stack : %d\n",cntStack); break; /* Destroy Stack */ case 'h': case 'H': // fine printf("\nCommand<H> "); printf("\n"); destroyStack(stack); break; default : // fine printf("\nInvalid Command ! Please Input Command Again..."); break; } } while ( key != 'h' && key != 'H'); return 0; }
Bài này pete viết để thể hiện ứng dụng của stack qua các hàm thôi.
Mọi người check xem khi mình test cứ qua hàm popStack nó đưa ra mỗi giá trị Top còn giá trị sau kô ra.
Còn Cái in ra giá trị toàn stack ngoài cách pop ra còn có cách nào hay hơn ko ?
Bài này viết trên môi trường VC++ 2k5 Pro.
Có gì mọi người vào góp ý cho vui !
None!
Đề nghị muốn test phải viết mục đích các hàm của cậu là gì ?
Ý nghĩa các tham số truyền vào. Và giá trị ra là gì? ý nghĩa của giá trị đầu ra.
Đã được chỉnh sửa lần cuối bởi shinichi_haha : 09-04-2007 lúc 08:39 PM.
OoShinHaoO
Visual C++ Code:
//This is a new version of pushStack bool pushStack(STACK* stack, void* dataInPtr) { STACK_NODE* newPtr; newPtr = (STACK_NODE*)malloc(sizeof(STACK_NODE)); if(!newPtr) return false; int newData = *((int*) dataInPtr); //We assume that dataInPtr points to an integer number newPtr->dataPtr = malloc(sizeof(int)); //Make a new room for storing this number's value //(NOT STORE ITS ADDRESS AS YOUR PREVIOUS VERSION //THROUGH A POINTER ASSIGNMENT) *((int*)(newPtr->dataPtr)) = newData; //Saving value... newPtr->link=stack->top; stack->top=newPtr; (stack->count)++; return true; }
You wrote:
Mọi người check xem khi mình test cứ qua hàm popStack nó đưa ra mỗi giá trị Top còn giá trị sau kô ra.
This concerning with your source code:
Visual C++ Code:
... int *dataPtr; int inNum; ... /* Push Data Into Stack */ case 'a': case 'A': // fine printf("\nCommand<A> "); printf("::Input data <integer> : "); scanf("%d",&inNum); dataPtr = &inNum; //dataPtr always points to inNum. if (!pushStack(stack,dataPtr)) //This will make a new node in stack, but node->dataPtr //will points to inNum's address only because you doesn't allocate //memory for storing the new value of inNum. The assignment //"node->dataPtr = dataPtr" means "let node->dataPtr & dataPtr point //"to the same variable" not "let node->dataPtr handle a COPY of data which //"dataPtr holds". printf("::Out of memory ! \n"); break; //Now, every node->dataPtr in your stack points to inNum //so there is only one choice for your popStack. //That's why you can not retrieve another value as you expect //and values displayed on screen are the lastest value of inNum
In addition, I think you should modify the file stdafx.h as below:
Visual C++ Code:
#pragma once #ifndef _STDAFX_INCLUDED #define _STDAFX_INCLUDED #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #include <stdio.h> #include <tchar.h> #include <malloc.h> #include <conio.h> ... #endif
This will allow you to save many keystrokes because you have to type "#include stdafx.h" only in stdafx.cpp & stack_application.cpp. Thus including other header files is unnecessary.
Visual C++ Code:
//stack_appilcation.cpp #include "stdafx.h" //nothing changed
and
Visual C++ Code:
//stdafx.cpp #include "stdafx.h" //nothing changed, except your pushStack function
Refer to C++ STL and Boost::any if you want to store anything in a container (instead of using void pointer).
Have fun!
Đã được chỉnh sửa lần cuối bởi ilovecplusplus : 10-04-2007 lúc 04:54 AM.
Our dreams are young and we both know they take us where we want to go...
Thanks...I corrected it ^^
None!