我正在尝试打印一个环形列表,它只适用于其中的一项,但当我添加第二项时,列表会无限期地打印最后一项。我不明白它为什么这么做,因为我一直在更新指针。有什么帮助吗?
#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 %sn", newNode->data);
}
else {
r->sentinel->prev = newNode;
r->current->prev = newNode;
printf("sentinel previous is %sn", 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 %sn", temp->data);
printf ("%s <<<n", temp->data);
printf("tempnext is %sn", temp->next->data);
temp = temp->next;
printf("temp is %sn", temp->data);
while (temp != control) {
printf("tempnext is %sn", temp->next->data);
printf ("%s ", temp->data);
temp = temp->next;
printf("temp is %sn", 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 Listn");
if (s == 0) {
}
else if (s != 0) {
printlist(thering);
}
//press n to add a new item
if (y == 'n') {
s = 1;
printf("Add your itemn");
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 lettern");
getch();
}
}
printf ("Shopping Completedn");
}
插入第二个节点时会有一个圆圈,节点指向自身。这就是为什么它将无限期地打印最后一项。更改插入代码如下:
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 %sn", newNode->data);
}