/ *
Time: October 13, 2019 11:16:02
Note: Some common operations linked list
* /
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
Time: October 13, 2019 11:16:02
Note: Some common operations linked list
* /
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
the Node struct typedef
{
int Data; // data field
struct Node * pNext; // pointer field
} NODE, * PNODE; // NODE equivalent to struct Node, PNODE equivalent to struct Node *
{
int Data; // data field
struct Node * pNext; // pointer field
} NODE, * PNODE; // NODE equivalent to struct Node, PNODE equivalent to struct Node *
//函数声明
PNODE create_list(void);
void traverse_list(PNODE pHead);
bool is_empty(PNODE pHead);
int length_list(PNODE);
bool insert_list(PNODE,int,int);
bool delete_list(PNODE,int,int*);
void sort_list(PNODE);
PNODE create_list(void);
void traverse_list(PNODE pHead);
bool is_empty(PNODE pHead);
int length_list(PNODE);
bool insert_list(PNODE,int,int);
bool delete_list(PNODE,int,int*);
void sort_list(PNODE);
main int (void)
{
pNode PHEAD = NULL; // * equivalent to the Node PHEAD = NULL struct;
PHEAD create_list = (); // create_list () Function: to create a single chain acyclic, and the head of the list junction address points paid to PHEAD
traverse_list (PHEAD);
IF (is_empty (PHEAD))
{
the printf ( "list is empty \ n-!");
}
the else
{
the printf ( "the list is not empty \ n-!");
}
length_list length = int (PHEAD);
the printf ( "length of list:% d \ n", length );
the printf ( "length of list:% d \ n", length );
( "sorted,") the printf;
sort_list (PHEAD);
traverse_list (PHEAD);
IF (insert_list (PHEAD, 4,26))
{
the printf ( "inserted node,");
traverse_list (PHEAD);
}
the else
{
the printf ( "insert node fails \ n-!");
}
sort_list (PHEAD);
traverse_list (PHEAD);
IF (insert_list (PHEAD, 4,26))
{
the printf ( "inserted node,");
traverse_list (PHEAD);
}
the else
{
the printf ( "insert node fails \ n-!");
}
int Val;
IF (delete_list (PHEAD,. 4, & Val))
{
the printf ( "delete elements: the% d, delete nodes,", Val);
traverse_list (PHEAD);
}
the else
{
the printf ( "Delete node fails! \ n-");
}
return 0;
IF (delete_list (PHEAD,. 4, & Val))
{
the printf ( "delete elements: the% d, delete nodes,", Val);
traverse_list (PHEAD);
}
the else
{
the printf ( "Delete node fails! \ n-");
}
return 0;
}
Create_list pNode (void)
{
int len; // used to store the number of active nodes
printf ( "Please enter your list node number to be generated: len =");
Scanf ( "% D", & len);
{
int len; // used to store the number of active nodes
printf ( "Please enter your list node number to be generated: len =");
Scanf ( "% D", & len);
int val; // for temporarily storing values input by the user node
// assigned a valid data is not assigned the first node, pHead a head pointer, pHead points to a first node
pNode PHEAD = (pNode) the malloc (the sizeof (NODEs));
IF (PHEAD == NULL)
{
the printf ( "allocation fails, the program terminates \ n-!");
Exit (-1);
}
pNode PHEAD = (pNode) the malloc (the sizeof (NODEs));
IF (PHEAD == NULL)
{
the printf ( "allocation fails, the program terminates \ n-!");
Exit (-1);
}
PNODE pTail = pHead; // pTail always point to the end node represents
pTail-> pNext = NULL;
pTail-> pNext = NULL;
for (int I = 0; I <len; I ++)
{
the printf ( "Enter a value of% d nodes:", I +. 1);
Scanf ( "% d", & Val);
// every cycle, PNEW will create a new node
pNode PNEW = (pNode) the malloc (the sizeof (nODEs));
IF (PNEW == NULL)
{
the printf ( "allocation fails, the program terminates \ n-!");
Exit (-1);
}
{
the printf ( "Enter a value of% d nodes:", I +. 1);
Scanf ( "% d", & Val);
// every cycle, PNEW will create a new node
pNode PNEW = (pNode) the malloc (the sizeof (nODEs));
IF (PNEW == NULL)
{
the printf ( "allocation fails, the program terminates \ n-!");
Exit (-1);
}
pNew-> Data = Val;
pTail-> pNext = PNEW; // linked to the rear of the PNEW PHEAD
pNew-> pNext = NULL;
PTAIL PNEW =;
}
pTail-> pNext = PNEW; // linked to the rear of the PNEW PHEAD
pNew-> pNext = NULL;
PTAIL PNEW =;
}
Pet return;
}
traverse_list void (pNode PHEAD)
{
pNode P = PHEAD;
the printf ( "the linked list is:");
the while (p-> pNext = NULL!)
{
the printf ( "% D", p-> pNext-> Data) ;
P = p-> pNext;
}
the printf ( "\ n-");
{
pNode P = PHEAD;
the printf ( "the linked list is:");
the while (p-> pNext = NULL!)
{
the printf ( "% D", p-> pNext-> Data) ;
P = p-> pNext;
}
the printf ( "\ n-");
}
bool is_empty(PNODE pHead)
{
if(NULL == pHead->pNext)
{
return true;
}
else
{
return false;
}
}
{
if(NULL == pHead->pNext)
{
return true;
}
else
{
return false;
}
}
int length_list(PNODE pHead)
{
int len = 0;
PNODE p = pHead->pNext;
while(p != NULL)
{
p = p->pNext;
len++;
}
return len;
}
{
int len = 0;
PNODE p = pHead->pNext;
while(p != NULL)
{
p = p->pNext;
len++;
}
return len;
}
void sort_list(PNODE pHead)
{
{
PNODE p,q;
int t;
for(p = pHead->pNext;p != NULL;p = p->pNext)
{
for(q = p->pNext;q != NULL;q = q->pNext)
{
if(p ->data > q -> data)
{
t = p->data;
p->data = q->data;
q->data = t;
}
}
}
return ;
int t;
for(p = pHead->pNext;p != NULL;p = p->pNext)
{
for(q = p->pNext;q != NULL;q = q->pNext)
{
if(p ->data > q -> data)
{
t = p->data;
p->data = q->data;
q->data = t;
}
}
}
return ;
}
// inserted in front of the list pointed to PHEAD pos nodes a new node, the node value is val, and the value is from the start pos. 1
BOOL insert_list (pNode PHEAD, int pos, int Val)
{
int I 0 =;
pNode P = PHEAD;
the while (= NULL && P-POS. 1> I!)
{
P = p-> pNext;
I ++;
}
BOOL insert_list (pNode PHEAD, int pos, int Val)
{
int I 0 =;
pNode P = PHEAD;
the while (= NULL && P-POS. 1> I!)
{
P = p-> pNext;
I ++;
}
if(i > pos-1 || p == NULL)
{
return false;
}
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pNew)
{
printf("动态分配内存失败!\n");
exit(-1);
}
pNew->data = val;
PNODE q = p->pNext;
p->pNext = pNew;
pNew->pNext = q;
return true;
}
{
return false;
}
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pNew)
{
printf("动态分配内存失败!\n");
exit(-1);
}
pNew->data = val;
PNODE q = p->pNext;
p->pNext = pNew;
pNew->pNext = q;
return true;
}
// find a node before the node is removed
BOOL delete_list (pNode PHEAD, POS int, int * pVal)
{
int I = 0;
pNode P = PHEAD;
the while (! P-> pNext = NULL && I <-POS. 1)
{
P = p-> pNext;
I ++;
}
IF (I> POS. 1-p-== NULL ||> pNext)
{
return to false;
}
pNode Q = p-> pNext;
* pVal Q- => Data;
/ / deleting nodes behind a node p
p-> = p-pNext> pNext-> pNext;
Free (Q);
Q = NULL;
return to true;
}