gpt4 book ai didi

c - 列表不断打印

转载 作者:行者123 更新时间:2023-11-30 19:14:23 24 4
gpt4 key购买 nike

我正在尝试打印一个响铃列表,它仅适用于其中的一个项目,但当我添加第二个项目时,列表会无限期地打印最后一个项目。我不明白为什么它要这样做,因为我一直更新指针。有什么帮助吗?

#include "ring.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

struct node {
char data[20];
struct node* next;
struct node* prev;
};
typedef struct node node;

struct ring {
struct node* sentinel;
struct node* current;
};

ring *newring() {
node *SentinelNode = malloc(sizeof(node));
SentinelNode->next = SentinelNode;
SentinelNode->prev = SentinelNode;
ring *initialring = malloc(sizeof(ring));
initialring->sentinel = SentinelNode;
return initialring;
}

node *GetNewNode (char *x) {
node *newNode = (struct node*)malloc(sizeof(node));
strcpy (newNode->data, x);
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

void insertnode (ring *r, char *x) {
node *newNode = GetNewNode(x);
if ((r->sentinel->next == r->sentinel) && (r->sentinel->prev == r->sentinel)) {
r->sentinel->prev = newNode;
r->sentinel->next = newNode;
newNode->prev = r->sentinel->next;
newNode->next = r->sentinel->prev;
r->current = newNode;
printf("newnode data is %s\n", newNode->data);
}
else {
r->sentinel->prev = newNode;
r->current->prev = newNode;
printf("sentinel previous is %s\n", r->sentinel->prev->data);
newNode->prev = r->sentinel->next;
newNode->next = r->current;
r->current = newNode;
}
}

void nextnode (ring *r) {
if (r->current->next != r->sentinel) {
r->current = r->current->next;
}
else {
r->current = r->sentinel->prev;
}
}

void prevnode (ring *r) {
if (r->current->prev != r->sentinel) {
r->current = r->current->prev;
}
else {
r->current = r->sentinel->next;
}
}

void printlist(ring *r) {
node *control = r->current;
node *temp = r->current;
printf("temp is %s\n", temp->data);
printf ("%s <<<\n", temp->data);
printf("tempnext is %s\n", temp->next->data);
temp = temp->next;
printf("temp is %s\n", temp->data);
while (temp != control) {
printf("tempnext is %s\n", temp->next->data);
printf ("%s ", temp->data);
temp = temp->next;
printf("temp is %s\n", temp->data);
}
printf("\n");
}

void deleteEntry(ring *r) {
if ((r->current->next != r->sentinel) && (r->current->prev != r->sentinel)) {
r->current->prev->next = r->current->next;
r->current->next->prev = r->current->prev;
}
if (r->current->next == r->sentinel) {
r->current->next->next = r->current->prev;
r->current->prev->next = r->current->next;
}
if (r->current->prev == r->sentinel){
r->current->prev->prev = r->current->next;
r->current->next->prev = r->current->prev;
}
return;
}

int main(int n, char *args[n]) {

ring *thering = newring();

int s = 0;
char y = 'n';
int finish = 0;
char item[20];

while (finish == 0) {
printf("My Shopping List\n");
if (s == 0) {

}
else if (s != 0) {
printlist(thering);
}
//press n to add a new item
if (y == 'n') {
s = 1;
printf("Add your item\n");
scanf("%s", item);
insertnode (thering, item);
printlist(thering);
y = getch();
}
else if (y == 's') {
prevnode(thering);
printlist(thering);
y = getch();
}
else if (y == 'w') {
nextnode(thering);
printlist(thering);
y = getch();
}
else if (y == 'x') {
deleteEntry(thering);
printlist(thering);
getch();
}
else if (y == 'e') {
finish = 1;
}
else {
printf ("Please enter a valid letter\n");
getch();
}
}
printf ("Shopping Completed\n");
}

最佳答案

插入第二个节点时会出现一个圆圈,该节点指向其自身。这就是为什么它会无限期地打印最后一项。像这样更改您的插入代码:

if ((r->sentinel->next == r->sentinel) && (r->sentinel->prev == r->sentinel)) {
newNode->prev = r->sentinel->next;
newNode->next = r->sentinel->prev;
r->sentinel->prev = newNode;
r->sentinel->next = newNode;
r->current = newNode;
printf("newnode data is %s\n", newNode->data);
}

关于c - 列表不断打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34125837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com