Single-chain table operation in C Language

Source: Internet
Author: User

# Include <stdio. h>

# Include <malloc. h>


Typedef struct Node {

Int data;

Struct Node * next;

} Node, * Link;


Node * init (int len) // initialize the linked list

{

Int I;

Node * head, * p, * q;


Head = (Link) malloc (sizeof (Node ));

P = head;

For (I = 0; I <len; I ++)

{

Q = (Link) malloc (sizeof (Node ));

Q-> data = 2 * I;

Q-> next = NULL;

P-> next = q;

P = q;

Head-> data ++;

}

Return head;

}


Node * insert (Node * hm, int x) // insert a Node

{

Node * m, * tmp;

M = hm-> next;

Tmp = (Link) malloc (sizeof (Node ));

Tmp-> data = x;

Tmp-> next = NULL;

While (m-> next! = NULL)

{

If (x> m-> data & x <= m-> next-> data)

{

Tmp-> next = m-> next;

M-> next = tmp;

Hm-> data ++;

}

M = m-> next;

}

If (x> m-> data)

{

M-> next = tmp;

Hm-> data ++;

}

Return hm;

}


Node * delete (Node * hm, int x) // delete a Node

{

Int bool = 0;

Node * m, * tmp;

M = hm;

While (m-> next! = NULL)

{

If (m-> next-> data = x)

{

Tmp = m-> next;

M-> next = tmp-> next;

Free (tmp );

Bool = 1;

Hm-> data --;

}

If (m! = NULL)

{M = m-> next ;}

If (m = NULL) {break ;}

}

If (bool = 0) {printf ("there is no this kind of node: % d \ n", x );}

Return hm;

}


// Linked list reverse http://blog.csdn.net/niuer09/article/details/5961004

Node * reverse (Node * hm) // reverse sequence of the linked list

{

If (hm-> next = NULL | hm-> next = NULL)

{Return hm ;}

Node * t = NULL, * p = hm-> next, * q = p-> next;

While (q! = NULL)

{

T = q-> next;

Q-> next = p;

P = q;

Q = t;

}

Hm-> next = NULL;

Hm-> next = p;

Return hm;

}


Void play (Node * m) // print the Node

{

While (m! = NULL)

{

Printf ("% d", m-> data );

M = m-> next;

}

Printf ("\ n ");

}


Void main (int argc, char * argv [])

{

Int len, insertnum, deletenum;

Node * l;

Len = atoi (argv [1]);

Insertnum = atoi (argv [2]);

Deletenum = atoi (argv [3]);

L = init (len );

L = insert (l, insertnum );

Play (l );

}


Related Article

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.