简体   繁体   English

如何在 C 中创建一个空链表?

[英]How do you create an empty linked list in C?

I have a C code, in which I am assigned to create an empty list.我有一个 C 代码,我被分配在其中创建一个空列表。 Although I create one, with Element = 0, it is still considered a list by the program, so it prints 0, when in reality I want it to be printed as NULL.虽然我创建了一个,元素 = 0,但它仍然被程序认为是一个列表,所以它打印 0,而实际上我希望它打印为 NULL。 Also, when I print the length of my list, it also comes out as 1, rather the 0 I'd be looking for.此外,当我打印列表的长度时,它也显示为 1,而不是我要查找的 0。

This is my code so far.到目前为止,这是我的代码。

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

    typedef int ElementType;

    struct Node;
    typedef struct Node *PtrToNode;
    typedef PtrToNode List;
    typedef PtrToNode Position;

    List CreateList();
    int IsEmpty(List L);
    int IsLast(List L);
    Position GetNode(List L,int idx);
    void FetalError(const char *msg);
   void PrintList(List L);

    struct Node {
        ElementType Element;
        Position Next;
        Position Previous;
    };

    List CreateList()
    {
        List L;
        L= (List)malloc(sizeof(struct Node));
        if(L==NULL) FetalError("Out of Memory");
        L->Element = 0;
        L->Next = NULL;
        L->Previous = NULL;
        return L;
    }

Position Insert(ElementType X, List L, Position P)
{
    Position Tmp;
    Tmp=(Position)malloc(sizeof(struct Node));
    if(Tmp==NULL) FetalError("Out of Memory");
    Tmp->Element=X;
    Tmp->Next=P->Next;
    P->Next=Tmp;
    return Tmp;
}

    int IsEmpty(List L){
        return(L->Next==NULL);
    }

    int IsLast(List P){
        return(P->Next==NULL);
    }

       void FetalError(const char *msg)
    {
        printf(msg);
        exit(-1);
    }

    PrintList(List L){

        while(L!= NULL){    
            printf("%d <-> ", L->Element);
            L=L->Next;
        }
        printf("\n");
    }

        int getCount(List L) 
{ 
    int count = 0;  // Initialize count 
    struct Node* current = L;  // Initialize current 
    while (current != NULL) 
    { 
        count++; 
        current = current->Next; 
    } 
        printf("%d number of elements", count); 
} 
    Position GetNode(List L, int idx){

        int i = 0; 
        while (L != NULL) 
        { 
            if (i == idx) 
                return(L); 
            i=i+1; 
            L= L->Next; 
        } 
        return;  
     } 

    Position Last(List L)
    { 
    return L;
    } 

    int main(int argc, char *argv[]) 
    {
        int i;
        ElementType X;
        List L;
        Position P, P1 , P2;

        srand(0);

        // creat an empty list
        printf("Creating an empty list\n");
        L = CreateList();
        PrintList(L);
        getCount(L);

        // Insert at the beginning
        printf("**(Inserting 5 random numbers at the beginning\n");
        for( i=0 ; i<5 ; i++ ) {
            Insert(rand(),L,L);
            PrintList(L);
        }
        P = GetNode(L,5);
        printf("The Element of 5th Node is %d\n",P->Element);
        getCount(L);

        return 0;
    }

you create an object with memory, so it wont be 0\null.你用 memory 创建一个 object,所以它不会是 0\null。 if you wish to get null object you should return null.如果你想得到 null object 你应该返回 null。

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

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