數據結構之線性表(單向鏈表)

單向鏈表c語言實現

link_list.h

#ifndef _LIST_H_
#define _LIST_H_
struct Node     //結點 
{
    int data;       //數據域 
    struct Node *next;  //指向下一個結點的指針 
};

struct Header   //頭結點 
{
    int length;     //記錄鏈表大小 
    struct Node *next;
}; 

typedef struct Node List;
typedef struct Header pHead;

pHead *createList();        //創建鏈表
int isEmpty(pHead *l);      //判斷鏈表是否爲空
int Insert(pHead *l,int pos,int val);       //插入元素,插入成功返回1
List *Delete(pHead *l, int ele);            //刪除元素,刪除成功則返回刪除的元素
List *find(pHead *l,int ele);               //查找某個元素是否存在
int Size(pHead *l);         //獲取鏈表大小
void Destory(pHead *l);     //銷燬鏈表
void print(pHead *l);       //打印鏈表 

#endif

link_list.c

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

//創建鏈表 
pHead *createList()             //pHead是struct Header的別名,是頭結點類型 
{
    pHead *ph=(pHead*)malloc(sizeof(pHead));    //爲頭節點分配內存 
    ph->length=0;                               //爲頭結點初始化 
    ph->next=NULL;
    return ph;                  //返回頭結點地址 
}
//獲取鏈表大小
int Size(pHead *ph)
{
    if(ph==NULL)
    {
        printf("ph==NULL\n");
        return 0;
    }
    return ph->length;  
} 
int Insert(pHead *ph,int pos, int val)  //在某個位置插入某個元素,插入成功返回1
{
    //先做健壯性判斷
    if(ph==NULL || pos<0 || pos >ph->length)
    {
        printf("error\n");
        return 0;   
    } 
    List* pval=(List *)malloc(sizeof(List));
    pval->data=val;
    List *pCur=ph->next;
    if(pos==0)
    {
        ph->next=pval;
        pval->next=pCur;
    }
    else
    {
        int i;
        for(i=1;i<pos;i++)
        {
            pCur=pCur->next;
        }
        pval->next=pCur->next;
        pCur->next=pval;
    }
    ph->length++;
    return 1;
} 
//查找某個元素 
List *find(pHead *ph,int val)
{
    //先做健壯性判斷
    if(ph==NULL)
    {
        printf("error\n");
        return NULL;    
    } 
    //遍歷鏈表來查找元素
    List *pTmp=ph->next;
    do
    {
        if(pTmp->data==val) 
        {
            return pTmp;
        }
        pTmp=pTmp->next;
    }while(pTmp->next!=NULL);
    printf("no find %d data\n",val);
    return NULL; 
} 
//刪除元素
List *Delete(pHead *ph,int val)
{
    //先做健壯性判斷
    if(ph==NULL)
    {
        printf("ph==NULL\n");
        return NULL;    
    }   
    List *pval=find(ph,val);
    if(pval==NULL)
    {
        printf("not find %d\n",val);
    }

    //遍歷鏈表找到要刪除的結點,並找出其前驅及後繼結點
    List *pRe=ph->next;     //當前結點
    List *pCur=NULL;
    if(pRe->data==val)      //第一個結點 
    {
        ph->next=pRe->next;
        ph->length--;
        return pRe; 
    } 
    else            //其它結點 
    {
        int i;
        for(i=0;i<ph->length;i++)
        {
            pCur=pRe->next;
            if(pCur->data==val)
            {
                pRe->next=pCur->next;
                ph->length--;
                return pCur;    
            } 
            pRe=pRe->next;
        } 
    }
} 
//銷燬鏈表
void Destory(pHead *ph)
{
    List *pCur=ph->next;
    List *pTmp;
    if(ph==NULL)
    {
        printf("error\n");
    }
    while(pCur->next != NULL)
    {
        pTmp=pCur->next;
        free(pCur);
        pCur=pTmp;  
    }   
    ph->length=0;
    ph->next=NULL;
} 
//遍歷打印鏈表
void print(pHead *ph)
{
    if(ph==NULL)
    {
        printf("ph==NULL\n");   
    }   
    List *pTmp=ph->next;
    while(pTmp!=NULL)
    {
        printf("%d  ",pTmp->data);
        pTmp=pTmp->next;
    }
    printf("\n");
}  

main.c

#define _CRT_SECURE_NO_WARNINGA
#include "link_list.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    int ret;
    pHead *ph=createList();
    if(ph==NULL)
    {
        printf("error\n");
    }
    int arr[10]={1,2,3,4,5,6,7,8,9,10};
    int i;
    for(i=0;i<10;i++)
    {
        Insert(ph, 0, arr[i]);
    }
    printf("link length %d\n",Size(ph));

    printf("print link_list ele:\n");
    print(ph);
    printf("please input delete link ele:\n");
    int num;
    scanf("%d",&num);
    Delete(ph,num);
    printf("delete over,print link\n");
    print(ph);

    ret=find(ph,3);
    if(ret)
    {
        printf("get!\n");
    }
    else
    {
        printf("no!\n");
    }
    system("pause");
    return 0;
}

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