Home > Back-end >  Using the vc compiler running, the input data and press enter always need to enter a more data to th
Using the vc compiler running, the input data and press enter always need to enter a more data to th

Time:10-25

Write a stack of any node, delete or increase the chain after running the program, whether to add or remove, at first the scanf input, total want to enter another data to perform the next step, and the input data will cover the next input data
the scanfThen I am a beginner, can help have a look at chain stack, by the way of any two nodes exchange where that part of the program went wrong
Thanks for elder people



The code is as follows: # include
#include
#include

//# define the SIZE of 100


Typedef struct NODE//define the type of NODE structure
{
int data;//data domain
Struct NODE * Next;//pointer field
} NODE, * PNODE;


Typedef struct Stack//define chain Stack
{
PNODE top;//the stack pointer
PNODE base;//the bottom of the stack pointer
Int stack_size;//stack size
} STACK, * PSTACK;

Typedef struct stack stack//define order
{
Int * top;//the stack pointer
Int * base;//the bottom of the stack pointer
Int SQ_size;//stack size
} stack, * pstack;

Int init_stack PSTACK (p);//initialize
Int Push_stack (PSTACK p, int push);//into the stack
Int full_stack PSTACK (p);//whether the stack is full
Int Pop_stack (PSTACK p);//the stack
Int travel_stack PSTACK (p);//traversal stack element
Int insert_stack PSTACK (p);//input data behind any node
Int delete_stack PSTACK (p);//delete any node
Int adjust_stack (PSTACK p, int, int t);//adjust the position of any two nodes data domain
Int length (PSTACK ps);//for the number of stack elements


Int main ()
{
Int pop;
STACK link_stack;
Init_stack (& amp; Link_stack);//stack initialization
Push_stack (& amp; Link_stack, 5);//insert data
Push_stack (& amp; Link_stack, 28);
Push_stack (& amp; Link_stack, 45);
Push_stack (& amp; Link_stack, 52);
Push_stack (& amp; Link_stack, 11);
Travel_stack (& amp; Link_stack);
printf("\n");
Insert_stack (& amp; Link_stack);//insert the data it anywhere
Travel_stack (& amp; Link_stack);
Delete_stack (& amp; Link_stack);
Travel_stack (& amp; Link_stack);
printf("\n");

//adjust_stack (& amp; Link_stack, 2, 3);
//printf("\n");
//travel_stack (& amp; Link_stack);
}


Int init_stack initialization (PSTACK p)//
{
P - & gt; Base=(PNODE) malloc (sizeof (NODE));//the application space to create a head node, address to p - & gt; Base

if(p-> The base==NULL)
{
Printf (" \ n application stack failure ");
The exit (1);
}
The else
{
P - & gt; Base - & gt; Next=NULL;
P - & gt; Top=p - & gt; The base;


P - & gt; Stack_size==NULL;
Printf (" stack initialization successful \ n ");
}
return 1;//initialization complete
}

Int Push_stack (PSTACK p, int push)
{
PNODE p_new;
P_new=(PNODE) malloc (sizeof (NODE));//the application space create node
P_new - & gt; data=https://bbs.csdn.net/topics/push;
P_new - & gt; Next=p - & gt; Top;
P - & gt; Top=p_new;
P - & gt; Stack_size + +;
Printf (" % d into the stack success \ n ", p_new - & gt; The data);
return ;

}


Int Pop_stack (PSTACK p, int * pop)//a stack
{
If (empty_stack (& amp; P)==1)
{
return 0;//the stack table is empty, no element of the stack
}
The else
{
* pop=p - & gt; Top - & gt; The data;
P - & gt; Top=p - & gt; Top - & gt; Next;
P - & gt; Stack_size -;
Printf (" % d stack \ n "success, * pop);
Printf (" the stack table with % d element \ n ", p - & gt; Top - p - & gt; Base);//????????

}
return 1;
}

Int travel_stack PSTACK (p)//traversal stack chain
{
If (empty_stack (& amp; P)==1)
{
return 0;//the stack table is empty
}
The else
{
PNODE q;
Q=p - & gt; Top;

While (q!=p - & gt; Base)
{
Printf (" % d ", q - & gt; The data);
Q=q - & gt; Next;

}

}

}


Int empty_stack PSTACK (p)//whether the stack is empty
{
if(p-> Top - p - & gt; The base==0)
{
Printf (" stack is empty \ n ");
return 1;//said stack empty
}
The else
{
return 0;//said stack is not empty
}
}

Int full_stack PSTACK (p)//whether the stack is full
{
if(p-> Top - p - & gt; The base==p - & gt; Stack_size)
{
Printf (" the stack is full \ n ");
return 1;//said the stack is full
}
The else
return 0;//said stack under
}

Int insert_stack PSTACK (p)//insert the data at any position
{
Int I, val.
int temp;
PNODE q;//secondary node
PNODE pnew;
Printf (" please enter the location and data to be inserted: ");
The scanf (" % d, % d \ n ", & amp; I, & amp; Val);//enter the location of the need to insert
Q=p - & gt; Top;
For (temp=0; Temp{
Q=q - & gt; Next;
}

Pnew=(PNODE) malloc (sizeof (NODE));//the application space for the new node
Pnew - & gt; data=https://bbs.csdn.net/topics/val;
Pnew - & gt; Next=q - & gt; Next;
Q - & gt; Next=pnew;

return 1;
}


Int delete_stack PSTACK (p)//delete any node
{
int i;
Int t, del;
PNODE r;
PNODE q;
Int len=length (& amp; P);//for the number of stack elements
R=p - & gt; Top;

Printf (" please enter the need to delete the node: ");
The scanf (" % d \ n ", & amp; I);
If (I==1)
{
R=p - & gt; Top;
Del=r - & gt; The data;
P - & gt; Top=r - & gt; Next;
Free (r);
Printf (" deleted value is: % d \ n ", del);
}
If (I.
=1){
For (t=0; T{
R=r - & gt; Next;
}
Q=r - & gt; Next;
Del=q - & gt; The data;
R - & gt; Next=q - & gt; Next;
Free (q);
Printf (" deleted value is: % d \ n ", del);
}



return 1;//delete success
}

Int adjust_stack PSTACK p, int, int (t)//exchange data
{

PNODE q=p - & gt; Top;
PNODE r=p - & gt; Top;
//swap the position of the
Int j, k, temp.//swap places the value of the data domain
Int len=length (& amp; P); The number of elements in the stack//o


for(j=0; j
  • Related