C-Language implementation single-linked list-version 02

Source: Internet
Author: User

The list we implemented in the C language Single-link list-version 01 is very simple;

But it's very helpful to understand a single-linked list, at least that's what I think;

Simple can not be more simple things are not so practical, so we have to change the scale of the next;

Problem

1, if the data a lot of what to do, 100,000 nodes, this main function to write how long ah ...

2, the way of this connection is too dirt! What if a node forgets to connect to the next ...

3, if I want to know how long this node is, do you want to count it every time ...

4, if I want to add a node in the tail, is not climbing to climb to the tail to ah ...

This is the problem raised in the simple version, and then they are solved;

Solution

First of all, define our data field, since it is a person, then put the height and weight on the human body it;

struct person_ {    int  hight;     int weight;} person;

and continue to define our nodes;

struct Node_ {    * data;     struct node_ * next;} Node;

All right! Next we want to have a function to connect the nodes together and fill in the data fields (the height and weight of the person);

Node * Create_node (void *data) {    * tmp = (node*)malloc(sizeof(node)) ;     if (TMP) {        tmp->data = data;        TMP->next = NULL;    }        return tmp;}  

Its intentions are obvious, just give me a person's height and weight I will return a node to you;

We can first test our own program to see if there is any problem, and then continue;

intMainvoid) {Person A= { -, the}; Node* A = Create_node (&a); Person B= { -, the}; Node* B = Create_node (&b); Person C= { the, the}; Node* C = Create_node (&c); A->next =B; B->next =C; Node* we=NULL; We=A;  while(We! =NULL) {printf ("%d--%d\n",we->data->hight,we->data->weight); We= we->Next; }       return 0;}

Look at the results:

Seems to have progressed more than the previous version;

But what can be said, the guts of the list we wrote in! But its intestines are still outside, disgusting:);

If not, understand, you see in the main function those disgusting steps still in ... ;

All right! Solve it;

struct link_list_ {    * head;     * tail;     int size;} Link_list;

In order to better manage our list, we need to know its head and tail and how long it is;

Therefore, the above structure should be temporarily so written;

All right! With the structure, the next step is to initialize it! It needs to be initialized like any other Int,char type before it is used;

(Like any baby who likes to drink milk, ha)

Link_list * Init_list (void)            {    * lst = (link_list*)malloc(sizeof( link_list));    LST->head = Lst->tail = NULL;    LST0;     return lst;}

After initialization, we need to add some nodes to it;

The way the list is managed here is simple, it only remembers the head and tail of the list and the length, does not care about what data you deposit in it;

(like, the school's dormitory administrator, it only know to hold the door of the dormitory key, no matter how you sleep in the dorm---in fact some tube is very strict:);

All right! We need a function to insert the node (let our intestines go with the guts.

Our insert data function is as follows:

void Insert (link_list *lst,void *data) {    * New_node = create_node (data);     if 0 ) {        lst->tail = New_node;   // tip:01     }       new_node->next=lst->head;    LST->head = new_node;    LST->size++;}

Then you can test our code!

intMainvoid) {link_list* Root =init_list (); Person A= { -, the}; Person B= { -, the}; Person C= { the, the}; Insert (Root,&a); Insert (Root,&b); Insert (Root,&c); Node* we=NULL; We= root->Head;  while(We! =NULL) {printf ("%d--%d\n",we->data->hight,we->data->weight); We= we->Next; }    return 0;}

Look at the results:

Note that our output order is not the same as in the previous order!

What's going on here? Think about it;

This question does not tell you first, next to see our list is not properly managed;

First, look at how long the chain is in the end.

{link_list* Root =init_list (); Person A= { -, the}; Person B= { -, the}; Person C= { the, the}; Insert (Root,&a); Insert (Root,&b); Insert (Root,&B); Node* we=NULL; We= root->Head;  while(We! =NULL) {printf ("%d--%d\n",we->data->hight,we->data->weight); We= we->Next; } printf ("The size of the link-list is:%d\n",root->size); return 0;}

Look at the results:

Look at the tail and head node, is not easy to tell us;

printf ("Thesize of the link-list is:%d\n",root->size);    printf ("Thehead guy is%d--%d\n", root->head->data->hight,root->head- >data->weight);    printf ("Thelast guy is%d--%d\n",root->tail->data->hight,root-> tail->data->weight);     return 0;

All right! Look at the results again:

All right! The result is as we expect;

Observation found that our last two sentences are root->head->data->hight and so on, very long, here to introduce a simple way of writing;

#define Go_h (RT,HW) (RT->HEAD->DATA->HW)    //Go_h:go head; Rt:root; Hw:hight,weight#define go_t (RT,HW) (RT->TAIL->DATA->HW)    //go_t:go tail; Rt:root; Hw:hight,weight

And then our code can be written like this!

    printf ("Thehead guy is%d--%d\n", Go_h (root,hight), Go_h (Root,weight));    printf ("Thelast guy is%d--%d\n", go_t (root,hight), go_t (Root,weight));

All right! Basically done!

All codes are as follows:

#include <stdio.h>#include<stdlib.h>#defineGo_h (RT,HW) (RT-&GT;HEAD-&GT;DATA-&GT;HW)//Go_h:go Head; Rt:root; Hw:hight,weight#definego_t (RT,HW) (RT-&GT;TAIL-&GT;DATA-&GT;HW)//Go_t:go Tail; Rt:root; Hw:hight,weighttypedefstructPerson_ {inthight; intweight;} Person;typedefstructNode_ { person*data; structNode_ *Next;} Node;typedefstructLink_list_ {Node*Head; Node*tail; intsize;} Link_list; Node* Create_node (void*data) {Node* tmp = (node*)malloc(sizeof(Node)); if(TMP) {tmp->data =data; TMP->next =NULL; }    returntmp;} Link_list* Init_list (void) {link_list* lst = (link_list*)malloc(sizeof(link_list)); LST->head = lst->tail=NULL; LST->size =0; returnlst;}voidInsert (Link_list *lst,void*data) {Node* New_node =Create_node (data); if(Lst->size = =0) {LST->tail = New_node;//tip:01} new_node->next=lst->Head; LST->head =New_node; LST->size++;}intMainvoid) {link_list* Root =init_list (); Person A= { -, the}; Person B= { -, the}; Person C= { the, the}; Insert (Root,&a); Insert (Root,&b); Insert (Root,&c); Node* we=NULL; We= root->Head;  while(We! =NULL) {printf ("%d--%d\n",we->data->hight,we->data->weight); We= we->Next; } printf ("The size of the link-list is:%d\n",root->size); printf ("The head guy is %d--%d\n", Go_h (root,hight), Go_h (Root,weight)); printf ("The last guy is %d--%d\n", go_t (root,hight), go_t (Root,weight)); return 0;}

But I have to tell you, it still has a lot of flaws;

We'll talk about it next time.

C-Language implementation single-linked list-version 02

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.