单链表的一些简单操作,请多多指正
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char key[10];
char name[20];
int age;
}Data;
typedef struct Node
{
Data nodeData;
struct Node *nextNode;
}CLType,*pCLType;
extern CLType *CL_add_end(CLType *head,Data nodeData);
extern CLType *CL_add_first(CLType *head,Data nodeData);
extern CLType *CL_insert_node(CLType *head,char *findkey,Data nodeData);
extern CLType *CL_del_head(CLType *head);
extern void CL_del_head_v2(CLType **head);
extern void CL_del_tail(CLType **head);
extern int CL_del_node(CLType **head,char *delkey);
extern void CL_clear_all(CLType **head);
extern CLType *CL_find_node(CLType *head,char *findkey);
extern int CL_get_length(CLType *head);
extern void CL_show_node(CLType *node);
extern void CL_show_all(CLType *head);
extern CLType *CL_sort_insert(CLType *head);
/**************************************************************************************/
/*尾巴加入*/
CLType *CL_add_end(CLType *head,Data nodeData)
{
CLType *node = NULL;
CLType *temp = NULL;
node = (CLType*)malloc(sizeof(CLType));
if(NULL == node){
printf("malloc node error!!");
return NULL;
}
node->nodeData = nodeData;
node->nextNode = NULL;
if(NULL == head){
head = node;
return head;
}
temp = head;
while(NULL != temp->nextNode){
temp = temp->nextNode;
}
temp->nextNode = node;
return head;
}
/*从头加入*/
CLType *CL_add_first(CLType *head,Data nodeData)
{
CLType *node = NULL;
node = (CLType*)malloc(sizeof(CLType));
if(NULL == node){
printf("malloc node error!!");
return NULL;
}
node->nodeData = nodeData;
node->nextNode = head;
head = node;
return head;
}
/*插入节点*/
CLType * CL_insert_node(CLType *head,char *findkey,Data nodeData)
{
CLType *node = NULL;
CLType *temp = NULL;
node = (CLType*)malloc(sizeof(CLType));
if(NULL == node){
printf("malloc node error!!");
return NULL;
}
node->nodeData = nodeData;
temp = CL_find_node(head,findkey);
if(NULL != temp){
node->nextNode = temp->nextNode;
temp->nextNode = node;
}else{
printf("not find key node!!");
free(node);
}
return head;
}
/**************************************************************************************/
/*删除头*/
CLType * CL_del_head(CLType *head){
CLType *temp = NULL;
temp = head;
head = temp->nextNode;
free(temp);
return head;
}
/*删除头*/
void CL_del_head_v2(CLType **head){
CLType *temp = NULL;
temp = *head;
*head = temp->nextNode;
free(temp);
}
/*删除尾*/
void CL_del_tail(CLType **head){
CLType *temp = NULL;
CLType *node = NULL;
temp = *head;
node = *head;
if(CL_get_length(*head)==1){
CL_del_head_v2(head);
return ;
}
while(NULL != temp->nextNode){
node = temp;
temp = temp->nextNode;
}
node->nextNode = NULL;
free(temp);
}
/*删除某个节点*/
int CL_del_node(CLType **head,char *delkey){
CLType *temp = NULL;
CLType *node = NULL;
temp = *head;
node = *head;
if(strcmp(temp->nodeData.key,delkey)==0){
CL_del_head_v2(head);
return 1;
}
while(NULL != temp){
if(strcmp(temp->nodeData.key,delkey)==0){
node->nextNode = temp->nextNode;
free(temp);
return 1;
}
node = temp;
temp = temp->nextNode;
}
return 0;
}
/*清理*/
void CL_clear_all(CLType **head)
{
CLType *temp;
while(NULL != *head){
temp = (*head)->nextNode;
free(*head);
(*head) = temp;
}
}
/**************************************************************************************/
/*查找节点*/
CLType *CL_find_node(CLType *head,char *findkey)
{
CLType *temp = head;
while(NULL != temp){
if(strcmp(temp->nodeData.key,findkey)==0){
return temp;
}
temp = temp->nextNode;
}
return NULL;
}
/*长度*/
int CL_get_length(CLType *head){
CLType *node = NULL;
CLType *temp = NULL;
int len = 0;
temp = head;
while(NULL != temp){
len++;
temp = temp->nextNode;
}
return len;
}
/**************************************************************************************/
/*显示*/
void CL_show_node(CLType *node)
{
if(NULL != node){
printf("node key=%s name = %s age= %d\n",node->nodeData.key,
node->nodeData.name,node->nodeData.age);
}
}
/*全部显示*/
void CL_show_all(CLType *head)
{
CLType *temp = NULL;
Data nodeData;
memset(&nodeData,0,sizeof(Data));
temp = head;
while(NULL != temp){
nodeData = temp->nodeData;
printf("nodeData key=%s name = %s age= %d\n",nodeData.key,nodeData.name,nodeData.age);
temp = temp->nextNode;
}
}
/**************************************************************************************/
/*排序*/
CLType *CL_sort_insert(CLType *head)
{
CLType *p = head;
CLType *minNode = head;
CLType *pstart = NULL;
CLType*sortedTail = NULL;
Data temp;
if(head == NULL || head->nextNode == NULL)
return head;
pstart = (CLType*)malloc(sizeof(CLType));
pstart->nextNode = head; //为了操作方便,添加一个头结点
sortedTail = pstart;//指向已排好序的部分的尾部
while(sortedTail->nextNode != NULL){
minNode = sortedTail->nextNode;
p = sortedTail->nextNode->nextNode;
//寻找未排序部分的最小节点
while(p != NULL){
if(p->nodeData.age < minNode->nodeData.age)
minNode = p;
p = p->nextNode;
}
temp = minNode->nodeData;
minNode->nodeData = sortedTail->nextNode->nodeData;
sortedTail->nextNode->nodeData = temp;
sortedTail = sortedTail->nextNode;
}
head = pstart->nextNode;
free(pstart);
return head;
}
/**************************************************************************************/
int main()
{
CLType * head = NULL;
CLType * findnode = NULL;
Data nodeData;
int iage=0;
int total_num ;
printf("输入个数:");
scanf("%d",&total_num);
for(;iage<total_num;iage++)
{
memset(&nodeData,0,sizeof(Data));
scanf("%s %s %d",nodeData.key,nodeData.name,&nodeData.age);
//head = CL_add_first(head,nodeData);
head = CL_add_end(head,nodeData);
}
printf("length = %d\n",CL_get_length(head));
findnode=CL_find_node(head,"2");
CL_show_node(findnode);
CL_show_all(head);
printf("\n\ninsert******************\n");
memset(&nodeData,0,sizeof(Data));
scanf("%s %s %d",nodeData.key,nodeData.name,&nodeData.age);
CL_insert_node(head,"2",nodeData);
CL_show_all(head);
printf("\n\nDelete 2******************\n");
//CL_del_head_v2(&head);
//CL_del_node(&head,"1");
//CL_del_tail(&head);
//CL_show_all(head);
printf("\n\n sort 2******************\n");
head=CL_sort_insert(head);
CL_show_all(head);
printf("\n\nclear******************\n");
CL_clear_all(&head);
getch();
return 1;
}