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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值