不带头结点的 单链表的各种操作实现c语言

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
typedef int ElementType;
typedef struct Node
{
    ElementType element;
    struct Node* next;
}Node;
///创建空链表
void creatList(Node ** pL)
{
    *pL = NULL;
}
///创建新的节点
Node *creatNode(int item)
{
    Node *tmp = (Node*)malloc(sizeof(struct Node));
    if(!tmp) return NULL;///判断可能内存已满,不能申请了
    tmp->element = item;
    tmp->next = NULL;
    return tmp;
}
///在链表头插入节点
void inserthead(Node ** pL,int item)
{
    Node *tmp = creatNode(item);
    if(!tmp) printf("内存已满,无法申请内存");
    else
    {
        tmp ->next = *pL;
        *pL = tmp;
    }
}
///在链表结尾插入新的节点
void insertlast(Node ** pL,int item)
{
    Node *p ;
    p = *pL;
    Node *tmp = creatNode(item);
    if(!tmp) printf("内存已满,无法申请内存");
    else
    {
        if(p == NULL)
            *pL = tmp;///让p 和*pL指向同一个地方,但是现在又让p指向tmp这个节点,但是*pL仍然指向原来的地方,没有改变*pL指向的值
        else{
        while(p->next!=NULL)
            p = p->next;
        p->next = tmp;
        }
    }
}
///在链表的指定位置插入值
void insertatlocation(Node **pL,int item,int index)
{
    if(index < 1) printf("插入的位置不可以");
    if(index == 1)
    {
        inserthead(pL,item);
        return;
    }
    Node *p = *pL;
    int cnt = 1;
    while(p&&cnt < index-1)
    {
        p = p->next;
        cnt++;
    }
    if(p)
    {
        Node *tmp = creatNode(item);
        if(!tmp) printf("内存已满,无法申请内存");
        tmp->next = p->next;
        p->next= tmp;
    }
    else
        printf("插入的位置不可以");
}
///打印链表中的数据
void print(Node *pL)
{
    if(!pL)
      printf("empty!");
    else
    {
        while(pL)
        {
            printf("%d ",pL->element);
            pL = pL->next;
        }
    }
    printf("\n");
}
///计算链表的长度
int sizeList(Node *L)
{
    Node *p = L;
    int cnt = 0;
    while(p)
    {
        cnt++;
        p = p->next;
    }
    return cnt;
}
///按值查找
Node* findkey(Node *L,int item)
{
    Node *p = L;
    while(p&&p->element != item)
        p = p->next;
    return p;
}
///按位置查找
int findkth(Node*L,int item)
{
    int cnt = 1;
    Node *p = L;
    while(p&&cnt < item)
    {
        p = p->next;
        cnt++;
    }
    if(cnt == item) return p->element;
    else return -1;
}
///删除某个位置上的节点
void delatlocation(Node **pL,int index)
{
    Node *p = *pL;
    Node *tmp;
    if(index == 1)
    {
        tmp = *pL;
        *pL = (*pL)->next;
        free(tmp);
    }
    else{
    int cnt = 1;
    while(p&&cnt < index-1)
    {
        cnt++;
        p = p->next;
    }
    if(p == NULL ||cnt != index-1||p->next==NULL)
    {
        printf("插入的位置错误");
    }
    else
    {
        tmp = p->next;
        p->next = p->next->next;
        free(tmp);
    }
}
}
///销毁真个链表
void delList(Node **pL)
{
    Node *tmp;
    Node *p = *pL;
    //*pL = NULL;///必须有
    while(p)
    {
        tmp = p->next;
        printf("%d\n",p->element);
        free(p);
        p = tmp;
    }
    *pL = NULL;///必须有,但是还不明白
}
///主函数进行测试,调用函数
int main()
{
    Node* L;
    creatList(&L);
    inserthead(&L,3);
    inserthead(&L,4);
    insertlast(&L,5);
    insertlast(&L,6);
    print(L);
    insertatlocation(&L,7,2);
    print(L);
    printf("%d\n",sizeList(L));
    findkey(L,5);
    //printf("%d\n",findkth(L,5));
    delatlocation(&L,5);
    print(L);
    delList(&L);
    print(L);
    return 0;
}

  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值