单链表的所有操作list

#include <stdio.h>
#include <stdlib.h>


typedef struct _list
{
int m_data;//成员变量
struct _list * next;//结构体调用自己的类型
}list,* node;




// 创建链表
void listCreate(list * t_list);


//插入数据
//头插法
void listInsert(list * t_list,int t_data);
//尾插法
void listTailInsert(list * t_list,int t_data);


//修改数据
void listModify(list * t_list,int t_old,int t_new);


//删除数据
void listDelete(list * t_list,int t_data);


//查找数据
node listFind(list * t_list,int t_data);


//打印数据
void listDisplay(list * t_list);


//void listDisplay(list * t_list);
void listDestroy(list * t_list);


//翻转
void reverse(list * t_list);


//求长度
int listLenght(list * t_list);


//排序
void listSort(list * t_list);
//list类型 我们叫链表
//List*类型 我们叫节点


int main(void)
{
    printf("Hello world!\n");
    //创建一个list变量
list t_list;


//创建链表
listCreate(&t_list);


int i = 0;
for(i = 0;i < 10;i++)
{
#if 0  //插入数据
 listInsert(&t_list,i);
#else
 listTailInsert(&t_list,i);
#endif
}
printf("listFind\n");


node t_node = listFind(&t_list,3);


printf("----------------------------------------------\n");


if(NULL == t_node)
{
printf("this code is not here\n");
}else
{
printf("%d\n",t_node->m_data);
}
listModify(&t_list,1,999);


listDelete(&t_list,5);


listSort(&t_list);
    //打印链表数据
listDisplay(&t_list);


printf("-----------------------------------------------\n");
    //销毁链表


    reverse(&t_list);


    listDisplay(&t_list);
    printf("-------------------------------------------------\n");
listDestroy(&t_list);
    return 0;
}


void listCreate(list * t_list)
{
//初始化链表内数据
t_list->m_data = -1;
    t_list->next = NULL;
}


void listInsert(list * t_list,int t_data)
{
//创建一个节点,并申请内存
#if 0
node t_node = (node)malloc(sizeof(list));//node = list *;
#else
list * t_node = (list *)malloc(sizeof(list));
#endif
//节点内容赋值
t_node->m_data = t_data;
t_node->next = NULL;


//头插法,新数据在前
    t_node->next = t_list->next;
t_list->next = t_node;
}


//查找数据
node listFind(list *t_list, int t_data)
{
//申明一个空节点
node t_node = NULL;


//遍历链表
node t_temp;
for(t_temp = t_list->next;t_temp !=NULL;t_temp =t_temp->next)
{
//如果找到该节点
if(t_temp->m_data == t_data)
{
//
t_node = t_temp;
//跳出循环
break;
}
}
return t_node;
}


//尾插法 新数据在后
void listTailInsert(list *t_list,int t_data)
{
//创建一个节点
node t_node = (node)malloc(sizeof(list));


//节点内容赋值
t_node->m_data = t_data;
t_node->next = NULL;


//声明一个尾节点
node t_tail = t_list;
//获取最后一个节点
while(t_tail->next != NULL)
{
//后移
t_tail = t_tail->next;
}
//添加节点
t_tail->next = t_node;
}
//删除数据
void listDelete(list * t_list,int t_data)
{
//查找是否存在改制的数据
node t_node = listFind(t_list,t_data);


//如果该值对应的节点不存在
if(NULL == t_node)
{
printf("该值不存在\n");
return;
}
//求出被删节点的前一个节点
node t_prev = t_list;
//遍历链表
while(t_prev->next != t_node)
{
t_prev = t_prev->next;
}
//前一个节点的next指向被删除节点的下一个节点
t_prev->next = t_node->next;
//释放内存
free(t_node);
//指针置空
t_node = NULL;
}
//修改数据
void listModify(list * t_list,int t_old,int t_new)
{
//查找值是否存在
  node t_node = listFind(t_list,t_old);


  //判断值是否存在
  if(t_node == NULL)
  {
  printf("该值不存在\n");
  return ;
  }
  t_node->m_data = t_new;
}


void listDisplay(list * t_list)
{
    //便利链表
node t_temp;


for(t_temp = t_list->next;t_temp != NULL; t_temp = t_temp->next)
{
printf("%d ",t_temp->m_data);
}
printf("\n");
}


void listDestroy(list * t_list)
{
//便利链表
node t_temp = t_list->next;


while(t_temp != NULL)
{
//先将当前节点保存
node t_node = t_temp;
//移动到下一各节点
t_temp = t_temp->next;
//释放保存内容的节点
free(t_node);
}
printf("all delete over\n");
}


int listLength(list * t_list)
{
node t_temp;
int t_length = 0;
for(t_temp = t_list->next;t_temp != NULL;t_temp = t_temp->next)
{
t_length++;
}
return t_length;
}


void listSort(list * t_list)
{
int t_length = listLength(t_list);


int i,j;


node t_temp;
for(i = 0;i < t_length;i++)
{
t_temp = t_list->next;
for(j = 0;j < t_length - i - 1;j++)
{
if(t_temp->m_data > t_temp->next->m_data)
{
int t_data = t_temp->m_data;
t_temp->m_data = t_temp->next->m_data;
t_temp->next->m_data = t_data;
}
t_temp = t_temp->next;
}
}
}


/**翻转**/
void reverse(list * t_list)
{
    list * head = NULL,*now = NULL,*temp = NULL;
    head = t_list->next;
    // head是来保存我们翻转以后链表中的头节点的
    now = head->next;
    // now用来保存我们当前待处理的节点
    head->next = NULL;
    // 一定要置为NULL,否则可能导致循环


    while(now)
    {
        temp = now->next;
        // 利用一个临时指针来保存下一个待处理的节点
        now->next = head;
        // 将当前节点插入到逆序节点的第一个节点之前,并更改head指向
        head = now;
        t_list->next = head;
        // 使链表头指针指向逆序后的第一个节点
        now = temp;
        // 更新链表到下一个待处理的节点
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

telllong

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值