C語言 單向鏈表的創建、遍歷、排序、刪除操作

參考網上的兩個鏈表案例,整理了一份針對單鏈表進行創建、遍歷、排序、刪除操作的代碼如下,以備後用。

/** 
 *	鏈表創建、遍歷、排序、刪除操作
 *  
 */
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

//定義鏈表中的節點
typedef struct node
{
    int data;                    //節點中的成員
    struct node *pNext;          //指向下一個節點的指針
}Node,*pNode;

/** 
 * 創建鏈表
 */
pNode CreateList()
{
    int i, len, val;
    pNode pHead = (pNode)malloc(sizeof(Node));    //創建頭結點並分配內存
    pNode pTail = pHead;                           
    pTail->pNext = NULL; 
                              
    printf("請輸入節點個數:");
    scanf("%d", &len);
    for(i = 0; i < len; i++)
    {
        printf("第 %d 個節點的數值:",i+1);
        scanf("%d",&val);
        pNode pNew = (pNode)malloc(sizeof(Node));    //創建節點
        pNew->data = val;                             //將用戶輸入的數據賦給節點的成員
        pTail->pNext = pNew;                         //將最後一個節點的指針指向下一個新的節點
        pNew->pNext = NULL;                          //將新節點中的指針置爲空
        pTail = pNew;                                //將新節點賦給最後的一個節點
    }
    return pHead;                                    //返回頭節點
}

//    遍歷鏈表函數
void printLinkedList(pNode pHead)
{
	  if(pHead==NULL)
		{
			printf("鏈表爲空\n");
			return;
		}
    pNode p = pHead->pNext;                            //將頭節點的指針給予臨時節點p
    printf("鏈表數據爲: ");
    while(NULL != p)                                   //節點p不爲空,循環    
    {
        printf("%d ",p->data);                    
        p = p->pNext;                                
    }
    printf("\n");
    return ;
}

//排序
pNode Sort (pNode pHead)
{
	pNode endpt;	  
	pNode p;		    
	pNode p1,p2;

	p1 = (pNode) malloc (sizeof(Node));
	p1->pNext = pHead;		   
	pHead = p1;			         

	for (endpt = NULL; endpt != pHead; endpt = p)	
	{
		for (p = p1 = pHead; p1->pNext->pNext != endpt; p1 = p1->pNext)
		{
			if (p1->pNext->data > p1->pNext->pNext->data)	
			{
				p2 = p1->pNext->pNext;	  
				p1->pNext->pNext = p2->pNext;	  
				p2->pNext = p1->pNext;	 
				p1->pNext = p2;	  
				p = p1->pNext->pNext;	
			}
		}
	}

	p1 = pHead;			    
	pHead = pHead->pNext;		
	free (p1);			
	p1 = NULL;			

	return pHead;
}

// 刪除鏈表指定數據
pNode Del(pNode pHead,int num){
		pNode p1;
		pNode p2;
		if( NULL == pHead)
		{
			printf("\n List is null!\n");
			return pHead	;
		}
		p1 = pHead;
		while( p1->data != num && p1 ->pNext != NULL)
		{
			p2 = p1;
			p1 = p1 ->pNext ;		
		}
		
		if(p1->data == num)
		{
			if (p1 == pHead)//如果要刪除的節點是第一個節點
			{
				pHead = p1->pNext;	 
			}
			else//如果是其它節點,則讓原來指向當前節點的指針,指向它的下一個節點,完成刪除
			{
				p2->pNext = p1->pNext;
			}
			
			free (p1);		          
			p1 = NULL;
			printf ("\ndelete %ld success!\n", num);				
		}
		else
		{
				printf ("\n%ld not been found in List!\n", num);
		}
		return pHead;	
} 

//銷燬鏈表
pNode DestroyList(pNode pHead)
{
	pNode p;
	if(pHead==NULL)
		return;
	while(pHead)
	{
		p = pHead->pNext;
		free(pHead);
		pHead=p;
	}	
	return pHead;
}

int main()
{
    pNode pHead = NULL;            //    定義初始化頭節點
    pHead = CreateList();          //    創建鏈表 
    printLinkedList(pHead);        //    調用遍歷鏈表函數
    //排序
    Sort (pHead);
    printLinkedList(pHead);
    //刪除指定數值的節點
    Del(pHead,5);
    printLinkedList(pHead);
    //銷燬鏈表
    DestroyList(pHead);
    printLinkedList(pHead);
               
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章