欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

直接插入排序法

程序员文章站 2022-03-15 21:57:30
...
#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;
}