繁体   English   中英

双链表删除功能错误

[英]Doubly Linked List Delete Function Error

我已经写了下面的程序,但是问题出在Delete函数上。每当我试图删除第一个位置的值时,整个列表就会丢失,不知道为什么。如果我尝试在那之后显示列表,那么会出现一些垃圾值该功能可以在其他位置正常工作。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>

struct Student
{
int rno;
char name[20];
struct Student *next;
struct Student *prev;
};

void Display(struct Student *head)
{
assert(head!=NULL);

while(head!=NULL)
{
    printf("%d\t%s\t",head->rno,head->name);
    head=head->next;
}

}

struct Student *Insert(struct Student *head,const int position,const int rno,const char name[])
  {

//printf("%s\n",__FUNCTION__);
struct Student *temp=(struct Student *)malloc(sizeof(struct Student));      
struct Student *traverse=head;  
int pos=position;

if(temp==NULL)
    exit(-1);
temp->rno=rno;
strcpy(temp->name,name);
temp->next=NULL;
temp->prev=NULL;
//  printf("%s\n",__FUNCTION__);
    if(pos==1)
    {
        if(head==NULL)
        {
            head=temp;
        }   
        else
        {
            temp->next=head;
            head->prev=temp;
            head=temp;
        }
    }
    else
    {   
        for(traverse=head,pos=position;traverse->next!=NULL&&pos-2!=0;traverse=traverse->next,pos--);

        if(traverse==NULL || pos-2!=0)
        {
            printf("Invalid Position");
        }
        else
        {
            temp->next=traverse->next;
            if(temp->next!=NULL)
                temp->next->prev=temp;
            temp->prev=traverse;
            traverse->next=temp;
        }       
    }

return head;
}


void DeleteAll(struct Student *head)
{
struct Student *temp=head;
while(temp->next!=NULL)
{
    head=head->next;
    free(temp);
    temp=head;  
}

free(temp);
}


 void  Delete(struct Student *head,int pos)
{
assert(head!=NULL);
struct Student *temp=head;
struct Student *traverse=head;
int position=pos;

if(position==1)
{
    if(head->next!=NULL)        
        head=head->next;
    head->prev=NULL;
    temp->next=NULL;
    free(temp);

}
else
{
    while(traverse->next!=NULL&&position-1!=0)
    {
        traverse=traverse->next;
        position--;
    }

    if(traverse==NULL || position-1!=0)
    {
        printf(".............Invalid position..........\n");
    }
    else
    {
        traverse->prev->next=traverse->next;
        if(traverse->next)
            traverse->next->prev=traverse->prev;
    }
}
}


    struct Student *CreateStudentList(const int no_of_students)
{
struct Student *head=NULL;
int i;
int rno;
char name[20];
for(i=0;i<no_of_students;i++)
{
    printf("Enter roll number and name:");
    scanf("%d%s",&rno,name);
    head=Insert(head,i+1,rno,name);
}
return head;
  }

    void SimulateDoublyLinkedList()
  {
struct Student *cdscpp2013=NULL;
int no_of_students;
int choice,rno,position;
char name[20];

while(choice!=5)
{
    if(NULL==cdscpp2013)
    {
        printf("Enter number of students:");
        scanf("%d",&no_of_students);
        cdscpp2013=CreateStudentList(no_of_students);
    }
    else
    {
        printf("\nMenu Operations\nPress 1 for Insert\nPress 2 for Delete\nPress 3 for DeleteAll\nPress 4 for Display\nPress 5 for Exit\nEnter your choice:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                printf("Enter roll number and name to ininsert:");
                scanf("%d%s",&rno,name);
                printf("Enter position to insert:");
                scanf("%d",&position);
                cdscpp2013=Insert(cdscpp2013,position,rno,name);
                break;
            case 2:
                printf("Enter position to delete:");
                scanf("%d",&position);
                Delete(cdscpp2013,position);
                break;
            case 3:
                DeleteAll(cdscpp2013);
                break;
            case 4:
                Display(cdscpp2013);
                break;
            case 5:
                exit(1);
            default:
                printf("Invalid choice....Please Enter proper option.....");
        }
    }
}

}

int main()
{
SimulateDoublyLinkedList();
}

删除列表的第一个元素时,调用者必须更新其对列表的引用。 因此,您的Delete函数必须将指针返回到结果列表的第一个元素。

在这种操作中,我还发现使用指向指针的指针很有用...这将简化您的代码。

尝试删除它应该可以工作-如temp = head,temp-> next = null将使整个列表消失//只删除temp->next = null;

if(位置== 1){if(head-> next!= NULL)
head = head-> next; head->上一个= NULL; 自由(临时);

}

编辑-

使用make delete函数返回头指针,在切换情况下使用此功能

cdscpp2013=Delete(cdscpp2013,position);

代替这个

Delete(cdscpp2013,position);

然后将其更改为deleteall函数-它应该起作用:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM