c语言链表的增删改查

#include<iostream>
#include<stdlib.h>
using namespace std;
struct stu{
 int num;
 struct stu *next;
};
struct stu *create(int n)
{
 struct stu*head,*p,*q;
 head=new stu;
 p=head;
 q=head->next;
 for(int i=0;i<n;i++)
 {
  q=new stu;
  cin>>q->num;
  p->next=q;
  p=q;
  
  }
  p=p->next;
  return head;
}
void print(struct stu *head)
{
 struct stu *p;
 p=head->next;
 while(p)
 {
  cout<<p->num<<"\t";
  p=p->next;
  }
}
struct stu *del(struct stu *head)
{
 struct stu *p,*q;
 p=head;  
 q=head->next;
 while(p)
 {
  if(q->num==4)
  {
   p->next=q->next;
   free(q);
   break;
  }
  p=p->next;
  q=q->next;
 }
 return head;
}
struct stu *chang(struct stu *head)
{
 struct stu *p;
 
 p=head;
 
 while (p)
 {
  
  if(p->num==7)
  {
   p->num=2;
  }
  p=p->next;
 }
 cout<<endl;
 return head;
}
 struct stu *add(struct stu *head)
{
 struct stu *p,*pnew;
 p=head->next;
 pnew=new stu; //申请空间
 cin>>pnew->num;
 while(p!=NULL)
 {
  if(p->num==8)
  {
   pnew->next=p->next;
   p->next=pnew;
   break;
  }
  p=p->next;
 }
 return head;
}
struct stu *check(struct stu *head)
{
 struct stu *p;
 p=head->next;
 while(p)
 { 
  
  if(p->num==2)
  {
   cout<<"YES";
   break;
  }
  p=p->next;
  
 } 
 if(p==NULL)
 cout<<"NO";
 return head;
}
int main()
{
 struct stu *head;
 head=create(4);
 print(head);
 head=del(head);
 print(head);
 head=chang(head);
 print(head);
 head=add(head);
 print(head);
 head=check(head);
 //print(head);
 
 return 0;
 }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值