簡體   English   中英

我在C中的FIFO的鏈接列表中的彈出功能不起作用

[英]Pop function in from my linked list of FIFO in C is not working

再過幾天,我將參加關於C的非常重要的考試。我的出色老師正在幫助我,並為我提供了這一測試。 在這里,我必須使函數“ pop”。 我幾乎整個周末都嘗試了一下,但是仍然無法正常工作,這讓我感到沮喪。 也許你們可以幫助我。 我希望彈出是FIFO。

#include <stdio.h>

typedef struct test NODE;
typedef NODE *NV;

struct test
{
  int data;
  NV next;
};

int empty(NV);
int geeftop(NV);
void pop(NV, NV *, NV *);
void push(NV *, NV *, int);

int main(void)
{
  int k, number;
  int controle;
  NV start;
  NV staart;
  staart = NULL;
  start = NULL;

  do
  {
    system("CLS");
    printf("\nKeuzemenu:\n");
    printf("[1] = ADD.\n");
    printf("[2] = DELETE.\n");
    printf("[3] = IS IT EMPTY?.\n");
    printf("[4] = Stop.\n\n");
    printf("type your number: ");

      do
      {
        scanf("%d", &k);
      } while (k <= 0 || k >= 5);

      switch (k)
      {
        case 1: printf("give a number: ");
                scanf("%d", &number);
                push(&start, &staart, number);
                break;
        case 2: if (!empty(start))
                {
                  pop(start, &start ,&staart);
                  printf("\ntop is deleted.\n\n");
                }
                else
                  printf("\nerror.\n\n");
                system("PAUSE");
                break;
        case 3: controle = empty(start);
                if (controle == 1)
                  printf("\nits emtpy.\n\n");
                else
                  printf("\nniet empty.\n\n");
                system("PAUSE");
                break;
        default: break;
      }
  } while (k != 4);

  system("PAUSE");
  return 0;
}

int empty(NV phuidige)
{
  return (phuidige == NULL);
}

void pop(NV phuidige2, NV *phuidige, NV *pstaart)
{
  if ((phuidige2) -> next  == NULL)
  {
    NV weg;
    NV weg2;
    weg = *pstaart;
    weg2 = *phuidige;
    pstaart = NULL;
    phuidige2 = NULL;
    free (pstaart);
    free (phuidige2);
  }
  else
  {
    while ((phuidige2) ->  next -> next != (NULL))
    {
      phuidige2 = phuidige2 -> next;
    }
  }

  {
    phuidige2 -> next  = NULL;
    NV weg;
    weg = *pstaart;
    *pstaart = NULL;
    free (weg);
  }
}

void push(NV *phuidige, NV *pstaart, int x)
/* pre er is ruimte op de stack;
   post stack(post) = stack(pre) U {x}; */
{
  NV hulp;

  hulp = malloc(sizeof(NODE));
  hulp -> data = x;
  hulp -> next = *phuidige;
  *phuidige = hulp;

  if (*pstaart == NULL)
  {
    *pstaart = hulp;
  }
  else
  {
  }
}

您實際上應該檢查scanf的返回值,k的值未初始化。 基本上pop應該只返回您的頭部指針,然后將頭部指針前進到下一個元素-進行時檢查是否為null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM