⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dystack.cpp

📁 It is A c++ code to run a simulation
💻 CPP
字号:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct stacknode
  {
  int data;
  stacknode *nextptr;
  } Stacknode;
typedef Stacknode* sptr;
void push (sptr*,int);
int pop(sptr*);
int isEmpty (sptr);
void printStack (sptr);
void instructions (void);
int main()
{
sptr stackptr=NULL;
int choice, value;
instructions();
printf ("?");
scanf ("%d",&choice);
     while (choice!=3)
	{
	 switch (choice)
	   {
	     case 1:
	       {
		printf ("Enter an integer:");
		scanf  ("%d",&value);
		push (&stackptr,value);
		printStack (stackptr);
		break;
	       }
	     case 2:
	       {
		if (!isEmpty(stackptr))
		  {
		  printf ("The popped value is %d.\n",pop(&stackptr));
		  }
		 printStack (stackptr);
		 break;
		}
	     default:
	      {
	      printf ("Invalid choice.\n\n");
	      instructions();
	      break;
	      }
	}
	printf ("?");
	scanf ("%d",&choice);
     }
      printf ("End of run.\n");
      return 0;
}
void instructions (void)
  {
   printf ("Enter your choice:\n");
   printf ("1 to push a value on the stack\n");
   printf ("2 to pop  a value off the stack\n");
   printf ("3 to end program\n");
  }
void push (sptr *topPtr,int info)
 {
 sptr newptr;
 newptr = (stacknode*)malloc(sizeof (stacknode));
  if (newptr!=NULL)
    {
     newptr->data=info;
     newptr->nextptr=*topPtr;
     *topPtr=newptr;
    }
  else
    {
    printf ("%d not inserted.No memory available.\n",info);
    }
 }
 int pop ( sptr*topPtr)
 {
  sptr tempPtr;
  int popvalue;
  tempPtr =*topPtr;
  popvalue=(*topPtr)->data;
  *topPtr=(*topPtr)->nextptr;
  free(tempPtr);
  return popvalue;
 }
 void  printStack (sptr currentptr)
  {
   if (currentptr==NULL)
    printf ("The stack is empty");
   else
    {
    printf ("The Stack is :\n");
     while (currentptr!=NULL)
     {
     printf ("%d-->",currentptr->data);
     currentptr=currentptr->nextptr;
     }
     printf ("NULL\n\n");
    }
  }
 int isEmpty (sptr topPtr)
   {
    return topPtr==NULL;
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -