Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define N 5
- typedef struct node
- {
- int type;
- void *dataPtr;
- struct node *next;
- }tNode;
- typedef struct nodeInfo
- {
- int count;
- tNode *head;
- }tStack;
- typedef struct Name
- {
- int type; //type 0
- char name[16];
- int location; // record the used space number
- bool used;
- }tTypeName;
- typedef struct Score
- {
- int type; //type 1
- int recordNumber;
- int location; // record the used space number
- bool used;
- }tTypeScore;
- tTypeName nameBuf[N];
- tTypeScore scoreBuf[N];
- tStack *createStack(void); //Create a stack
- void pushStack (tStack *pStack, void *dataPtr); //push a dataPtr to a specific stack
- void *handlePopOperation (tStack *pStack); //pop a specific stack and return a void* dataPtr
- void printStackContentFromBottom(tStack *pStack); //print all content in stack
- int getNameSpace (tTypeName **ppName); //get the space from nameBuf, return the allocated number
- int getScoreSpace (tTypeScore **ppScore); //get the space from scoreBuf, return the allocated number
- void returnNameSpace (int location); //return the space of the used nameBuf
- void returnScoreSpace (int location); //return the space of the used scoreBuf
- tStack* createStack(void)
- {
- tStack *pstack;
- pstack = (tStack *)malloc(sizeof(tStack));
- return pstack;
- }
- void handlePushOperation(tStack *pStack)
- {
- int datatype=-1;
- tTypeName *pName;
- tTypeScore *pScore;
- int location;
- tNode *newnode;
- bool key=true;
- while(key)
- {
- printf("Which data (0 or 1) you are going to push?");
- scanf("%d", &datatype);
- if(datatype==0 && nameBuf[4].used==false) key=false;
- else if(datatype==1 && scoreBuf[4].used==false) key=false;
- else printf("Error Input!!\n");
- }
- switch(datatype)
- {
- case 0:
- printf("Please enter a string:");
- location = getNameSpace(&pName);
- newnode = (tNode *)malloc(sizeof(tNode));
- newnode->dataPtr=(void *)pName;
- newnode->next = pStack->head;
- pStack->head = newnode;
- if(pStack->count==0) pStack->head = NULL;
- pStack->count++;
- break;
- case 1:
- printf("Please enter a number:");
- location = getScoreSpace(&pScore);
- // xx
- break;
- default:
- printf("Error Input!!\n");
- break;
- }
- }
- int getNameSpace (tTypeName **ppName)
- {
- int i;
- for(i=0;i<5;i++) if(nameBuf[i].used==false) break;
- *ppName=&nameBuf[i];
- scanf("%s",nameBuf[i].name);
- nameBuf[i].type = 0;
- nameBuf[i].location = i;
- nameBuf[i].used = true;
- return i;
- }
- int getScoreSpace (tTypeScore **ppScore)
- {
- int i;
- for(i=0;i<5;i++) if(scoreBuf[i].used==false) break;
- *ppScore=&scoreBuf[i];
- scoreBuf[i].type = 1;
- scanf("%d",&scoreBuf[i].recordNumber);
- scoreBuf[i].location = i;
- scoreBuf[i].used = true;
- return i;
- }
- /*
- void handlePopOperation(tStack *pStack)
- {
- //if 0
- returnNameSpace(loc);
- //if 1
- returnScoreSpace(loc);
- }
- */
- void printStackContentFromBottom(tStack *pStack)
- {
- tNode *p = pStack->head;
- tTypeName *pName;
- tTypeScore *pScore;
- while (p!= NULL)
- {
- if(p->type==0)
- {
- pName=(tTypeName *)p->dataPtr;
- puts(pName->name);
- }
- if(p->type==1) {
- pScore=(tTypeScore *)p->dataPtr;
- printf("%d\n",pScore->recordNumber);
- }
- p = p->next;
- }
- }
- int main (void)
- {
- tStack *pStack;
- int op;
- pStack = createStack();
- while (1)
- {
- //menu
- printf("\n1. push a item to the stack\n");
- printf("2. pop a item to the stack\n");
- scanf("%d",&op);
- if(op==0){
- handlePushOperation(pStack);
- }
- else if(op==1){
- handlePopOperation(pStack);
- }
- else {
- printf("Error Input!!\n");
- }
- printStackContentFromBottom(pStack);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement