栈_数组版

This commit is contained in:
lsy2246 2024-06-01 18:36:51 +08:00
parent 5ee422c56f
commit 649a6a2f44
3 changed files with 56 additions and 69 deletions

View File

@ -1,69 +0,0 @@
#include <stdio.h>
#include <malloc.h>
struct link_list_struct
{
struct link_list_struct * back_node;
int current_data;
};
void add_node ( struct link_list_struct **current_node , int content )
{
struct link_list_struct *new_node = malloc(sizeof(struct link_list_struct));
new_node->back_node = *current_node;
new_node->current_data = content;
*current_node = new_node;
};
void delete_node(struct link_list_struct **current_node){
if(*current_node == NULL){
printf ("当前没有结点\n");
}
else{
struct link_list_struct *tmp_node;
tmp_node = *current_node;
*current_node = (*current_node)->back_node;
free (tmp_node);
}
}
void view_node(struct link_list_struct *current_node){
if(current_node == NULL){
printf ("当前没有结点\n");
}
else{
printf ("%d\n",current_node->current_data);
}
}
int main ()
{
enum {add=1,delete,view};
struct link_list_struct *current_node=NULL;
while( true){
printf ("1.增加\n");
printf ("2.删除\n");
printf ("3.查看\n");
printf ("请输入:");
int pick;
scanf_s ("%d",&pick);
if(pick==add)
{
int content;
printf ("请输入要添加的数值:");
scanf_s ("%d",&content);
add_node (&current_node,content);
}
else if(pick==delete)
{
delete_node(&current_node);
}
else if(pick==view)
{
view_node (current_node);
}
}
}

View File

@ -0,0 +1,56 @@
#include <stdio.h>
struct queue
{
int _arr[10];
int _head;
int _tail;
int amount;
};
void in_queue ( struct queue * _arr_queue , int _content )
{
if ( _arr_queue -> amount == 10 )
{
printf ( "队列已满\n" );
return;
}
_arr_queue -> _arr[ _arr_queue -> _head ] = _content;
_arr_queue -> _head = ( _arr_queue -> _head + 1 ) % 10;
_arr_queue -> amount += 1;
printf ( "%d,入队成功\n\n",_content );
}
void out_queue ( struct queue * _arr_queue )
{
if ( _arr_queue->amount == 0 )
{
printf ( "队列为空\n\n" );
return;
}
printf ( "%d,出队成功\n" , _arr_queue -> _arr[ _arr_queue -> _tail ] );
_arr_queue -> _tail = ( _arr_queue -> _tail + 1 ) % 10;
_arr_queue -> amount -= 1;
}
int main ()
{
struct queue _arr_queue={0};
enum {_in=1,_out};
int pick,_number;
while(1){
printf ("1.入站\n");
printf ("2.出站\n");
printf ("请输入:");
scanf_s ("%d",&pick);
if(pick==_in){
printf ("请输入数字:");
scanf_s ("%d",&_number);
in_queue (&_arr_queue,_number);
}
else if(pick==_out){
out_queue (&_arr_queue);
}
}
}