栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

单链表操作大全(C语言)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单链表操作大全(C语言)

#include 
#include 
#include


typedef int Elemtype;

//单链表

typedef struct LNode
{
    Elemtype data;//存放数据
    struct LNode *next; //指向LNode的一个指针

} LNode,*LinkList; //相当于取别名,LNode代表一个节点,LinkList代表整个单链表(指向LNode的一个指针)

void PrintList(LinkList L)
{
    LinkList p;
    p=L->next;//找到头指针指向节点,开始遍历
    printf("链表元素如下:n");
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("n");
}

//头插法建立链表 (打印倒叙打印)
LinkList List_HeadInsert(LinkList L)
{
    LNode *s;
    int x;
    L=(LinkList)malloc(sizeof(LNode));//创建头节点
    L->next=NULL;
    scanf("%d",&x);
    while(x!=9999)
    {
        s=(LNode*)malloc(sizeof(LNode));//创建新节点
        s->data=x;
        s->next=L->next;
        L->next=s;
        scanf("%d",&x);
    }
    return L;
}

//尾插法
LinkList List_TailInsert(LinkList L)
{
    int x;
    L=(LinkList)malloc(sizeof(LNode));
    LNode *r=L,*s;
    scanf("%d",&x);

    while(x!=9999)
    {
        s=(LNode*)malloc(sizeof(LNode));
        s->data=x;
        r->next=s;
        r=s;
        scanf("%d",&x);
    }
    r->next=NULL;
    return L;
}

//按序号查找节点值
LNode *GetElem(LinkList L,int i)
{
    int j=1;
    LNode *p=L->next;//拿到一个节点(不是头节点),实际上就是开辟两个指针,指向同一个节点区域,指针不断向下移动,链表是一动不动的!

    if(i==0)return L;//返回头节点
    if(i<1)return NULL;

    while(p&&j
        p=p->next;
        j++;
    }
    return p;
}

//按值查找节点
LNode *LocateElem(LinkList L,Elemtype e)
{
    LNode *p=L->next;//拿到第一个节点
    while(p!=NULL&&p->data!=e)
    {
        p=p->next;
    }
    return p;
}

int GetLength(LinkList L)
{
    if(L->next==NULL)return 0;
    int len=0;
    LNode *r=L->next;
    while(r!=NULL)
    {
        r=r->next;
        len++;
    }
    return len;
}
//前插节点
bool InsertLinkList(LinkList L,int pos,Elemtype e)
{
    if(pos<1||pos>GetLength(L)+1)return false;
    LNode *r=L,*n;//尾指针,和新的节点,尾指针用来移动遍历
    n=(LNode*)malloc(sizeof(LinkList));
    n->data=e;
    n->next=NULL;
    while(--pos>0) //移动到pos位置的前一个节点
    {
        r=r->next;
    }
    n->next=r->next;
    r->next=n;
    return true;
}


//删除节点前元素
bool DeleteLinkList(LinkList L,int pos,Elemtype *e)
{
    if(pos<1||pos>GetLength(L))return false;
    LNode *r=L,*p;//p等待删除节点,r尾指针用来遍历
    while(--pos>0)
    {
        r=r->next;
    }
    p=r->next;
    *e=p->data;
    r->next=p->next;
    free(p);//释放指针,防止野指指针
    return true;
}

int main()
{

    LinkList L;//使用头插法 这里的L1就是一个头指针
    printf("请输入链表元素,输入9999表示结束n");
    L=List_TailInsert(L); // 1 2 3 4
    PrintList(L);
    printf("当前链表长度为:%d",GetLength(L));
    printf("插入元素n");
    InsertLinkList(L,1,-1);
    PrintList(L);
    int *e;
    DeleteLinkList(L,1,e);
    printf("删除元素%d",*e);
    printf("删除后链表为:n");
    PrintList(L);

    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/850818.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号