直接插入排序法

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node{
	
	int date;
	struct node* pnext;
}strNode;

strNode *pHead = NULL;


//创建链表=====头插法
void CreateList(int date)
{
	strNode *pNode,*Head;
	Head = pHead;
	pNode = (strNode*)malloc(sizeof(strNode));
	if(NULL == pNode)
	{
		printf("not enough memory!!");
		return;
	}
	memset(pNode,0,sizeof(strNode));
	
	pNode->date = date;
	printf("Insert Data into linklist, date = %d.\n", date);
	if(Head == NULL)
	{
		Head = pNode;
		pNode->pnext = NULL;
	}
	else 
	{
		pNode->pnext = Head->pnext;
		Head->pnext = pNode;
	}	
	pHead = Head;
}
//打印输出链表

void printLinklist()
{
	printf("========================List===========================\n");
	strNode* plocallhead = pHead;
	while(plocallhead != NULL)
	{
		printf("输出链表的值:date = %d\n",plocallhead->date);
		plocallhead = plocallhead->pnext;
	}
}

//释放由malloc定义的空间
void destoryLinklist()
{
	strNode* plocallhead = pHead;
	
	while(plocallhead != NULL)
	{   printf("释放顺序:date = %d\n",plocallhead->date);
        pHead = pHead->pnext;
		free(plocallhead);
		plocallhead = pHead;
	}
}
//增
void addnodelist()
{
	
}
 
//删除节点
int  deleteNode(int k)
{
	strNode* plocallhead = pHead;
	strNode* plasthead = pHead;
	
	while(plocallhead != NULL)
	{   
       if(plocallhead->date == k)
	   {
		   if(plasthead == plocallhead)
		   {
			   plocallhead = plocallhead->pnext;
			   free(pHead);
			   pHead = plocallhead;
			   return 0;
		   }
		   else
		   {
			  plasthead->pnext = plocallhead->pnext;
              free(plocallhead);	
              return 0;			  
		   }
	   }
		plasthead = plocallhead;
		plocallhead = plocallhead->pnext;
        return -1;		
	}

	
}
//修改节点值
int modifyNode(int k,int t)
{
	strNode* plocallhead = pHead;
	
	if(pHead == NULL) return -1;
	
	while(plocallhead != NULL)
	{   
		//printf("[modifyNode] k = %d\n", k);
		//printf("[modifyNode] get value : plocallhead->date = %d\n", plocallhead->date);
		if(plocallhead->date == k) 
		{
			plocallhead->date = t;
			return 0;
		}
		plocallhead = plocallhead->pnext;
	}
	return -1;
}
//查询节点
strNode* searchNode(int k)
{
	strNode* plocallhead = pHead;
	if(pHead == NULL) return NULL;
	
	while(plocallhead != NULL)
	{   
		if(plocallhead->date == k) return plocallhead;
		plocallhead = plocallhead->pnext;
	}
	return NULL;
}
//查询节点数量
int NodeNumber()
{
	strNode* plocallhead = pHead;
	int count = 0;
	if(pHead == NULL) return 0;
	
	while(plocallhead != NULL)
	{   
		count+=1;
		plocallhead = plocallhead->pnext;
	}
	return count;
	
}
//链表排序
void OrderList()
{
	strNode* plocallhead = pHead->pnext;
	strNode *p,*plasthead,*plasthead1;
	
	//if(pHead == NULL) return;
	for(;plocallhead->pnext;plasthead = plocallhead,plocallhead = plocallhead->pnext)
	{   
		for(p = pHead; p->pnext!=plasthead->pnext; plasthead1 = p,p = p->pnext)
		{
			if(p->date>plocallhead->date)
			{
				if(p == pHead)
				{
					plasthead->pnext = plocallhead->pnext;
					plocallhead->pnext = pHead;
					pHead->pnext = plocallhead; 
				}
				else
				{
					plasthead->pnext = plocallhead->pnext;
					plocallhead->pnext = p;
					p->pnext = plocallhead; 
				}
			}
			else break;
		}
          
	}
	
}
int main(void)
{    int a[10] = {0,1,2,3,4,5,6,7,8,9};
     int i,j,p;
	 int count;
	 strNode *ptemp;
   for(i=0;i<10;i++)
   {
	   CreateList(a[i]);
   }
   printLinklist();
   
   //增加节点值
    CreateList(12);
	printf("======================增加=========================\n");
    printLinklist();
  
  //删除节点
    p = deleteNode(0);
	printf("===========================删除=============================\n");
	 if(p == 0) 
		 printf("=======================删除成功==================\n");
     if(p == -1)
		 printf("=======================删除失败=====================\n");
    printLinklist();
  
    //修改节点值
     j = modifyNode(7,15);
     if(j == 0) 
		 printf("=======================修改成功==================\n");
     if(j == -1)
		 printf("=======================修改失败=====================\n");
	  printf("=========================修改============================\n");
      printLinklist();
	
	//查询节点
	 printf("========================查询============================\n");
	  ptemp = searchNode(9);
	  printf("查询值得地址:0x%x, 值为:%d\n",(int)&ptemp->date,ptemp->date);
	  printLinklist();
   
   //查询节点数量
    count =  NodeNumber();
    printf("查询到的节点数量:%d\n",count);
	printf("***************************************************************************");
	//链表排序
	OrderList();
	printLinklist();
	destoryLinklist();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值