列表不断打印

时间:2015-12-07 03:13:17

标签: c pointers data-structures linked-list nodes

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

#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");
}

1 个答案:

答案 0 :(得分:1)

插入第二个节点时有一个圆圈,节点指向自身。这就是它将无限期打印最后一项的原因。像这样更改插入代码:

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);
}