July 2019 Wednesday 31 May (data structures)

2019 Nian 7 Yue 31 Wednesday, May

A . Doubly linked list

1. What are the characteristics doubly linked list is that?

There is only a one-way linked list node pointer successor next, so singly linked list can only refer to the future.

Doubly linked list node where there is not only the successor pointer next, but a precursor pointer prev, doubly linked list can either access point forward, the future can also be access nodes.

2. doubly linked list model?

struct list_node{

       / * Data field * /

       ....;

 

       / * Pointer field * /

       Precursor pointer ...;

       Successor pointer ...;

};

Example: Assume that a node is stored int type data, then how to define the structure of the node?

struct list_node{

       int a;

       struct list_node *prev;

       struct list_node *next;

};

Two . Doubly linked list of CRUD?

1. initialization list?

struct list_node *init_list_head(struct list_node *head)

{

       head = (struct list_node *)malloc(sizeof(struct list_node));

       if(head == NULL)

              printf("malloc head error!\n");

      

       head->prev = NULL;

       head->next = NULL;

      

       return head;

}

2. plug the end list?

int tail_add_list(struct list_node *head,int num)

{

       struct list_node *Node = NULL;

       Node = (struct list_node *)malloc(sizeof(struct list_node));

       if(Node == NULL)

              printf("malloc Node error!\n");

      

       Node->a = num;

      

       struct list_node *p = NULL;

       for(p=head;p->next!=NULL;p=p->next);

      

       p->next = Node;

       Node->next = NULL;

       Node->prev = p;

      

       return 0;

}

3. The head interpolated data?

int head_add_list(struct list_node *head,int num)

{

       struct list_node *Node = NULL;

       Node = (struct list_node *)malloc(sizeof(struct list_node));

       if(Node == NULL)

              printf("malloc Node error!\n");

      

       Node->a = num;

      

       Node->next = head->next;

      

       if(head->next != NULL)

              head->next->prev = Node;

      

       head->next = Node;

       Node->prev = head;

      

       return 0;

}

4. traverse the list?

int backward_show_list(struct list_node *head)

{

       struct list_node *p = NULL;

       for (p = head; p-> next = NULL;! p = p-> next); // time from the circulation , p points to the last node

      

       for(;p!=head;p=p->prev)

       {

              printf("p->a:%d\n",p->a);

       }

      

       return 0;

}

 

int forward_show_list(struct list_node *head)

{

       struct list_node *p = NULL;

       for(p=head->next;p!=NULL;p=p->next)

       {

              printf("p->a:%d\n",p->a);

       }

      

       return 0;

}

The search node?

int search_list_node(struct list_node *head,int num)

{

       struct list_node *p = NULL;

       for(p=head->next;p!=NULL;p=p->next)

       {

              if(p->a == num)

              {

                     printf("p->a:%d\n",p->a);

              }

       }

      

       return 0;

}

6. delete nodes?

int delete_list_node(struct list_node *head,int num)

{

       struct list_node *p = NULL;

       struct list_node *q = NULL;

      

       for(q=head,p=head->next;p!=NULL;q=p,p=p->next)

       {

              if(p->a == num)

              {

                     q->next = p->next;

                     if (p-> next! = NULL) // instructions to remove is not the last

                            p->next->prev = q;

                           

                     free(p);

              }

       }

      

       return 0;

}

int delete_list(struct list_node *head)

{

       struct list_node *p = NULL;

       struct list_node *q = NULL;

      

       for(p=q=head;p!=NULL;p=q)

       {

              q = p->next;

              free(p);

       }

      

       return 0;

}

Guess you like

Origin www.cnblogs.com/zjlbk/p/11278395.html