#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Node {
int data;
struct Node * pNode
} NODE,* PNODE;
PNODE create_list();
void show_list(PNODE);
int is_emp(PNODE);
int length_list(PNODE);
int insert_list(PNODE,int,int);
int delete_list(PNODE,int,int *);
void sort_list(PNODE);
int main(void) {
PNODE p = create_list();
sort_list(p);
insert_list(p,3,99);
show_list(p);
int i;
delete_list(p,3,&i);
show_list(p);
printf("
");
printf("%d",i);
return 0;
}
int delete_list(PNODE phead ,int pos,int * val){
PNODE p = phead;
int i =0;
while(NULL!=p && i<pos-1) {
p=p->pNode;
i++;
}
if(i>pos-1|| NULL == p){
return 0;
}
*val = p->pNode->data;
p->pNode=p->pNode->pNode;
return 1;
}
int insert_list(PNODE phead,int pos,int val){
PNODE p = phead;
int i =0;
while(NULL!=p && i<pos-1) {
p=p->pNode;
i++;
}
if(i>pos-1|| NULL == p){
return 0;
}
PNODE pnew = (PNODE)malloc(sizeof(NODE));
PNODE temp;
pnew ->data=val;
temp = p->pNode;
p->pNode=pnew;
pnew->pNode = temp;
return 1;
}
void sort_list(PNODE phead) {
int i,j,k;
int len = length_list(phead);
PNODE p,q;
for(i=0,p=p->pNode;i<len-1;p=p->pNode,i++) {
for(j=i+1,q=p->pNode;j<len;q=q->pNode,j++) {
if(p->data > q->data) {
k=q->data;
q->data=p->data;
p->data=k;
}
}
}
return;
}
int is_emp(PNODE phead) {
if(phead -> pNode == NULL) {
return 1;
} else {
return 0;
}
}
int length_list(PNODE phead) {
int i =0;
PNODE p = phead->pNode;
while(NULL != p) {
p=p->pNode;
i++;
}
return i;
}
PNODE create_list() {
int length = 0;
int val=0;
int i =0;
printf("input the list len: ");
scanf("%d",&length);
PNODE pHead = (PNODE)malloc(sizeof(NODE));
PNODE tail=pHead;
tail->data=0;
tail->pNode = NULL;
if(NULL == pHead) {
printf("t is error");
}
for(i=0; i<length; ++i) {
printf("the %d node value: ",i+1);
scanf("%d",&val);
PNODE pnew = (PNODE)malloc(sizeof(NODE));
pnew ->data=val;
tail->pNode=pnew;
pnew->pNode=NULL;
tail=pnew;
}
return pHead;
}
void show_list(PNODE Plist) {
PNODE next = Plist->pNode;
while(NULL != next) {
printf("%d ",next->data);
next = next->pNode;
}
printf("
");
return;
}