On the list of functions (sort, add, delete, determine the length, to determine whether the empty, traversal)

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Node
{
int // Data field Data
struct node * pnext; // pointer field
} node, * pnode; (semicolon not less) // node is equivalent to struct node pnode equivalent to struct node *

create_list pNode (void);
void traverse_list (pNode PHEAD);
BOOL is_empty (pNode PHEAD);
int length_list (pNode);
BOOL insert_list (pNode, int, int); // first parameter that list, the second one is that position, and the third represents the value
bool delete_list (pnode, int, int *); // first representation list, the second one is that node, and the third means to delete nodes into the third seed
void sort_list (pNode PHEAD);

int main (void)
{
pNode PHEAD = NULL; // node * equivalent to PHEAD = NULL struct;
PHEAD create_list = (); // create a single chain acyclic, the node and the head node address points paid PHEAD
traverse_list (PHEAD); // iterate


IF (is_empty (PHEAD))
the printf ( "list is empty \ n-!");
the else
the printf ( "the list is not empty \ n-");
int len = length_list (pNode PHEAD);
the printf ( "length of list is% d \ n-", len);
sort_list (pNode PHEAD);


if (delete_list (phead, 4, & val)) // save a function value for deletion, the applied address-identifier
the printf ( "Delete successfully \ n-");
the else
the printf ( "Delete failed \ n-");
return 0;
}
create_list pNode (void)
{
int len;
int I;
int Val; // for temporarily storing values input by the user node

// assigned to a valid data is not stored in the first node
pnode phead = (pnode) malloc ( sizeof (node)) // node itself generates the data type, a node, not pNode
IF (PHEAD == NULL)
{
the printf ( "allocation fails, the program terminates \ n-!");
Exit (-1);
}

pNode PTAIL PHEAD =;
ptail-> pNext = NULL; (length may be prevented from 0)
the printf ( "Please enter the number of the linked list of nodes that you need to generate: len =");
Scanf ( "% D", & len);
for ( 0 = I; I <len; I ++)
{
the printf ( "enter a value of% d nodes:", i + 1);
scanf("%d",&val);

Pnew = pNode (pNode) the malloc (the sizeof (Node));
IF (PHEAD == NULL)
{
the printf ( "allocation fails, the program terminates \ n-!");
Exit (-1);
}
pnew-> Data = Val; // go to the data temporarily stored in pnew
ptail-> pNext = pnew;
pnew-> pNext = NULL;
PTAIL = pnew; // make PTAIL from the original head node becomes equal pnew, equivalent displacement
}
return PHEAD;
}
void traverse_list (pNode PHEAD)
{
pNode P = phead-> pNext; P // pointer to the first active element
the while (P = NULL!)
{
the printf ( "% D", p-> Data);
P = p- > pnext; // pointer back shift corresponds to p
}
the printf ( "\ n-");
}

bool is_empty(pnode phead)
{
if(NULL=phead->pnext)
return ture;
else
return false;
}

int length_list(pnode phead)
{
pnode p=phead->next;
int len;
while(NULL!=p)
{
len++;
p=p->next;
}
return len;
}

void sort_list(pnode phead)//排序
{
int i,j,t;
int len=length_list(pnode phead);
pnode p,q;
for(i=0,p=phead->pnext;i<len-1;i++,p=p->pnext)
{
for(j=i+1,q=p->pnext;j<len;j++,q=q->pnext)
{
if(p->pdata>q->pdata)
t=p->pdata;
p->pdatd=q->pdata;
q->pdata=t;
}
}
}

// inserted in front of the list pointed to PHEAD pos node of a new node, the new node value val, and the value is from 1 pos starting
BOOL insert_list (pNode PHEAD, int pos, int Val)
{
int I 0 =;
pNode P = PHEAD;
the while (P = NULL && I! <-POS. 1)
{
P = P-pNext;
++ I;
}
IF (I> POS-P. 1 || == NULL)
return to false;
pNode Pnew = (pNode) the malloc (the sizeof (Node));
IF (Pnew == NULL)
{
the printf ( "dynamic memory allocation failure \ n-");
Exit (-1);
}
pnew-> Data = Val;
pNode Q = P -> pNext;
p-> pNext = Pnew;
pnew-> pNext = Q;

return ture;

}

BOOL delete_list (pNode PHEAD, int POS, int * PVAL)
{
int I = 0;
pNode P = PHEAD;
the while (NULL = p-> pNext && I <POS-. 1!)
{
P = P-pNext;
++ I;
}
IF (I> POS. 1-p-== NULL ||> Next)
return to false;
pNode = p-q> pNext;
* Q- PVAL => Data;

// delete nodes behind a node q
p-> pnext = p -> pnext-> pnext; // corresponding to q = p-> next-> next; change point q
Free (q);
q = NULL;

return ture;

}

Guess you like

Origin www.cnblogs.com/mingstatebird/p/12080744.html