單鏈表實現

單鏈表實現

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdbool.h>

typedef struct Node
{
    int data;  //數據域
    struct Node * pNext;  //指針域 
}NODE, *PNODE;   //NODE等價於struct Node,PNODE等價於struct Node *  

//函數聲明 
PNODE creat_list();
void traverse_list(PNODE pHead);
bool is_empty(PNODE pHead);
int length_list(PNODE pHead);
bool insert_list(PNODE pHead,int pos,int val); 
bool delete_list(PNODE pHead,int,int*); 
void sort_list(PNODE pHead);

int main()
{
    PNODE pHead = NULL;  //等價於struct Node * pHead = NULL;頭結點 
    int val;
    
    pHead = creat_list();
    traverse_list(pHead);
    if( is_empty(pHead) )
        printf("鏈表爲空\n");
    else
        printf("鏈表不空\n"); 
    int len = length_list(pHead);
    printf("鏈表長度是%d\n",len);
    
    if( insert_list(pHead,4,33) )
    {
        printf("第四個節點插入數據成功!\n"); 
    } 
    else
    {
        printf("插入失敗!\n");
    }
    traverse_list(pHead);
    
    if( delete_list(pHead,4,&val) )
    {
        printf("刪除成功,您刪除的元素是:%d\n",val); 
    } 
    else
    {
        printf("刪除失敗!您刪除的元素不存在!\n");
    }
    traverse_list(pHead);
    
    sort_list(pHead);
    printf("鏈表由小到大排序\n");
    traverse_list(pHead);
    
    return 0;
} 

PNODE creat_list()
{
    int len;   //用來存放有效節點的個數 
    int val;   //用來臨時存放用戶輸入節點的值
    
    PNODE pHead = (PNODE)malloc(sizeof(NODE));
    if(NULL == pHead)
    {
        printf("分配失敗,程序終止"); 
        exit(-1);
    } 
    PNODE pTail = pHead;  
    pTail->pNext = NULL;
    
    printf("請輸入您須要生成的鏈表節點的個數:len = ");
    scanf("%d",&len);
    
    for(int i=0;i<len;++i)
    {
        printf("請輸入第%d個節點的值:",i+1);
        scanf("%d",&val);
        
        PNODE pNew = (PNODE)malloc(sizeof(NODE));
        if(NULL == pNew)
        {
            printf("分配失敗,程序終止"); 
            exit(-1);
        } 
        pNew->data = val;   //尾插法 
        pTail->pNext = pNew;
        pNew->pNext = NULL;
        pTail = pNew;  //pTail在不斷的移動,至關於指向要插入節點上一個節點指針 
    }
    return pHead; 
}

void traverse_list(PNODE pHead)
{
    PNODE p = pHead->pNext;
    while(NULL != p)
    {
        printf("%d ",p->data);
        p = p->pNext;
    }
    printf("\n");
    return;
} 

bool is_empty(PNODE pHead)
{
    if(NULL == pHead->pNext)
        return true;
    else
        return false;
}

int length_list(PNODE pHead)
{
    PNODE p = pHead->pNext;
    int len=0; 
    while(NULL != p)
    {
        ++len;
        p = p->pNext;
    }
    return len;
}

void sort_list(PNODE pHead)   //選擇排序算法 
{
    PNODE p,q; 
    int i,j,t;
    int len = length_list(pHead);
    
    for(i=0,p=pHead->pNext;i<len-1;++i,p=p->pNext)
    {
        for(j=0,q=p->pNext;j<len;++j,q=q->pNext)
        {
            if(p->data > q->data)   //相似於數組中的:a[i]>a[j] 
            {
                t = p->data;//相似於數組中的:t = a[i];
                p->data = q->data;//相似於數組中的:a[i] = a[j];
                q->data = t;//相似於數組中的:a[j] = t;
            } 
        }
    }
    return;
}

bool insert_list(PNODE pHead,int pos,int val)
{
    int i=0;
    PNODE p = pHead;
    
    while(NULL!=p && i<pos-1)
    {
        p = p->pNext;
        ++i;
    }
    if(i>pos-1 || NULL==p)
        return false;
        
    PNODE pNew =(PNODE)malloc(sizeof(NODE));
    if(NULL == pNew)
    {
        printf("動態分配內存是啊比!\n");
        exit(-1);
    }
    pNew->data = val;
    PNODE q = p->pNext;
    p->pNext = pNew;
    pNew->pNext = q;
    return true;
}

bool delete_list(PNODE pHead,int pos,int* pVal)
{
    int i=0;
    PNODE p = pHead;
    
    while(NULL!=p->pNext && i<pos-1)
    {
        p = p->pNext;
        ++i;
    }
    if(i>pos-1 || NULL==p->pNext)
        return false;
        
    PNODE q = p->pNext;   //將要刪除的節點賦值給臨時指針q 
    *pVal = q->data;
    
    //刪除p節點後面的節點
    p->pNext = p->pNext->pNext;
    free(q);
    q = NULL; 
    return true;    
}
相關文章
相關標籤/搜索