数据结构 实验1


数据结构 实验1

#include <iostream>

using namespace std;
typedef struct STU
{
    // char name[20];
    // char sno[20];
    // int age;
    // float score;
    char xuehao[100];
    char xingming[100];
    char xingbie[100];
    char zhuanye[100];
    char shengri[100];
} Student;

typedef struct LNode
{
    Student data;
    struct LNode *next;
} LNode, *LinkList;

//初始化(带头结点的单链表)
int InitList(LinkList &L)
{
    L = new LNode;
    L->next = NULL;
    return 1;
}

//判断链表是否为空
int ListEmpty(LinkList L)
{
    if (L->next == NULL)
    {
        return 1; // 链表为空
    }
    else
    {
        return 0; //链表非空
    }
}

//获取链表长度
int ListLength(LinkList L)
{
    int length = 0;
    LNode *p;
    p = L->next;

    while (p)
    {
        p = p->next;
        length++;
    }

    return length;
}

//遍历链表
void TraveList(LinkList L)
{
    LNode *p;
    p = L->next;

    printf("链表结构如下:\n");
    while (p)
    {
        printf("%s %s %s %s %s", p->data.xuehao, p->data.xingming, p->data.xingbie, p->data.zhuanye, p->data.shengri);
        printf("\n");
        p = p->next;
    }
}

//插入操作
int ListInsert(LinkList &L, int location, Student &e)
{
    //在链表L的location位置插入元素e
    LNode *p;
    int j = 0;
    p = L;

    while (p && j < location - 1)
    {
        p = p->next;
        j++;
    }
    if (!p || j > location - 1)
    {
        return 0;
    }

    LNode *s;
    s = new LNode;
    s->data = e;
    s->next = p->next;
    p->next = s;

    return 1;
}

//删除操作
int ListDelete(LinkList &L, int location, Student &e)
{
    //删除L中location位置的元素,并用e返回其值
    LNode *p;
    int j = 0;
    p = L;

    while (p->next && j < location - 1)
    {
        p = p->next;
        j++;
    }
    if (!(p->next) || j > location - 1)
    {
        return 0;
    }

    LNode *q;
    q = p->next;
    p->next = q->next;
    e = q->data;
    delete q;
    return 1;
}
int ListChange(LinkList &L, int location)
{

    LNode *p;
    int j = 0;
    p = L;

    while (p->next && j < location)
    {
        p = p->next;
        j++;
    }
    if ( j > location)
    {
        return 0;
    }
    printf("请输入学号:");
    scanf("%s", p->data.xuehao);
    printf("请输入姓名:");
    scanf("%s", p->data.xingming);
    printf("请输入性别:");
    scanf("%s", p->data.xingbie);
    printf("请输入专业:");
    scanf("%s", p->data.zhuanye);
    printf("请输入出生年月:");
    scanf("%s", p->data.shengri);
    return 1;
}


// 尾插法创建单链表
void CreateList2(LinkList &L, int n)
{
    L = new LNode;
    L->next = NULL;
    LNode *r;
    r = L;

    printf("请输入链表元素值:\n");
    for (int i = 0; i < n; i++)
    {
        LNode *p;
        p = new LNode;
        printf("请输入第%d个元素的值:\n", i + 1);
        printf("请输入学号:");
        scanf("%s", p->data.xuehao);
        printf("请输入姓名:");
        scanf("%s", p->data.xingming);
        printf("请输入性别:");
        scanf("%s", &p->data.xingbie);
        printf("请输入专业:");
        scanf("%s", &p->data.zhuanye);
        printf("请输入出生年月:");
        scanf("%s", &p->data.shengri);

        p->next = NULL;
        r->next = p;
        r = p;
    }
}
int main()
{
    LinkList L;

    int choose = -1;
    if (InitList(L))
    {
        printf("链表初始化成功!\n");
    }
    else
    {
        printf("链表初始化失败!\n");
    }
    while (choose != 0)
    {
        printf("1.建立\n");
        printf("2.插入\n");
        printf("3.删除\n");
        printf("4.查询\n");
        printf("5.修改\n");
        printf("0.退出\n");
        printf("请选择操作:\n");
        scanf("%d", &choose);
        switch (choose)
        {
        case 1:
        {

            printf("请输入链表长度:\n");
            int n;
            scanf("%d", &n);
            CreateList2(L, n);
            TraveList(L);
            break;
        }
        case 2:
        {
            printf("请输入插入的位置:\n");
            int location;
            scanf("%d", &location);

            Student stu;
            printf("请输入学号:");
            scanf("%s", stu.xuehao);
            printf("请输入姓名:");
            scanf("%s", stu.xingming);
            printf("请输入性别:");
            scanf("%s", &stu.xingbie);
            printf("请输入专业:");
            scanf("%s", &stu.zhuanye);
            printf("请输入出生年月:");
            scanf("%s", &stu.shengri);

            if (ListInsert(L, location, stu))
            {
                printf("插入成功!\n");
                TraveList(L);
            }
            else
            {
                printf("插入失败!\n");
            }
            //TraveList(L);
            break;
        }
        case 3:
        {
            printf("请输入要删除的元素的位置:\n");
            int location;
            scanf("%d", &location);

            Student stu;

            if (ListDelete(L, location, stu))
            {
                printf("删除成功!\n");
                printf("删除的元素值是:\n");
                printf("%s %s %s %s %s\n", stu.xuehao, stu.xingming, stu.xingbie, stu.zhuanye, stu.shengri);
                TraveList(L);
            }
            else
            {
                printf("删除失败!\n");
            }
            break;
        }
        case 4:
        {
            TraveList(L);
            break;
        }
        case 5:
        {
            printf("请输入修改的位置:\n");
            int location;
            scanf("%d", &location);
            
            if (ListChange(L, location))
                printf("修改成功!");
            else
                printf("修改失败!");
            
            TraveList(L);
            break;
        }
        }
    }

    system("pause");
}


文章作者: LHL
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 LHL !
评论
 上一篇
One more thing... One more thing...
One more thing…
2020-11-12 LHL
下一篇 
204实验 204实验
Here's something encrypted, password is required to continue reading.
2020-11-09 LHL
  目录