#include <stdio.h>
#include <stdlib.h>
#include "./func.h"
//创建单链表
struct node* link()
{
struct node* head=(struct node*)malloc(sizeof(struct node));
if(NULL==head)
{
printf("单链表创建失败\n");
return NULL;
}
head->txt.len=0;
head->next=NULL;
return head;
}
//头插
void headmeg(struct node* head,datatype data)
{
struct node*temp=(struct node*)malloc(sizeof(struct node));
if(NULL==temp)
{
printf("插入数据失败\n");
return;
}
//初始化
temp->txt.data=data;
temp->next=NULL;
temp->next=head->next;
head->next=temp;
head->txt.len++;
}
//尾插
void winser(struct node *head,int data)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
struct node *p=head;
//初始化
temp->txt.data=data;
temp->next=NULL;
if(NULL==temp)
{
printf("创建失败\n");
}
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
head->txt.len++;
printf("插入成功\n");
}
//遍历链表
void showpoint(struct node *head)
{
if(NULL==head)
{
printf("链表出错\n");
return;
}
//按位置插入
void wzinsert(struct node* head,int index,datatype data)
{
if(index<1 || index>head->txt.len+1)
{
printf("插入位置非法");
return;
}
if(NULL == head)
{
printf("传参失败");
return;
}
struct node* p=head;
//创建temp结点并初始化
struct node* temp=(struct node*)malloc(sizeof(struct node));
if(NULL == temp)
{
printf("创建失败");
return;
}
temp->txt.data=data;
temp->next=NULL;
//移动指针
for(int i=0;i<index-1;i++)
{
p=p->next;
}
temp->next=p->next;
p->next=temp;
head->txt.len++;
return;
}
//按位置删除
void del(struct node *head,int index)
{
if(index<=0 || index>head->txt.len)
{
printf("位置不合法\n");
}
struct node *p=head;
for(int i=0;i<index-1;i++)
{
p=p->next;
}
//备份删除节点
struct node *temp=p->next;
p->next=temp->next;
free(temp);
head->txt.len--;
printf("删除成功\n");
//快慢指针
struct node *kM_list(struct node *head)
{
//判断链表是否为空
if(head->next==NULL)
{
printf("链表为空\n");
}
//定义指针
struct node *low,*K;
low=K=head->next;
while(K->next!=NULL && K->next->next!=NULL)
{
low=low->next;
K=K->next->next->next;
}
return low;
}
数据结构day2
最新推荐文章于 2024-07-20 11:22:21 发布