0

我已经编写了以下程序,但问题出在删除函数中。每当我试图删除第一个位置的值时。整个列表都丢失了,不知道为什么。如果我在那之后尝试显示列表,那么一些垃圾值是打印。该功能适用​​于其他职位。

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

2 回答 2

2

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

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

于 2013-10-05T13:28:38.553 回答
0

试试这个删除这应该工作 - 因为 temp = head , temp->next =null 将使整个列表消失//刚刚删除temp->next = null;

if(position==1) { if(head->next!=NULL)
head=head->next; 头->上一页=空;免费(临时);

}

已编辑——

使删除函数返回头指针,并在开关情况下使用此

cdscpp2013=Delete(cdscpp2013,position);

而不是这个

Delete(cdscpp2013,position);

然后为 deleteall 函数更改相同的内容 - 它应该可以工作:)

于 2013-10-05T13:35:21.727 回答