C 如何修复从堆栈中删除元素的函数?
我尝试创建一个函数,该函数将获得一些包含整数和“n”(用于删除的数字)的堆栈。如果找到元素,函数将检查堆栈并删除元素。删除“n”元素后,堆栈需要保持相同的顺序。我尝试将所有元素复制到临时堆栈中,并在找到元素时不复制他。之后,我想将所有元素返回到第一个堆栈。我的功能有问题。我在网上找到了一些代码,并以此为基础构建了我的函数C 如何修复从堆栈中删除元素的函数?,c,stack,C,Stack,我尝试创建一个函数,该函数将获得一些包含整数和“n”(用于删除的数字)的堆栈。如果找到元素,函数将检查堆栈并删除元素。删除“n”元素后,堆栈需要保持相同的顺序。我尝试将所有元素复制到临时堆栈中,并在找到元素时不复制他。之后,我想将所有元素返回到第一个堆栈。我的功能有问题。我在网上找到了一些代码,并以此为基础构建了我的函数 #include<stdio.h> #define MAX 3 typedef int item; typedef struct { int TOP;
#include<stdio.h>
#define MAX 3
typedef int item;
typedef struct
{
int TOP;
int ele[MAX];
}Stack;
void init(Stack* s)
{
s->TOP = -1;
}
int isFull(Stack* s)
{
if (s->TOP == MAX - 1)
return 0;
return -1;
}
int isEmpty(Stack* s)
{
if (s->TOP == -1)
return 0;
return -1;
}
void push(Stack* s, int item)
{
if (!isFull(s))
{
printf("\nStack is full");
return;
}
s->TOP = s->TOP + 1;
s->ele[s->TOP] = item;
}
int pop(Stack* s, int* item)
{
if (!isEmpty(s))
{
printf("\nStack is empty");
return -1;
}
*item = s->ele[s->TOP];
s->TOP = s->TOP - 1;
return 0;
}
void func(Stack* s, item num)
{
//Check stack
if (s->TOP == -1)
{
printf_s("is empty");
return 0;
}
Stack temp; //def temporary stack
init(&temp); //init temporary stack
item current=0;
while (s->TOP != -1)
{
pop(&s, ¤t);
if (current != num)
{
push(&temp, current);
}
}
while (!isEmpty(&temp))
{
pop(&temp, ¤t);
push(&s, current);
}
while (s->TOP != -1)
{
pop(&s, ¤t);
{
printf_s("\nPoped Item : %d", current);
}
}
}
int main()
{
Stack s;
item num = 20;
init(&s);
push(&s, 4);
push(&s, 20);
push(&s, 11);
func(&s, num); //delete specific element in stack
getch();
return 0;
}
#包括
#定义最大值3
typedef int项;
类型定义结构
{
int TOP;
int ele[最大值];
}堆叠;
void init(堆栈*s)
{
s->TOP=-1;
}
int为满(堆栈*s)
{
如果(s->TOP==最大值-1)
返回0;
返回-1;
}
int isEmpty(堆栈*s)
{
如果(s->TOP==-1)
返回0;
返回-1;
}
无效推送(堆栈*s,整数项)
{
如果(!已满)
{
printf(“\n堆栈已满”);
返回;
}
s->TOP=s->TOP+1;
s->ele[s->TOP]=物料;
}
int-pop(堆栈*s,int*item)
{
如果(!isEmpty(s))
{
printf(“\n堆栈为空”);
返回-1;
}
*物料=s->ele[s->TOP];
s->TOP=s->TOP-1;
返回0;
}
void func(堆栈*s,项目编号)
{
//检查堆栈
如果(s->TOP==-1)
{
printf_s(“为空”);
返回0;
}
堆栈温度;//定义临时堆栈
init(&temp);//init临时堆栈
项目当前=0;
而(s->TOP!=-1)
{
流行音乐(流行音乐、流行音乐和流行音乐);
如果(当前!=num)
{
推送(温度和电流);
}
}
while(!isEmpty(&temp))
{
pop(温度和电流);
推送(&s,电流);
}
而(s->TOP!=-1)
{
流行音乐(流行音乐、流行音乐和流行音乐);
{
打印文件(“\n打印项目:%d”,当前);
}
}
}
int main()
{
堆栈s;
项目编号=20;
初始化(&s);
推送(s,4);
推(s,20);
推送(s,11);
func(&s,num);//删除堆栈中的特定元素
getch();
返回0;
}
我试图修复所有的时刻,但所有相同的警告都出现了。。。而且代码不起作用。==>>新版本:
#include<stdio.h>
#define MAX 3
typedef int item;
typedef struct
{
int top;
int ele[MAX];
}Stack;
void init(Stack* s)
{
s->top = -1;
}
int isFull(Stack* s)
{
return s->top == MAX - 1;
}
int isEmpty(Stack* s)
{
return s->top != -1;
}
void push(Stack* s, int item)
{
if (isFull(s))
{
printf("\nStack is full");
return;
}
++s->top; //can be written as: s->TOP = s->TOP + 1;
s->ele[s->top] = item;
}
int pop(Stack* s, int* item)
{
if (!isEmpty(s))
{
printf("\nStack is empty");
return -1;
}
*item = s->ele[s->top];
--s->top; //can be written as: s->top = s->top - 1;
return 0;
}
void func(Stack* s, item num)
{
//Check stack
if (s->top == -1)
{
printf_s("is empty");
return;
}
Stack temp; //def temporary stack
init(&temp); //init temporary stack
item current = 0;
while (s->top != -1)
{
pop(s, ¤t);
if (current != num)
{
push(&temp, current);
}
}
while (!isEmpty(&temp))
{
pop(&temp, ¤t);
push(s, current);
}
while (s->top != -1)
{
pop(&s, ¤t);
{
printf_s("\nPoped Item : %d", current);
}
}
}
int main()
{
Stack s;
item num = 20;
init(&s);
push(&s, 4);
push(&s, 20);
push(&s, 11);
func(&s, num); //delete specific element in stack
getch();
return 0;
}
#包括
#定义最大值3
typedef int项;
类型定义结构
{
int top;
int ele[最大值];
}堆叠;
void init(堆栈*s)
{
s->top=-1;
}
int为满(堆栈*s)
{
返回s->top==MAX-1;
}
int isEmpty(堆栈*s)
{
返回s->top!=-1;
}
无效推送(堆栈*s,整数项)
{
如果(已满)
{
printf(“\n堆栈已满”);
返回;
}
++s->top;//可以写成:s->top=s->top+1;
s->ele[s->top]=物料;
}
int-pop(堆栈*s,int*item)
{
如果(!isEmpty(s))
{
printf(“\n堆栈为空”);
返回-1;
}
*物料=s->ele[s->top];
--s->top;//可以写成:s->top=s->top-1;
返回0;
}
void func(堆栈*s,项目编号)
{
//检查堆栈
如果(s->top==-1)
{
printf_s(“为空”);
返回;
}
堆栈温度;//定义临时堆栈
init(&temp);//init临时堆栈
项目当前=0;
而(s->top!=-1)
{
流行音乐(流行音乐和流行音乐);
如果(当前!=num)
{
推送(温度和电流);
}
}
while(!isEmpty(&temp))
{
pop(温度和电流);
推动(s,电流);
}
而(s->top!=-1)
{
流行音乐(流行音乐、流行音乐和流行音乐);
{
打印文件(“\n打印项目:%d”,当前);
}
}
}
int main()
{
堆栈s;
项目编号=20;
初始化(&s);
推送(s,4);
推(s,20);
推送(s,11);
func(&s,num);//删除堆栈中的特定元素
getch();
返回0;
}
在
pop(&s, ¤t);
s
已经是指针。它被定义为Stack*s
。因此&s
是一个堆栈**
,而pop
期望Stack*
作为第一个参数。只需在此处删除&
。感谢所有帮助我提供提示和提示的用户!这是经过所有修改后的最终版本。终于成功了
#include<stdio.h>
#define MAX 7
typedef int item;
typedef struct
{
int top;
int ele[MAX];
}Stack;
void init(Stack* s)
{
s->top = -1;
}
int isFull(Stack* s)
{
return s->top == MAX - 1;
}
int isEmpty(Stack* s)
{
return s->top == -1;
}
void push(Stack* s, int item)
{
if (isFull(s))
{
printf("\nStack is full");
return;
}
++s->top; //can be written as: s->TOP = s->TOP + 1;
s->ele[s->top] = item;
}
int pop(Stack* s, int* item)
{
if (isEmpty(s) == 1)
{
printf("\nStack is empty");
return -1;
}
*item = s->ele[s->top];
--s->top; //can be written as: s->top = s->top - 1;
return 0;
}
void func(Stack* s, item num)
{
//Check stack
if (isEmpty(s) == 1)
{
printf_s("is empty");
return;
}
Stack temp; //def temporary stack
init(&temp); //init temporary stack
item current;
printf_s("\n\nCopy all items from s stack to temp stack");
while (isEmpty(s) != 1)
{
pop(s, ¤t);
if (current != num)
{
push(&temp, current);
printf_s("\nPushed Item is: %d", current);
}
else
printf_s("\nDeleted Item is: %d", current);
}
printf_s("\n\n\nCopy all items from temp stack to s stack");
while (isEmpty(&temp) != 1)
{
pop(&temp, ¤t);
printf_s("\nPoped Item is: %d", current);
push(s, current);
}
printf_s("\n\n\nPosition of elements in s stack");
while (isEmpty(s) != 1)
{
pop(s, ¤t);
printf_s("\nPoped Item is: %d", current);
}
}
int main()
{
Stack s;
item stackElem;
item num = 20;
init(&s);
printf("Fill stack \'s\'\n");
while (!isFull(&s))
{
printf_s("Enter integer number please: ");
scanf_s("%d", &stackElem);
push(&s, stackElem);
printf_s("Pushed Item is: %d\n", stackElem);
}
func(&s, num); //delete specific element in stack
getch();
return 0;
}
#包括
#定义最大值7
typedef int项;
类型定义结构
{
int top;
int ele[最大值];
}堆叠;
void init(堆栈*s)
{
s->top=-1;
}
int为满(堆栈*s)
{
返回s->top==MAX-1;
}
int isEmpty(堆栈*s)
{
返回s->top==-1;
}
无效推送(堆栈*s,整数项)
{
如果(已满)
{
printf(“\n堆栈已满”);
返回;
}
++s->top;//可以写成:s->top=s->top+1;
s->ele[s->top]=物料;
}
int-pop(堆栈*s,int*item)
{
如果(isEmpty=1)
{
printf(“\n堆栈为空”);
返回-1;
}
*物料=s->ele[s->top];
--s->top;//可以写成:s->top=s->top-1;
返回0;
}
void func(堆栈*s,项目编号)
{
//检查堆栈
如果(isEmpty=1)
{
printf_s(“为空”);
返回;
}
堆栈温度;//定义临时堆栈
init(&temp);//init临时堆栈
当前项目;
printf_s(“\n\n将所有项目从s堆栈复制到临时堆栈”);
while(isEmpty(s)!=1)
{
流行音乐(流行音乐和流行音乐);
如果(当前!=num)
{
推送(温度和电流);
打印文件(“\n推送的项目为:%d”,当前);
}
其他的
打印文件(“\n删除的项目为:%d”,当前);
}
printf_s(“\n\n\n将所有项目从临时堆栈复制到s堆栈”);
而(isEmpty(&temp)!=1)
{
pop(温度和电流);
打印文件(“\n打印项目为:%d”,当前);
推动(s,电流);
}
printf_s(“\n\n\n元素在s堆栈中的位置”);
while(isEmpty(s)!=1)
{
流行音乐(流行音乐和流行音乐);
打印文件(“\n打印项目为:%d”,当前);
}
}
int main()
{
堆栈s;
项目stackElem;
项目编号=20;
初始化(&s);
printf(“填充堆栈\'\n”);
而(!已满(&s))
{
printf_s(“请输入整数:”);
scanf_s(“%d”和stackElem);
推送(s、stackElem);
P