Algorithm-Data Structure-linked list

Source: Internet
Author: User
Algorithm-Data Structure-linked list-Linux general technology-Linux programming and kernel information. The following is a detailed description. /*************************************** *************************
Title: list. c
Author: Zhang Long
Time: 200508
**************************************** *************************/




# Include "stdio. h"
# Include "stdlib. h"



Typedef int ElementType;

Typedef struct _ listnode
{
ElementType data;
Struct _ listnode * next;
} Listnode;

Typedef struct _ list
{
Listnode * firstnode;
Int count;
} List;




/* = ======= */
Void init (list * l );
Int FreeList (list * l );
Int IsEmpty (list * l );
Int length (list * l );
Int insert (list * l, ElementType x, int position );
Int delete (list * l, ElementType x );
ElementType find (list * l, int position );
Int traverse (list * l );




/* = */
Void init (list * l)
{
L-> firstnode = 0;
L-> count = 0;
}

Int FreeList (list * l)
{

}

Int IsEmpty (list * l)
{
If (! L-> firstnode)
Return 1;
Else
Return 0;
}

Int length (list * l)
{
Return l-> count;
}

Int insert (list * l, ElementType x, int position)
{
Int I;
Listnode * node, * tmp;

If (! L-> firstnode ){
Node = l-> firstnode = (listnode *) malloc (sizeof (listnode ));
Node-> data = x;
Node-> next = 0;
}
Else {
If (position = 0 ){
Node = (listnode *) malloc (sizeof (listnode ));
Node-> data = x;
Node-> next = l-> firstnode;
L-> firstnode = node;
}
Else {
/* Find the parent node of the node at position */
For (I = 0; I
If (I = 0 ){
Tmp = l-> firstnode;
}
Else {
Tmp = tmp-> next;
}
}

Node = (listnode *) malloc (sizeof (listnode ));
Node-> data = x;
Node-> next = tmp-> next;
Tmp-> next = node;
}

}

L-> count ++;

Return l-> count;
}

Int delete (list * l, ElementType x)
{

}

ElementType find (list * l, int position)
{
Int I;
Listnode * node;

If (position = 0 ){
Node = l-> firstnode;
}
Else {
/* Find the parent node of the node at position */
For (I = 0; I
If (I = 0 ){
Node = l-> firstnode;
}
Else {
Node = node-> next;
}
}

Node = node-> next;
}

Return node-> data;
}

Int traverse (list * l)
{
Int I, j;
J = length (l );

Listnode * tmp;

For (I = 0; I
If (I = 0 ){
Tmp = l-> firstnode;
Printf ("% d", tmp-> data );
}
Else {
Tmp = tmp-> next;
Printf ("% d", tmp-> data );
}
}

Printf ("\ n ");

Return;
}


/* = =============== */
Int main (int argc, char * argv [])
{
Int I, j;
List * mylist;

Mylist = (list *) malloc (sizeof (list ));

Init (mylist );

For (I = 0; I <100; I ++ ){
J = I + 3;
Insert (mylist, j, I );
Printf ("% d", find (mylist, I ));
}

Printf ("\ n ");

Printf ("the length of list is % d \ n", length (mylist ));

Traverse (mylist );

If (IsEmpty (mylist ))
Printf ("the list is empty \ n ");
Else
Printf ("the list is not empty \ n ");

J = find (mylist, 15 );
Printf ("the 15th element of list is % d \ n", j );

}

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.