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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验一 1. 使用记事本和命令行程序编写Java应用程序,打印出所有的水仙花数。 2. 程序设计(开发环境不限): 1) 打印出100以内的素数 2) 求1!+2!+……+20! 3) 课后习题2.6 4) 编写程序,命令行窗口输出希腊字母表。(希腊字母表开始为α,最后一个为ω) 实验二 1、设计一个名为figure的图形软件包(package)。包中包含三角形、矩形、圆三个类。要求:(1)每个类都要构造方法并为成员设置get和set方法;(2)每个类都要有计算周长和面积的成员方法;(3)完成该软件包后的编码后,在另一个包的含有main方法的类中编写代码,分别使用图形软件包中的三个类,生成三个对象,并打印出其周长和面积。 2、编写类Factorial,为其添加两个静态方法(方法名自定义)。其中一个使用递归计算n的阶乘,一个使用非递归计算n的阶乘。构造main方法进行测试。 3、按照要求使用Java进行编码。 1) 设计一个教师类Teacher,属性有编号(no)、姓名(name)、年龄(age)、所属学院(seminary),为这些属性设置相应的get和set方法; 2) 为Teacher类添加方法equals;(当两个教师对象的no相同时返回true) 3) 为Teacher类添加方法toString,通过该方法可以返回“编号为**、姓名为**、年龄为**的**学院老师”形式的字符串。 4) 构造main方法进行测试。 4、设计一个带表头的单链表(链表中的元素属于同一类型对象,但对象的类型可以随意),提供以下操作:(1)insert:在某个位置插入对象;(2)delete:在某个位置删除对象;(3)delete:删除链表中与x相同的元素;(4)size:返回当前链表中对象的个数;(5)isEmpty:判断链表是否为空;(6)traverse:遍历链表,打印出所有的元素;(7)getData:取得某个位置的对象。构造main函数进行测试。 实验三 1、按照要求使用Java进行编码。 1) 编写一个抽象类Shape,其中有抽象方法getArea()和getPerimeter() 2) 在Shape类的基础上派生出Rectangle和Circle类,二者都实现了计算面积的方法getArea()和计算周长的方法getPerimeter(); 3) 构造main函数,生成Rectangle和Circle对象,并用Shape类型的变量调用Rectangle和Circle对象的getArea()和getPerim()方法。 2、以电话为父类,移动电话和固定电话为两个子类,并使移动电话实现接口:可移动。固定电话又有子类:无绳电话。定义接口及各类,明确他们的继承关系。 3、在实验2中所实现的Teacher类的基础上,修改Teacher类的代码,要求:由多个Teacher对象所形成的数组可以使用Arrays.sort方法进行排序(编号由低到高排序)。 实验四 1、在main方法中创建一个含有10个元素的int型数组,进行以下操作:(1)将数组元素按照从小到大的顺序排列;(2)对排好序的数组使用折半查找(使用递归和非递归两种形式分别实现)查找某一个int元素。 2、使用一维数组编码实现一个栈(Stack)类,要求提供以下操作:(1)boolean isEmpty():判断栈当前是否为空;(2)入栈操作void push(obj):把数据元素obj插入堆栈;(3)出栈操作Object pop():出栈,并返回删除的数据元素;(4)Object getTop():取堆栈当前栈顶的数据元素并返回。编写代码测试所形成的Stack类,然后利用Stack类实现以下功能:输入一个正整数,输出该整数所对应的二进制数。 3、按照要求使用Java编码。 1) 以类型int[][]声明一个叫matrix的二维数组变量,将矩阵初始化为一个5个元素的数组。 2) 以下列方式为matrix的内部元素赋值:matrix从零开始循环到其长度值;例如索引为i,在每次迭代中,将matrix[i]指向一个新的整数数组,其长度为i。然后用索引变量j,对数组中的每一个元素进行循环。在每次内部循环中,将matrix[i][j]赋值为(i*j)。 3) 通过循环打印matrix中的所有元素,结果为: <> <0> <0 2> <0 3 6> <0 4 8 12> 4、利用二维数组实现一个矩阵类:Matrix。要求提供以下操作:(1)set(int row, int col, double value):将第row行第col列的元素赋值为value;(2)get(int row,int col):取第row行第col列的元素;(3)width():返回矩阵的列数;(4)height():返回矩阵的行数;(5)Matrix add(Matrix b):返回当前矩阵与矩阵b相加后的结果矩阵;(6)Matrix multiply(Matrix b):返回当前矩阵与矩阵b相乘后的结果矩阵。(7)print():打印出当前矩阵的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值