albert_m
albert_m
2017-10-23 13:07

是不是赋值失败了,没理由的,请大家帮忙看看。

  • c
  • 线性表
  • 结构

 #include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define ERROR 0
#define OK    1
#define OVERFLOW -2
#define info      0
typedef int ElemType;
typedef int Status;
typedef struct  Lnode {//线性表的链式结构
    ElemType data;
    struct  Lnode *next;
}Node;
typedef struct Lnode  *LinkList;
Status InitList_L(LinkList *L)  //初始化线性表
{
    *L = (LinkList)malloc(sizeof(Lnode));
    if (!*L)return ERROR;
    (*L)->next = NULL;
    (*L)->data = info;
    return OK;
}

Status GetElem(LinkList *L, int i, ElemType &e)//访问第i个元素并用e返回其值
{
    int j = 1;
    LinkList p;
    p = (*L)->next;

    while (p&&j<i)
    {
        p = p->next;
        p++;
    }
    if (!p || j>i)return ERROR;
    e = p->data;
    return e;
}
Status  ListInsert_L(LinkList *L, int i, ElemType &e)//在第i个位置前插入元素e
{
    LinkList p;
    LinkList s;
    p = *L;
    int j = -1;

    while (p&&j < i - 1)
    {
        p = p->next; ++j;
    }
    if (!p || j>i - 1)return ERROR;
    s = (LinkList)malloc(sizeof(Lnode));
    s->data = e;
    s->next = p->next;
    p->next;
    return OK;
}
Status ListDelate_L(LinkList *L, int i, ElemType e)//删除第i个元素,并用e返回其值
{
    LinkList p;
    p = *L;
    LinkList q;
    int j = 0;
    while (p->next&&j<i - 1)
    {
        p = p->next; ++j;
    }
    if (!(p->next) || j>i - 1)return ERROR;
    q = p->next;
    p->next = q->next;
    e = q->data;
    free(q);
    q->next = NULL;
    return OK;
}
Status ListEmpty(LinkList *L)//判断表是否为空
{
    if ((*L)->next)
        return ERROR;
    else
        return OK;
}
void Creatlist_L(LinkList *L, int n)
{
    //逆序输入
    int i;
    LinkList p=NULL;
    *L = (LinkList)malloc(sizeof(Lnode));
    (*L)->next = NULL;
    printf("请为该结点赋值!\n");
    for (i = n; i > 0; --i)
    {

        p = (LinkList)malloc(sizeof(Lnode));
        scanf("%d",&(p)->next);
        (p)->next = (*L)->next;
        (*L)->next = p;
    }
}
Status Traversal(LinkList *L)  //遍历线性表
{

    if ((*L)->next == NULL) {
        printf("线性表为空!\n");
        return 0;
    }
    LinkList p;
    p = (*L)->next;
    printf("线性表各元素的值:\n");
    while (p != NULL)
    {
        printf("%d\n", p->data);
        p = p->next;
    }
    return 0;

}
Status Operatemenu(LinkList *L)  //操作菜单
{
    int num;
    printf("\t\t\t------****操作菜单****------\n");
    printf("\t\t\t------    1 访问元素\n");
    printf("\t\t\t------    2 插入元素\n");
    printf("\t\t\t------    3 删除元素\n");
    printf("\t\t\t------    0 退出系统\n\n\n");
    printf("\t\t\t------    请选择你所需要的操作\n");
        scanf("%d", &num);
        switch (num)
        {
        case 1:
            int i0, e0;
            printf("请输入你所访问元素的位置i0=");
            scanf("%d", &i0);
            GetElem(L, i0, e0);
            printf("%d", GetElem(L, i0, e0));
            break;
        case 2:
            int i1, e1;
            printf("请输入你所要插入元素的值e1=");
            scanf("%d", &e1);
            printf("请输入你所要插入元素的位置i1=");
                scanf("%d", &i1);
            ListInsert_L(L, i1, e1);
            Traversal(L);
            break;
        case 3:
            int i2;
            int e2;
            printf("请输入你要删除元素的位置=");
                scanf("%d",&i2);
            ListDelate_L(L,i2,e2);
            printf("删除成功\n");
            Traversal(L);
            break;
        case 0:
            printf("正在退出\n");
            exit(0);
        default:
            printf("输入有误!┑( ̄Д  ̄)┍\n");

        }
        return OK;
}
int main()
{
    int NodeLength,i,val;
    //LinkList p;
    LinkList L;
    printf("请输入你要建立线性表的长度\n");
    scanf("%d", &NodeLength);
    Creatlist_L(&L, NodeLength);
    Traversal(&L);
    getchar();
    char opp;
    do
    {
        printf("请输入你所需要的操作\n");
        Operatemenu(&L);
        getchar();
        printf("是否继续y/n\n");
        scanf("%c", &opp);

    } while (opp =='Y' || opp == 'y');
    return 0;

}

图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐

换一换