Linklist L and linklist *l for reference in linked list

Source: Internet
Author: User

The difference between linklist L and linklist *l in the list and (*l). Elem,l.elem L->next, (*l) the difference between->next
typedef struct node{
int elem;
struct node * NEXT;
}node,*linklist;

For linklist L:l is a pointer to the defined node struct, you can access the struct member by using the-e operator, that is, L->elem, and (*l) is a node-type struct, and you can access the struct member with the dot operator, that is (*l). Elem;

For linklist *l:l is a pointer to a defined node struct pointer, so (*L) is a pointer to the node struct, and you can access the struct member with an operator--(*l)->elem, of course, (**l) is the node struct. So you can use the dot operator to access struct members, i.e. (**l). Elem;

In the chain list operation, we often have to use the parameters of the chain table variable crop function, at this time, with linklist L or linklist *l is worth considering, a bad, function will appear logic error, the criteria is:
If the function will change the value of the pointer L, and you want the function to end the call to save the value of L, then you need to use linklist *l, so that the function is passed to the address of the pointer, after the end of the call, it is natural to change the value of the pointer;
If the function only modifies what the pointer points to, and does not change the value of the pointer, then the linklist L is used.

Let's say a concrete example!

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

typedef int ELEMTYPE;
typedef struct node{
Elemtype Elem;
struct Node * NEXT;
}node, * linklist;

Initialize the list, when the function call is complete, L will point to an empty list, which will change the value of the pointer, so use *L
void Initlist (linklist *l)
{
*l = (linklist) malloc (sizeof (Node));
(*l)->next = NULL;
}

Empty the list L, so l re-become empty linked list, function call will not change the value of the pointer L, will only change the pointer l point to the content (that is, the value of L->next)
void Clearlist (linklist L)
{
Linklist p;
while (P = l->next)
Free (p);
}

Destroy the linked list L, release the linked list L The requested memory, so that the value of L becomes null again, so it will change the value of L, with *l
void Destroylist (linklist *l)
{
Linklist p;
while (P = (*l)->next)
Free (p);
Free (*l);
*l = NULL;
}

int main ()
{
linklist L = NULL;
Initlist (&L);
Clearlist (L);
Destroylist (&L);
}

Linklist L and linklist *l for reference in linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.