您好,第一次发布,所以我正在尝试编写的程序,假设是要接受无限制的输入,它只是将它们压入,直到您输入负值为止。
我已经走过那部分了。该程序假设要删除(弹出)每个堆栈,直到破坏该值为止,例如,我压入5,6,7,8,9,8,8时,我压入的第一个值是5,因此它只能在它上面放5叠。
因此,由于堆栈中有6个位于其顶部,所以5个中断了,因此我们将所有这些弹出。我认为我的代码也适用于这种情况。但是我的问题是,如果我按8,7,8,3,2,1,1,0,那3就是这里被打破的一个,因为在3之上有4个值,在这种情况下,一个会弹出所有通往3。
我对函数进行了编码,以使用节点作为占位符来遍历堆栈,而我尝试编码的想法是在最低处有一个占位符,并在堆栈的顶部(头)开始有第三个节点并遍历并弹出它,如果与推入的最低元素的差,并且其顶部的堆栈数量等于-1。
Input -: 1,1,1,1,1,1,-1
output -: stack broke at stack place 3, the size of the stack is 0
stack broke at stack place 6, new stack size is 0
There is 2 broken stacks pieces and 4 stack pieces still attached to each other
下面是我与占位符一起使用的横向代码,上面是我上面描述的大多数操作。
void display( int counter,node *head)
{
int counter2, counter3=0;
int differ;
node*lowest = head;
node*crawler = head;
node*crawler2=head;
printf("%d \n", crawler->data);
while (crawler->next != NULL)
{
crawler=crawler->next;
if( crawler->data < lowest->data){
lowest=crawler;
}
counter3++;
/*/ printf("the crawler has traversed this many times %d \n", counter3);
printf("the lowest stack is %d \n", lowest->data);
*/ printf("%d \n", crawler->data);
}
while (crawler2->next != lowest)
{
crawler2=crawler2->next;
printf("helo\n");
pop();
counter2++;
}
printf("the count of the lowest is %d \n", counter3);
printf("the lowest stack is %d \n", lowest->data);
printf("the count is %d \n", counter);
differ=lowest->data-counter3;
printf("the difference %d\n", differ);
/*/if(differ<=-1){
for( int a = 0; a < counter3+1; a++){
pop();
counter2++;
}
}*/
printf("the pop count is %d \n", counter2);
}
横向代码的结尾
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *top;
void initialize()
{
top = NULL;
}
void push(int value)
{
node *tmp;
tmp = malloc(sizeof(node));
tmp -> data = value;
tmp -> next = top;
top = tmp;
}
int pop()
{
node *tmp;
int n;
tmp = top;
n = tmp->data;
top = top->next;
free(tmp);
return n;
}
int Top()
{
return top->data;
}
int isempty()
{
return top==NULL;
}
void display( int counter,node *head)
{
int counter2, counter3=0;
int differ;
node*lowest = head;
node*crawler = head;
node*crawler2=head;
printf("%d \n", crawler->data);
while (crawler->next != NULL)
{
crawler=crawler->next;
if( crawler->data < lowest->data){
lowest=crawler;
}
counter3++;
/*/ printf("the crawler has traversed this many times %d \n", counter3);
printf("the lowest stack is %d \n", lowest->data);
*/ printf("%d \n", crawler->data);
}
while (crawler2->next != lowest)
{
crawler2=crawler2->next;
printf("helo\n");
pop();
counter2++;
}
printf("the count of the lowest is %d \n", counter3);
printf("the lowest stack is %d \n", lowest->data);
printf("the count is %d \n", counter);
differ=lowest->data-counter3;
printf("the difference %d\n", differ);
/*/if(differ<=-1){
for( int a = 0; a < counter3+1; a++){
pop();
counter2++;
}
}*/
printf("the pop count is %d \n", counter2);
}
int main()
{
int choice=1, data=0, counter;
initialize();
printf("Enter data to push into stack: ");
while(data>=0)
{
scanf("%d", &data);
if(data==-1){
break;
}
push(data);
counter++;
}
counter=counter-1;
printf("the count is %d \n", counter);
node*copyofhead=top;
//copyofhead->data--;
printf("The top is %d and the imposter is %d\n",Top(), copyofhead->data);
printf("The top after pop is %d\n",Top());
display(counter, top);
return 0;
}
最佳答案
如果要允许输入逗号,则必须使用它们:scanf("%d,", &data);
counter=counter-1;
行是错误的,因为counter
没有它是正确的。
恐怕您的“横向代码”几乎不可挽救,尤其是因为它缺少一个完整的循环,因此只能生成一组输出数据,而所需的输出显示多于一条stack broke at stack place …
行。但是,只有在您阐明问题后,我才能提供解决方案。
关于c - 堆栈链表,带功能的遍历器链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58714586/