【问题标题】:delete multiple stack lists in C删除C中的多个堆栈列表
【发布时间】:2015-04-06 18:06:50
【问题描述】:

我正在尝试删除单链表堆栈实现。 我的程序有一个堆栈数组。

struct node
{
    int data;
    struct node *next;
}*start=NULL,*current;

在我的主要内容中,我有类似的东西。

void main()
{
        struct node top[99];
        //Input data here for stack top
        //Push function
        //This is where I implement stack and SLL
        printf("\n\nDo you want to try again?(Y/N) ");
        fflush(stdin);
        choice=getch();
        if(choice=='y' || choice=='Y')
        {
            deleteall();//delete the sinly linked list
                         //which is working fine. no problem

            for(y=0;y<n;y++)
            {
                delStack(&top[y]);//here delete the stack sll.


            }
        }

问题是当我输入“Y”重试时。它会冻结或什么的。 在我实现 delStack 函数之前一切都很好。

void delStack(struct node **head)
{
    struct node *ptr;
    ptr=*head;
    while(head!=NULL)
    {
        ptr=*head;
        ptr=ptr->next;//when i try head=head->next why does it give me non portable pointer conversion?
        free(ptr);
    }
}

编辑

这是我的程序的全部代码。现在它正在工作,但在下一次尝试时,它会打印我输入的无限循环。

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
}*start=NULL,*current;


int Sempty(struct node *temp) {
   if (temp == NULL)
      return 1;
   else
      return 0;
}

struct node * get_node(int item) {
   struct node * temp;
   temp = (struct node *) malloc(sizeof(struct node));
   if (temp == NULL)
      printf("\nMemory Cannot be allocated");
   temp->data = item;
   temp->next = NULL;
   return (temp);
}

void Push(int Item, struct node **top) {
   struct node *New;
   struct node * get_node(int);
   New = get_node(Item);
   New->next = *top;
   *top = New;
}


void Display(struct node **head) {
   struct node *temp;

   temp = *head;

   if (Sempty(temp))
      printf("\n");
   else {
      while (temp != NULL) {
        //printf("\t\t");       
   //        gotoxy(10,x);
         printf("%d ", temp->data);
         temp = temp->next;
     //    x++;
      }
   }
   printf("\n");
   //getch();
}

void display(int divi)
{
    struct node *temp;
    int x=2;
    temp=start;
    while(temp!=NULL)
    {
    gotoxy(1,x);
    printf("%d-%d",temp->data-divi,temp->data-1);
    temp=temp->next;
    x++;
    }

    //printf("\n\n");
}


void creat(int num)
{
      struct node *new_node;

      new_node=malloc(sizeof(struct node));


      new_node->data=num;
      new_node->next=NULL;

      if(start==NULL)
      {
      start=new_node;
      current=new_node;

      }
      else
      {
      current->next=new_node;
      current=new_node;
      }
      //printf("%d->",new_node->data);
}

void deleteall()
{
    struct node *ptr;

    ptr=start;
    while (start!=NULL)
    {
        ptr=start;
        start=start->next;
        free(ptr);
    }

}

void delStack(struct node **head)
{
    struct node *ptr;
    struct node *temp;
    ptr=*head;
    while(ptr!=NULL)
    {
        temp = ptr;
        ptr=ptr->next;
        free(temp);
    }
}

void main()
{

    int binrange,max=100,n,i,divi,j,k,l,flag=0,y,x;
    int inp[5];
    char choice;
    struct node *top[99]={0};
    while(flag!=1)
    {
        x=2;
        k=0;
        clrscr();
        printf("enter 5 numbers:\n");
        for(i=0;i<5;i++)
        {
            scanf("%d",&inp[i]);
        }
        printf("\nenter range: ");
        scanf("%d",&binrange);
        n=max/binrange;
        divi=max/n;
        clrscr();
        printf("BINRANGE\tDATA");   
        for(i=0;i<=max;i++)
        {
            if(i%divi==0 && i>0)
            {
                //create nodes here
                //store i into nodes
                creat(i);



                for(j=0;j<5;j++)
                {
                    if(inp[j]<i && inp[j]>=i-divi)
                    {
                        Push(inp[j],&top[k]);
                    }
                }
                if(k<n)
                {
                //printf("\n");
                gotoxy(17,x);   
                Display(&top[k]);
                }
                k++;
                x++;

            }


        }

        display(divi);
        printf("\n\nDo you want to try again?(Y/N) ");
        fflush(stdin);
        choice=getch();
        if(choice=='y' || choice=='Y')
        {
            deleteall();

            for(y=0;y<n;y++)
            {
                delStack(&top[y]);


            }
        }
        else
        {
            flag=1;
        }



    }

}

【问题讨论】:

  • 显示一个最小的工作示例。

标签: c stack singly-linked-list


【解决方案1】:
struct node *temp = NULL;
while(ptr!=NULL)
{
    temp = ptr;
    ptr=ptr->next;
    free(temp);
}

你有一个无限循环

【讨论】:

  • 考虑一下。但是您提供的上述代码仍然无法正常工作。
  • @PaulR 谢谢先生,我错过了基础知识。
  • 它的工作。现在我有另一个问题。在我第一次尝试时它正在工作,但是当我再次尝试时,它会打印出无限的值。
  • @reader 不要这么快下结论..确实会发生错误..我正在解决手头的实际问题是无限循环..所以写了导致UB的代码我立即修复了它..
猜你喜欢
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
  • 2010-10-19
  • 1970-01-01
  • 2013-02-16
  • 2015-03-08
相关资源
最近更新 更多