• 数据结构——链式线性表


    代码如下:

    #include<stdio.h> //此链表设有头结点
    #include<stdlib.h>
    #include<conio.h>
    #define ERROR 0
    #define OK 1
    #define OVERFLOW -2
    typedef int Elemtype;
    typedef int Status ;

    typedef struct LNode
    {
    Elemtype data;
    struct LNode *next;
    }LNode,*Linklist;
    //函数声明应放在定义结构体之后
    LNode * Creat(int n);
    Status Insert(Linklist L,int i,Elemtype e);
    Status Del(Linklist L,int i,Elemtype *e);
    Status Find(Linklist L,int i,Elemtype *e);
    void display(Linklist L);

    LNode * Creat(int n)
    {
    LNode *p,*q,*L;
    int i;
    p=(Linklist)malloc(sizeof(LNode));
    if(!p)
    {
    return ERROR;
    exit(OVERFLOW);
    }
    p->data=0;//头结点存放的值
    for(i=1;i<=n;i++)
    {
    if(i==1)L=p;
    else
    q->next=p;
    q=p;
    p=(Linklist)malloc(sizeof(LNode));
    scanf("%d",&p->data);
    }
    q->next=p;
    q=p;
    q->next=NULL;
    return L;


    }
    Status Insert(Linklist L,int i,Elemtype e)
    {
    int j=0;
    LNode *p,*q;
    p=L;
    while(p&&j<i-1)
    {
    p=p->next;
    j++;
    }
    if(!p||j>i-1)
    {
    printf("插入失败!\n\n");
    return ERROR;
    }
    q=(Linklist)malloc(sizeof(LNode));
    q->data=e;
    q->next=p->next;
    p->next=q;

    return OK;


    }
    Status Del(Linklist L,int i,Elemtype *e)
    {
    LNode *p,*q;
    int j=0;
    p=L;
    while(p->next&&j<i-1)//寻找第i个结点,并p指向其前驱
    {
    p=p->next;
    j++;
    }
    if(!p->next||j>i-1)
    {
    printf("删除失败!\n");
    return ERROR;
    }
    else
    {
    q=p->next;
    p->next=q->next;
    *e=q->data;
    free(q);
    return OK;
    }
    }
    Status Find(Linklist L,int i,Elemtype *e)
    {
    LNode *p;
    int j=0;
    p=L;
    while(p&&j<i)
    {
    p=p->next;
    j++;
    }
    if(!p||j>i)
    {
    printf("查找失败!\n");
    return ERROR;
    }
    else
    *e=p->data;
    return OK;
    }
    void display(Linklist L)
    {
    LNode *p;
    p=L->next;
    while(p)
    {
    printf("%d ",p->data);
    p=p->next;
    }
    putchar('\n');
    }
    int main()
    {
    LNode *p;
    int n,i,t=1;
    Elemtype e;
    char c;
    printf("请输入你要输入的元素的个数:\n");
    scanf("%d",&n);
    printf("请分别输入各个元素:");
    p=Creat(n);

    while(t)
    {
    printf("1)插入元素\n2)删除元素\n3)查找某个元素\n4)显示所有元素\n0)退出\n");
    getchar();
    c=getchar();
    switch(c)
    {
    case '1':printf("请输入你要插入的元素的位置:\n");
    scanf("%d",&i);printf("请输入插入的元素:\n");
    scanf("%d",&e);
    Insert(p,i,e);
    t=1;
    break;
    case '2':printf("请输入你要删除的元素的位置:\n");
    scanf("%d",&i);
    if(Del(p,i,&e)==OK)
    printf("删除的元素为:%d\n",e);
    t=1;
    break;
    case '3':printf("请输入你要查找的元素的位置:\n");
    scanf("%d",&i);
    if(Find(p,i,&e)==OK)
    printf("查找的元素为:%d\n",e);
    t=1;
    break;
    case '4':display(p);t=1;break;
    case '0':t=0;break;
    }
    if(t)
    {
    printf("请按任意键继续……\n");
    getch();
    system("cls");
    }
    }

    return 0;
    }

    总结:

    1)链表最好带上头结点,容易操作;

    2)用返回的状态来控制输出

    3)传地址的操作,若函数的改变,则原来的也变,但是我试了一下,创建函数,只能返回头指针,才能用display()函数输出;

    4)Del()函数中,需用中间变量q代替p->next,不能直接p,而free(p->next),会出错的,试过了。

    5)Del()和Find()函数中,注意是怎样传入,和输出的。

  • 相关阅读:
    [LeetCode] 39. Combination Sum 组合之和
    CSS3
    常见中文字体在CSS中的Unicode编码(宋体:5B8B4F53)
    List<Object> 使用Linq
    查看工作流详情页面
    java程序调用.net接口服务地址的写法
    C# Repeater 嵌套
    JavaScript刷新页面,不重复提交
    Migration-添加表(加外键)
    Migration-添加表
  • 原文地址:https://www.cnblogs.com/hsqdboke/p/2430580.html
Copyright © 2020-2023  润新知