简体   繁体   English

双链表查询

[英]Double Linked List query

so i have created a program which uses double linked list and performs some operations on it . 所以我创建了一个使用双链表并对其执行一些操作的程序。 The problem is that it displays garbage values at the end every time i try to create a linked list and then display it. 问题是,每次我尝试创建链表然后显示它时,它都会在末尾显示垃圾值。 whats wrong in my code?(sorry! for the bad indentations) if the linked list i created has elements say 15 and 16, it displays it as 15 16 25710 0 0 我的代码有什么问题?(对不起,缩进很不好!)如果我创建的链表具有元素15和16,它将显示为15 16 25710 0 0

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct dll
{
    int data;
    struct dll *llink;
    struct dll *rlink;
};
typedef struct dll *node;
node head=NULL;
void create();
int search(int u);
void insert(int num1);
void Delete(int num2);
void display();
node getnode();
int main()
{
    int i,num,o;
    while(1)
    {
        printf("\n1.create a list\n 2. insert before a search node\n 3. delete a node\n 4.display\n 5.exit\n");
        scanf("%d",&num);
        switch(num)
        {
        case 1 :
            create();
            break;
        case 2 :
            printf("enter the value before which you want to enter the node\n");
            scanf("%d",&i);
            insert(i);
            break;
        case 3 :
            printf("enter the value to be deleted\n");
            scanf("%d",&o);
            Delete(o);
            break;
        case 4 :
            printf("the linked list has :\n");
            display();
            break;
        case  5 :
            getch();
            exit(1);
        default :
            printf("enter the correct option\n");
            break;
        }
    }
}
node getnode()
{
    node temp1;
    temp1=(node)malloc(sizeof(node));
    temp1->llink=NULL;
    temp1->rlink=NULL;
    return temp1;
}
void create()
{
    node nn;
    int num,y;
    if(head==NULL)
        head=getnode();
    while(1)
    {
        printf("enter the data for the node");
        scanf("%d",&num);
        head->data=num;
        printf("do you want to create another node(1/0):\n");
        scanf("%d",&y);
        if(y==1)
        {
            nn=getnode();
            nn->rlink=head;
            head->llink=nn;
            head=nn;
            nn=NULL;
        }
        else
            break;
    }
}
void  insert (int num1)
{
    int i,n,k;
    node temp=head,nn;
    n=search(num1);
    if(n==0)
    {
        printf("element not present in the linked list");
    }
    if(n==1)
    {
        nn=getnode();
        printf("enter the data for the node");
        scanf("%d",&k);
        nn->data=k;
        nn->rlink=head;
        head->llink=nn;
        head=nn;
        nn=NULL;
    }
    else
    {
        for(i=2; i<=n; i++)
            temp=temp->rlink;
        nn=getnode();
        temp->llink->rlink=nn;
        nn->llink=temp->llink;
        nn->rlink=temp;
        temp->llink=nn;
    }
}
void Delete(int num2)
{
    node temp=head;
    int p,i;
    p=search(num2);
    if(p==0)
    {
        printf("no element is found");
    }
    if(p==1)
    {
        printf("the deleted element is %d",head->data);
        head=head->rlink;
        head->llink=NULL;
        free(temp);
    }
    else
    {
        for(i=2; i<=p; i++)
        {
            temp=temp->rlink;
        }
        temp->llink->rlink=temp->rlink;
        temp->rlink->llink=temp->llink;
        free(temp);
        temp=temp->rlink;
    }
}
int search(int u)
{
    node temp=head;
    int pos=0;
    if(u==head->data)
        return 1;
    else
    {
        while(temp!=NULL)
        {
            pos++;
            if(temp->data==u)
            {
                printf("element found\n");
                return(pos);
            }
            temp=temp->rlink;
        }
    }
    if(temp==NULL)
    {
        return 0;
    }
    return -1;
}
void display()
{
    node temp=head;
    while(temp!=NULL)
    {
        printf("%d\n",temp->data);
        temp=temp->rlink;
    }
}

This: 这个:

temp1=(node)malloc(sizeof(node));

is a major error. 是一个重大错误。 Since you're "hiding a star", and node is a typedef for a pointer type, you're not allocating enough memory. 由于您是“隐藏一颗星星”,而node是指针类型的typedef ,因此您没有分配足够的内存。 It should be: 它应该是:

node temp1 = malloc(sizeof *temp1);

But I really recommend against typedef ing a pointer away, it just makes things confusing. 但是我真的建议不要使用typedef将指针移开,这只会使事情变得混乱。

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

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