線性表 | 單鏈表

  • 頭文件
#ifndef _SINGLE_LINK_LIST_H_
#define _SINGLE_LINK_LIST_H_

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0




typedef int ElemType;
typedef int status;


//定義鏈表節點
typedef struct Node
{
    ElemType data;//數據域
    struct Node *next;//指針域
}Node;

typedef struct Node* LinkList;

//函數聲明
status create_list(LinkList *head);
status free_list(LinkList head);
status tail_insert(LinkList head,int i,ElemType *arr);
status head_insert(LinkList head,int i,ElemType *arr);
status print_list(LinkList head);
status get_element(LinkList head,int i,ElemType *e);
status insert_element(LinkList *head,int i,ElemType e);
status delete_element(LinkList *head,int i,ElemType *e);
#endif
  • 主程序
#include <stdio.h>
#include <stdlib.h>
#include "single_link_list.h"



int main()
{
    LinkList head = NULL;
    int      rv = -1;
    int      i;
    int      e = 0;
    ElemType arr[5];

    rv = create_list(&head);
    //初始化數組
    for(i = 0;i < 5;i++)
    {
        arr[i] = i+2;
    }

    tail_insert(head,5,arr);
    printf("print list:\n");
    print_list(head);

    get_element(head,5,&e);
    printf("Get element is:%d\n",e);

    insert_element(&head,1,10);
    printf("print list after inserting:\n");
    print_list(head);

    delete_element(&head,2,&e);
    printf("Delete element is %d\n",e);
    printf("print list after deleting:\n");
    print_list(head);

    free_list(head);

    //頭插法
    create_list(&head);
    head_insert(head,5,arr);
    printf("print list after head deleting:\n");
    print_list(head);
    free_list(head);
    return 0;

}


/*func:Create a list.
  para:
        head:single list head ptr
        e:data
  return:
        success:OK
        fail:ERROR
*/
status create_list(LinkList *head)
{
    LinkList head_node = malloc(sizeof(Node));
    if(!head_node)
    {
        printf("Malloc head node failed!\n");
        return ERROR;
    }
    head_node->data = 0;
    head_node->next = NULL;

    *head = head_node;

    return OK;
}

/*func:Add new node by using the tail interpolation.
  para:
        head:single list head ptr.
        i:Need to add the number of nodes.
        arr:Data stored in a linked list.
  return:
        success:OK
        fail:ERROR
*/
status tail_insert(LinkList head,int i,ElemType *arr)
{
    LinkList newnode;
    LinkList ptr;//動態指針
    if(!head)
    {
        printf("Link list is not exist!\n");
        return ERROR;
    }

    ptr = head;//指向頭節點

    //遍歷鏈表到尾節點
    while(ptr->next != NULL)
    {
        ptr = ptr->next;
    }

    //從尾部添加節點
    while(i--)
    {
        newnode = malloc(sizeof(Node));
        if(!newnode)
        {
            printf("Malloc new node failed!\n");
        }
        newnode->data = *arr;
        newnode->next = NULL;
        ptr->next = newnode;
        ptr = newnode;
        arr++;
    }

    return OK;
}


/*func:Add new node by using the head interpolation.
  para:
        head:single list head ptr.
        i:Need to add the number of nodes.
        arr:Data stored in a linked list.
  return:
        success:OK
        fail:ERROR
*/
status head_insert(LinkList head,int i,ElemType *arr)
{
    LinkList newnode;

    if(!head)
    {
        printf("Link list is not exist!\n");
        return ERROR;
    }

    while(i--)
    {
        newnode = malloc(sizeof(Node));
        newnode->data = *arr;
        newnode->next = head->next;
        head->next = newnode;
        arr++;
    }

    return OK;

}


/*func:free link list.
  para:
        head:single list head ptr.
  return:
        success:OK
        fail:ERROR
*/

status free_list(LinkList head)
{
    LinkList ptr;
    LinkList temp;
    
    if(!head)
    {
        printf("Link list is not exist!\n");
        return ERROR;
    }

    ptr = head;

    while(ptr != NULL)
    {
        temp = ptr;
        ptr = ptr->next;
        free(temp);
    }

    printf("Free Link List successfully!\n");

    return OK;
}

/*func:Add new node by using the tail interpolation.
  para:
        head:single list head ptr.
  return:
        success:OK
        fail:ERROR
*/
status print_list(LinkList head)
{
    LinkList ptr = NULL;
    ElemType e;

    if(!head)
    {
        return ERROR;
    }
    ptr = head->next;//指向第一個節點

    while(ptr != NULL)
    {
        e = ptr->data;
        printf("%d ",e);
        ptr = ptr->next;
    }
    printf("\n");

    return OK;
}



/*func:Get an element into a single list before i position.
  para:
        head:single list head ptr
        i:Get the i element.
        e:Use e return element.
  return:
        success:OK
        fail:ERROR
*/
status get_element(LinkList head,int i,ElemType *e)
{
    LinkList p;
    int count = 1;//計數器

    if(!head)
    {
        return ERROR;
    }
    p = head->next;//略過頭節點,指向第一個節點

    while(p && count < i)
    {
        p = p->next;
        count++;
    }

    if(!p || count > i)//判斷是否是最後一個空節點
    {
        printf("Beyond the scope of the linked list!\n");
        return ERROR;
    }
    
    *e = p->data;

    return OK;
}

/*func:Inserts an element into a link list before i position.
  para:
        head:single list head ptr
        i:Insertion position.
        e:The element to be inserted.
  return:
        success:OK
        fail:ERROR
*/
status insert_element(LinkList *head,int i,ElemType e)
{
    LinkList newnode;//新結點
    LinkList ptr;//當前節點
    int      count = 1;//計數器

    if(!*head)
    {
        printf("List is not exsited!\n");
        return ERROR;
    }
    ptr = *head;//指向頭結點

    //指針偏移
    while(ptr && count < i)
    {
        ptr = ptr->next;
        count++;
    }
    if(!ptr || count > i)//判斷是否是最後一個節點
    {
        printf("The number of List Node less than i.\n");
        return ERROR;
    }

    newnode = malloc(sizeof(Node));
    if(!newnode)
    {
        printf("Malloc new node failed!\n");
        return ERROR;
    }
    newnode->data = e;
    newnode->next = ptr->next;
    ptr->next = newnode;

    return OK;
}

/*func:Delete an element from a link list on i position.
  para:
        head:single list head ptr
        i:Delete position.
        e:Returns the deleted element.
  return:
        success:OK
        fail:ERROR
*/
status delete_element(LinkList *head,int i,ElemType *e)
{
    LinkList ptr;//偏移指針
    LinkList temp;//中間變量
    int      count = 1;//節點計數器


    if(!*head)
    {
       printf("List is not exsited!\n");
       return ERROR;
    }

    ptr = *head;//指向第一個

    //遍歷鏈表
    while(ptr && count < i)
    {
        ptr = ptr->next;
        count++;
    }

    if(!ptr || count > i)
    {
        printf("Beyond the scope of the linked list!\n");
        return ERROR;
    }

    temp = ptr->next;
    *e = temp->data;

    ptr->next = temp->next;
    free(temp);

    return OK;
}

  • 測試
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章