/*the filename: main *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include "list.h" #include "stack.h" int main(){ STACK *myStack; NODE *x; myStack = (STACK *) malloc(sizeof(STACK)); creatStack(myStack); pushStack(myStack, 72); printStack(myStack); x = topOfStack(myStack); dumpNode(x); printf("******************\n"); pushStack(myStack, 39); printStack(myStack); x = topOfStack(myStack); dumpNode(x); printf("******************\n"); popStack(myStack); printStack(myStack); x = topOfStack(myStack); dumpNode(x); printf("******************\n"); popStack(myStack); printStack(myStack); x = topOfStack(myStack); dumpNode(x); printf("******************\n"); }
/*the filename: list_implement *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include "list.h" void creatList(LIST *listHead) { listHead->count = 0; listHead->head = NULL; } void insertFirstList(LIST *listHead, int data){ NODE *pNew; pNew = (NODE *) malloc(sizeof(NODE)); pNew->data = data; if (listHead->head == NULL){ // case 1: empty list pNew->link = NULL; listHead->head = pNew; } else { // case 2: begining list pNew->link = listHead->head; listHead->head = pNew; } listHead->count++; } void deleteFirstList(LIST *listHead){ NODE *pLoc; if (listHead->count == 0){ printf("Do nothing \n"); }else if (listHead->count == 1){ pLoc = listHead->head; listHead->head = NULL; listHead->count--; free(pLoc); } else { pLoc = listHead->head; listHead->head = pLoc->link; listHead->count--; free(pLoc); } } NODE *getFirstList(LIST *listHead){ NODE *pLoc; if (listHead->count == 0){ printf("Do nothing \n"); }else { pLoc = listHead->head; return pLoc; } } void dumpNode(NODE *input){ if (input != NULL){ printf("the data =%d\n", input->data); } } void printList(LIST *listHead){ NODE *curr; if (listHead->head != NULL){ printf("the total count = %d\n", listHead->count); for (curr = listHead->head; curr != NULL; curr = curr->link){ printf("%d\n", curr->data); } } else{ printf("the list is empty\n"); } }
/*the filename: stack_implement *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include "list.h" #include "stack.h" void creatStack(STACK *stackHead){ creatList(stackHead); } void pushStack(STACK *stackHead, int data){ insertFirstList(stackHead, data); } void printStack(STACK *stackHead){ printList(stackHead); } void popStack(STACK *stackHead){ deleteFirstList(stackHead); } NODE *topOfStack(STACK *stackHead){ return getFirstList(stackHead); }
/*the filename: stack *the filename extension: .h */ #include "list.h" #ifndef MY_STACK_HEAD #define MY_STACK_HEAD typedef LIST STACK; #endif void creatStack(STACK *stackHead); void pushStack(STACK *stackHead, int data); void printStack(STACK *stackHead); void dumpNode(NODE *input); void popStack(STACK *stackHead); NODE *topOfStack(STACK *stackHead);
/*the filename: list *the filename extension: .h */ #ifndef MY_LIST_NODE #define MY_LIST_NODE typedef struct node { int data; struct node *link; } NODE; #endif #ifndef MY_LIST_HEAD #define MY_LIST_HEAD typedef struct list { int count; struct node *head; } LIST; #endif void creatList(LIST *listHead); void insertFirstList(LIST *listHead, int data); void printList(LIST *listHead); void dumpNode(NODE *input); void deleteFirstList(LIST *listHead); NODE *getFirstList(LIST *listHead);
沒有留言:
張貼留言