2-1單鏈表的基本操作

/***********************
Date:2017-2-24
Author:Sedate
Description:單鏈表的基本操作
***********************/

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<cstdio>
using namespace std;

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

typedef int ElementType;//數據元素的類型爲整型
typedef int Status;     //表示函數返回的狀態值

typedef struct Node
{
    ElementType data;
    Node* next;
}node,*listNode;

typedef struct LinkList
{
    listNode head;          //頭結點
    int size;           //鏈表長度
}*LinkList,Head;

/*
* Summary: 頭插法建立單鏈表,鏈表存儲1-100之間的隨機數
* Parameters:
    list:   the linklist
    n:      the length of linklist
* Return: the status
*/
Status createListHead(LinkList &list,int n)
{
    listNode p;
    if (!(list = (LinkList)malloc(sizeof(Head))))
    {
        return ERROR;
    }
    list->size = 0;
    list->head->data = INT_MIN;
    list->head->next = NULL;
    srand(time(0));
    for (int i = 0; i < n; i++)
    {
        if (!(p = (listNode)malloc(sizeof(node))))
        {
            return ERROR;
        }
        p->data = rand() % 100 + 1;
        p->next = list->head->next;
        list->head->next = p;
        ++list->size;
    }
    return OK;
}

/*
* Summary: 尾插法建立單鏈表,鏈表存儲隨機數
* Parameters:
    list:   the linklist
    n:      the length of linklist
* Return: the status
*/

Status createListTail(LinkList &list, int n)
{
    listNode p;
    listNode q;
    if (!(list = (LinkList)malloc(sizeof(Head))))
    {
        return ERROR;
    }
    list->size = 0;
    q = list->head;
    q->data = INT_MIN;
    q->next = NULL;
    srand(time(0));
    for (int i = 0; i < n; i++)
    {
        if (!(p = (listNode)malloc(sizeof(node))))
        {
            return ERROR;
        }
        p->data = rand() % 100 + 1;
        p->next = NULL;
        q->next = p;
        q = p;
        ++list->size;
    }
    return OK;
}

/*
* Summary: 按位置查找元素,從1開始
* Parameters:
    list:   the linklist
    pos:    the position where the elem to be found
    e:      the elem found
* Return: the status
*/
Status find_by_pos(LinkList &list, int pos,ElementType *e)
{
    if (pos > list->size)
    {
        e = NULL;
        return ERROR;
    }
    listNode p = list->head;
    int i = 0;
    while (i<pos)   
    {
        p = p->next;
        ++i;
    }
    *e = p->data;
    return OK;
}

/*
* Summary: 按值查找元素,從1開始
* Parameters:
    list:   the linklist
    pos:    the postion found
    e:      the elem to get the position
* Return: the status
*/
Status find_by_value(LinkList &list, int pos, ElementType *e)
{
    listNode p = list->head;
    int i = 0;
    while (p!=NULL)
    {
        if (p->data != *e)
        {
            p = p->next;
            ++i;
        }
        else
        {
            pos = i;
            return OK;
        }
    }
    return ERROR;
}

/*
* Summary: 按位置插入元素,pos從1開始計數
* Parameters:
    list:   the linklist
    pos:    the position where the elem to be found
    e:      the elem
* Return: the status
*/
Status insert(LinkList &list, int pos, ElementType *e)
{
    listNode p;
    listNode q;
    int i = 0;
    p = list->head;
    if (pos > list->size)
        return ERROR;
    //找到pos的前一個元素
    while (i < pos - 1)
    {
        p = p->next;
        ++i;
    }
    if (!(q = (listNode)malloc(sizeof(node))))
    {
        return ERROR;
    }
    q->data = *e;
    q->next = p->next;
    p->next = q;
    ++list->size;
    return OK;
}

/*
* Summary: 在頭部插入指定元素
* Parameters:
    list:   the linklist
    e:      the elem
* Return: the status
*/
Status insertFirst(LinkList &list,ElementType *e)
{
    listNode p;
    if (!(p = (listNode)malloc(sizeof(node))))
    {
        return ERROR;
    }
    p->data = *e;
    p->next = list->head->next;
    list->head->next = p;
    ++list->size;
    return OK;
}

/*
* Summary: 在尾部插入指定元素
* Parameters:
    list:   the linklist
    e:      the elem
* Return: the status
*/
Status insertFirst(LinkList &list, ElementType *e)
{
    listNode p,q;
    if (!(p = (listNode)malloc(sizeof(node))))
    {
        return ERROR;
    }
    p->data = *e;
    p->next = NULL;
    q = list->head;
    while (q->next != NULL)
    {
        q = q->next;
    }
    q->next = p;
    ++list->size;
    return OK;
}

/*
* Summary: 刪除指定位置的元素
* Parameters:
    list:   the linklist
    pos:    the position where an elem to be deleted
* Return: the status
*/
Status delete_by_pos(LinkList &list, int pos)
{
    if (pos > list->size)
        return ERROR;
    listNode p,q;
    int i = 0;
    p = list->head;
    while (i < pos-1)
    {
        p = p->next;
        ++i;
    }
    q = p->next;
    p->next = p->next->next;
    free(q);
    --list->size;
    return OK;
}

/*
* Summary: 刪除頭部元素
* Parameters:
    list:   the linklist    
* Return: the status
*/
Status deleteFirst(LinkList &list)
{
    listNode p;
    if (list->head->next == NULL)
        return ERROR;
    p = list->head->next;
    list->head = p->next;
    free(p);
    --list->size;
    return OK;
}

/*
* Summary: 刪除尾部元素
* Parameters:
    list:   the linklist    
* Return: the status
*/
Status insertFirst(LinkList &list)
Status insertFirst(LinkList &list)
{
    if (list->head->next == NULL)
        return ERROR;
    listNode p, q;
    p = list->head;
    while (p->next->next != NULL)
    {
        p = p->next;
    }
    free(p->next);
    p->next = NULL;
    --list->size;
    return OK;
}
發佈了44 篇原創文章 · 獲贊 7 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章