链表的基本操作(*╹▽╹*)

构成链表
头文件
#include<stdio.h> #include<stdlib.h> #include<string.h>

  • 创建节点(结构体)
 {
 int a;
 struct Node *next;
};//创建节点
 -

全局定义链表头尾指针

 struct Node *head=NULL;
struct Node *end=NULL;
  • 创建链表(增—尾添加)AddListTill(函数)
void AddListTill(int a)
{
 struct Node *temp=(struct Node*)malloc(sizeof(struct Node));
 temp->a=a;
 temp->next=NULL;
 if(head==NULL)
 {
  head=temp;
  end=temp;
 }else{
  end->next=temp;
  end=temp;
 }

头添

struct Node *readlist()
{
 int num;
 struct Node *head=NULL;
 while(scanf("%d",&num)&&num!=-1)
 {
  struct Node *p=(struct Node*)malloc(sizeof(struct Node));
  p->a=num;
  p->next=head;
  head=p;
 }
 return head;
}

1.创建一个节点
(给节点在堆区分配一段内存)
2.给节点数据进行赋值
3.尾添加(两种情况:a.一个节点都没有Head=NULL b.已经有了节点,添加到尾巴上)

  • 查—遍历链表(函数)
    1.普通的查(Scanlist)
void Scanlist()
 {
  struct Node *temp=Head;
  while(temp!=NULL)
   temp=temp->next;//++操作
 }

2.查询指定的节点(遍历,一个一个找)(FindNode)(结构体函数)

 struct Node *FindNode(int a)
 {
  struct Node *temp=head;
  while(temp!=NULL)
  {
   if(a==temp->a)
   {
   return temp;
   }
   temp=temp->next;
  }
   return NULL;
 }

定义一个临时变量指向头,对数据中的节点进行判断,若找到,返回该节点,没找到,返回NULL

  • 增—在指定位置插入节点(函数)(AddListRand)

1.没有节点(NULL=Head)
2.没找到此节点(FindNode)—返回为NULL
3.有此节点(在连接到链表上时,分为两种情况,找到的节点在尾部,找到的节点在中间)

void AddListRand(int index,int a)//在指定位置index增加节点a
//分为三种情况
{
 if(head=NULL)
 {
  printf("链表没有节点");
  return;
 }//
 struct Node *pt=FindNode(index);//pt就是找指定位置的指针
 if(NULL==pt)
 {
  printf("没有指定节点");
  return;
 }//
 struct Node *temp=(struct Node*)malloc(sizeof(struct Node));
 temp->a=a;
 temp->next=NULL;
 if(pt==end)//在尾部
 {
  end->next=temp;
  end=temp;
 }else{
  pt->next=temp->next;
  pt->next=temp;
 }
  • 删—链表清空(全部删除)(函数FreeList)

1.一个一个清空
2.记录头结点的位置,实现++,释放
3.头尾清空

void FreeList()
{
 struct Node *temp=head;
 while(temp!=NULL)
 {
  struct Node *pt=head;
  temp->next=temp;
  free(pt);
 }
 head=NULL;
 end=NULL;
}
  • 删—尾删除(函数DeleteListTail)

1.链表为空
2.只有一个节点
3.有大于1的节点(找到尾节点的前一个节点)

void DeleteListTail()
{
 if(head==NULL)
 printf("链表为空,无需删除");
 if(head==end)
 {
 free(head);
 head=NULL;
 end=NULL;
    }
    else{
     struct Node *temp=head;
     while(temp->next!=end)
     {
      temp=temp->next;
  }
  free(end);
  end=temp;
  end->next=NULL;//注意尾巴指针为空 
 }
}
  • 删—删除头(函数FreeHeadList)

1.空节点
2.非空节点

void FreeHeadList() 
{
 struct Node *temp=head;//记住旧的头节点 
 if(head==NULL)
 {
  printf("链表为空");
 }else{
  head=head->next; 
  free(temp);
 } 
}
  • 删—删除指定节点(函数DeleteListRand)

1.没有节点
2.没有指定节点
3.找到了指定节点:a.只有一个节点
b.只有两个节点(看删除头还是删除尾)
c.有多个节点(看删除头还是删除尾还是删除中间某个节点//直接跳过temp节点)

void DeleteListRand(int a)
{
 if(head==NULL)
 printf("链表没东西");// 
 struct Node *temp=FindNode(a);
 if(NULL==temp)
 {
  printf("查无此点")}//无指定节点 
 if(head==end)
 {
 free(head);
 head=NULL;
 end=NULL;
 }//有一个节点 
 else if(head->next=end)
 {
  if(end==temp)
  {
   DeleteListTail();
  }else if(head==temp)
  {
   DeleListHead();
  }
  }//有两个节点
  else
  {
   if(end==temp)
   DeleteListTail();
   else if(head==temp)
   DeleteListHead();
   else//删除中间某节点
   {
    struct Node *pt=head;
    while(pt->next!=temp) 
    {
     pt=pt->next;
    }
    pt->next=temp->next;
    free(temp);
  } 
    
   } 
}

主函数

void main()
{
struct Node *pFind;
for(i=0;i<5;i++)
{
AddListTill(i);
}
AddListRand(4,14);
DeleteListTail();
DeleteListRand(4);
ScanList();
FreeList();
pFind=FindNode(5);
if(pFind!=NULL)
{
printf("找到%d",pFind->a);
}else{
printf("No Find");

删除 (两个指针相互移动的那种)

struct ListNode *readlist()//创建链表 
{
 struct ListNode *head=NULL,*end=NULL;//这里头尾指针一定要指空!!!!!!(警告)
 int num;
 while(scanf("%d",&num)&&num!=-1)
 {
  struct ListNode *p=(struct ListNode*)malloc(sizeof(struct ListNode));
  p->data=num;
  if(head==NULL)
  {
   head=end=p;
  }else{
   end->next=p;
   end=p;
   p->next=NULL;
  }
 }
 return head;
}
struct ListNode *deletem( struct ListNode *L, int m )
{
 struct ListNode *temp,*p,*q;
 temp=L;
 while(temp->data==m&&temp->next!=NULL)
 {
  p=temp->next;
  free(temp);
  temp=p;
 }删除头
 if(temp->data==m)
 {
  return NULL;
 }删除尾
 p=temp;
 q=temp->next;//q找要删除的点 
 while(q)
 {
  if(q->data==m)
  {
   p->next=q->next;
   free(q);
   q=p->next;
  }else{
   p=q;
   q=q->next;
  }
 }
 return temp;
} 删除中间
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值