Single-chain table creation, output, inverted Rotation

Source: Internet
Author: User

Form of declaring struct types:

Sturct struct type name

{

Member columns;

}

 

Three methods for defining struct type variables:

1. type name + variable name;

 

2. struct name

{

Member List

} Variable table column;

 

3. sturct

{

Member List

} Variable name list; // rarely used

 

Usage of typedef in struct

 

Typedef struct
{Char input;
A * p_input;
} A, * B;

 

Equivalent

 

Typedef struct
{Char input;
A * p_input;
} * B;

B is declared as the struct pointer type (A *): typedef A * B;

 

For example, typedef char * string // declare string as the character pointer type

String P, S [10]; // P is the character pointer variable, and s is the pointer array (including 10 elements)

 

The procedure is as follows:

# Include <stdio. h>
# Include <malloc. h>

Typedef struct lnode {
Int data;
Struct lnode * next;
} Lnode, * linklist;

 

Linklist createlist_l (linklist L, int N) // create a single-chain table
{
L = (linklist) malloc (sizeof (lnode ));
L-> next = NULL;
Linklist P;
For (INT I = N; I> 0; I --){
P = (linklist) malloc (sizeof (lnode ));
Scanf ("% d", & (p-> data ));
P-> next = L-> next;
L-> next = P; // insert to the header
}
Return L;
}

Void outputlist_l (linklist L, int N) // output the value of a single-chain table
{
Linklist P;
P = L-> next;
For (INT I = 0; I <n; I ++ ){
Printf ("% d", p-> data );
P = p-> next;
}
Printf ("/N ");
}

 

Linklist inversion (linklist L) // inverted linked list
{
Linklist newp = NULL, nowp, oldp; // defines three node tags
Oldp = L-> next;
While (oldp ){
Nowp = oldp;
Oldp = oldp-> next;
Nowp-> next = newp;
Newp = nowp;
}
L-> next = newp;
Return L;
}

 

Void main ()
{
Linklist L;
L = createlist_l (L, 4 );
Outputlist_l (L, 4 );
Int I;
L = inversion (L );
Outputlist_l (L, 4 );

}

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.