#include #include #include #include /*-------------------------*/ struct node { int val; struct node* next; }; typedef struct node NODE; /*-------------------------*/ NODE* newnode(void) { NODE* pnode; pnode = (NODE*)malloc(sizeof(NODE)); pnode->next = NULL; //(*pnode).next=NULL; return pnode; } /*-------------------------*/ NODE* push(NODE* root, int val) { NODE* nnode = newnode(); nnode->val = val;//(*nnode).val; if (root== NULL) root = nnode; else { nnode->next = root; root = nnode; } return root; } /*-------------------------*/ NODE* pop(NODE* root) { NODE* t = root; if (root != NULL) { root = root->next; free(t); } return root; } /*-------------------------*/ int peek(NODE* root) { return root->val; } /*-------------------------*/ void displaystack(NODE* root) { NODE* p = root; if (p == NULL)printf("The the stack is empty\n"); while (p) { printf("addr:%d(%d)->%i\n", (int)p, p->val, (int)p->next); p = p->next; } } /*-------------------------*/ NODE* findnode(NODE* root, int val) { NODE* p = root; while (p) { if (val == p->val) break; p = p->next; } return p; } /*--------PRESS ENTER-------*/ void press_enter() { printf("Press ENTER to continue"); getchar(); while (getchar() != '\n'); } /*-------------------------*/ int main(int argc, char* argv[]) { int key, val; NODE* root = NULL, * pnode; do { system("cls"); printf("1.Push\n"); printf("2.Pop\n"); printf("3.Search node\n"); printf("4.Print Stack\n"); printf("5.Peek\n"); printf("0.Exit\n"); key = getchar(); switch (key) { case '1':printf("val:"); scanf("%i", &val); root = push(root, val); break; case '2':root = pop(root); break; case '3':printf("val:"); scanf("%i", &val); pnode = findnode(root, val); if (pnode)printf("%i found in address %u\n", val, (int)pnode); else printf("Value not found\n"); break; case '4':displaystack(root); break; case '5':printf("Val %i on the top of stack\n",peek(root)); break; case '0':break; }; press_enter(); } while (key != '0'); return 0; }