1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct Node 5 { 6 int data; //数据域 7 struct Node * pNext;//指针域 8 }NODE,*PNODE; 9 10 //函数声明 11 PNODE creat_list(void); 12 void traverse_list(PNODE pHead); 13 bool is_empty(PNODE pHead); 14 int length_list(PNODE pHead); 15 bool insert_list(PNODE ,int,int); 16 bool delete_list(PNODE,int,int *);// int*表示删除的数字 17 void sort_list(PNODE); 18 19 20 int main (void) 21 { 22 PNODE pHead = NULL; 23 int val; 24 25 pHead = creat_list();//创建一个非循环单链表 ,并将该链表的头结点的地址付给pHead; 26 traverse_list(pHead); 27 insert_list(pHead,4,33); 28 traverse_list(pHead); 29 delete_list(pHead,5,&val); 30 traverse_list(pHead); 31 printf("删除的是%d ",val); 32 33 /* 34 sort_list(pHead); 35 traverse_list(pHead); 36 int len = length_list(pHead); 37 printf("链表的长度是:%d ", len); 38 /* if(is_empty(pHead)) 39 printf("链表为空!"); 40 else 41 printf("链表不空!");*/ 42 43 return 0; 44 } 45 46 PNODE creat_list(void) 47 { 48 int len; 49 int i; 50 int val;//临时存放用户输入结点的值 51 PNODE pHead = (PNODE)malloc(sizeof(NODE));//pHead是指向头节点的指针,指向的是头节点 52 if(NULL==pHead) 53 { 54 printf("分配内存失败! "); 55 exit(-1); 56 } 57 PNODE pTail = pHead; 58 pTail ->pNext = NULL; 59 60 printf("请输入您需要生成的链表结点的个数:LEN="); 61 scanf("%d",&len); 62 for(i = 0;i<len;i++) 63 { 64 printf("请输入第%d个结点的值:",i+1); 65 scanf("%d",&val); 66 PNODE pNew = (PNODE)malloc(sizeof(NODE)); 67 if(NULL==pNew) 68 { 69 printf("分配内存失败! "); 70 exit(-1); 71 } 72 pNew->data = val; 73 pTail->pNext = pNew; 74 pNew->pNext = NULL; 75 pTail = pNew;//pTail往后挪一位 76 77 } 78 return pHead; 79 } 80 81 void traverse_list(PNODE pHead) 82 { 83 PNODE p = pHead->pNext; 84 while(NULL!=p) 85 { 86 printf("%d ",p->data); 87 p = p->pNext; 88 } 89 printf(" "); 90 return; 91 } 92 bool is_empty(PNODE pHead) 93 { 94 if(NULL==pHead->pNext) 95 return true; 96 else return false; 97 } 98 int length_list(PNODE pHead) 99 { 100 int len = 0; 101 PNODE p = pHead->pNext; 102 while(NULL!=p) 103 { 104 len++; 105 p = p->pNext; 106 } 107 return len; 108 } 109 110 void sort_list(PNODE pHead) 111 { 112 int i,j,t; 113 int len = length_list(pHead); 114 PNODE p,q; 115 116 for(i=0, p = pHead->pNext;i<len-1; ++i,p=p->pNext) 117 { 118 for(j= i+1,q = p->pNext; j<len;++j, q = q->pNext) 119 { 120 if(p->data>q->data) 121 { 122 t = p->data; 123 p->data = q->data; 124 q->data = t; 125 } 126 } 127 } 128 return; 129 } 130 131 //在pos前的位置上插入 132 bool insert_list(PNODE pHead ,int pos,int val) 133 { 134 PNODE p = pHead; 135 int i = 0; 136 //循环的结果是 p到了pos的前一个指针位置 137 for(i = 0; i<pos-1 ;i++) 138 { 139 p = p->pNext; 140 } 141 if(pos<0 ||p == NULL) 142 { 143 return false; 144 } 145 PNODE pNew = (PNODE)malloc(sizeof(NODE)); 146 if(pNew ==NULL) 147 { 148 printf("申请内存失败! "); 149 exit(-1); 150 } 151 pNew->data = val; 152 pNew->pNext = p->pNext; 153 p->pNext = pNew; 154 return true; 155 } 156 157 bool delete_list(PNODE pHead,int pos,int *val) 158 { 159 PNODE p = pHead; 160 int i = 0; 161 162 for(i = 0; i<pos-1 ;i++) 163 { 164 if(P==NULL) 165 { 166 break; 167 } 168 p = p->pNext; 169 } 170 if(pos<0||p->pNext ==NULL) 171 { 172 *val = 0; 173 return false; 174 } 175 PNODE q = p->pNext; 176 p->pNext = p->pNext->pNext; 177 *val = q->data; 178 free(q); 179 q = NULL; 180 return true; 181 }