C语言学习第十次打卡

Linux系统下编写关于链表的程序:包含查询、删除、数据前方插入新数据、数据后方插入新数据。

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

enum func{dataSeek=1,dataDelect,froInsert,backInsert};
enum func meiju;
struct link
{
int data ;
struct link* next;
};


void func_Display(struct link*head)//打印链表数据   
{
printf ("链表数据:");
while(1)
{
  if(head->next!=NULL)
  {
  printf ("%d ",head->data);
  head=head->next;
  }
  else
  {
  printf ("%d ",head->data);
  break;
  }
}
puts("/n");
}


//功能选择函数
void sele_Func(void)
{
printf("请输入指令序号:\n1.dataSeek\n2.dataDelect\n3.froInsert\n4.backInsert\n");
printf("序号 ");
scanf("%d",&meiju);
}

//计算链表数量函数
int linkNum(struct link*head)
{int num=0;
while (head->next!=NULL)
{ head=head->next;
  num++;
}
return num;
}

//前插入函数
struct link* froInsertFunc(struct link*head,int data,struct link* new)
{
struct link* p1=head;//p1用来操作,head不操作。
if(p1->data==data)//插入点,头前
{
   {
   new->next=head;
   return new;
   }
}
while(p1->next->next!=NULL)//操作点头以外部分,循环找点位
{    
p1=p1->next;
}
if(p1->next->data==data)//找到点位
{
new->next=p1->next;
p1->next=new;
}
else{puts("未找到对应的数据插入点");}//链表未找到目标点位
return head;
}

//后插入函数
struct link * backInsertFunc(struct link*head,int data,struct link*new)
{
struct link*p1=head;
while(p1->next!=NULL)//对尾巴以外的数据做判断
{
  if(p1->data==data)
  {
  new->next=p1->next;
  p1->next=new;
  return head;
  }
  p1=p1->next;
}
//p1->next==NULL跳出了循环,最后判断尾巴是不是目标数据。
if(p1->data==data)
{  p1->next=new;
   new->next=NULL;
   return head;
}
else{puts("未找到对应的数据插入点");}//链表未找到目标点位
return head;
}

//数据删除函数
struct link * dataDelectFunc(struct link*head,int data)
{
struct link* p1=head;//p1用来操作,head不操作。
if(p1->data==data)//删除的是链表的头
{
   if(p1->next!=NULL)//是链表的头的情况下而且链表下一位不是NULL;
   {
   head=head->next;
   return head;
   }
   else//如果链表只有一个数据
   {
   p1=NULL;
   return NULL;
   }
}
while(p1->next!=NULL)//操作头以外的数据
{
  if(p1->next->data==data)
  {  
    p1->next=p1->next->next;
    return head;
  }
  p1=p1->next;
}

//删除的是中间或者尾巴的情况下。返回的还是head。
if(p1->next==NULL)
{
  if( p1->data!=data)//删除的是尾巴
  {  
puts("未找到想要删除的数据");
  }
return head;
}
}



//int seeknum;//定义一个全局变量

//链表位置查询
int  dataSeekFunc(struct link*head,int data,int total)//total链表数量
{
int seeknum=1;
while (seeknum<=(total+1))
{
  if(head->data==data)
  { return seeknum;}//return在链表的位置。
  head=head->next;
  seeknum++;
}
printf("未匹配到目标数据\n");
return 0;
}


//功能集合函数
struct link* func_All(struct link  *head,int total)
{  struct link * allhead=NULL;
   switch(meiju)
{
case 1://函数1
{int seeknum2;
int datacase1;
printf("请输入数据data\n");
scanf("%d",&datacase1);
seeknum2=dataSeekFunc(head,datacase1,total);
if(seeknum2!=0)
{
printf("数据在链表的第%d位\n",seeknum2);
}
return head;
break;
}

case 2://函数2
{
struct link *phead1;
int insertdata0=0;
puts("请输入你要删除的数据:");
scanf("%d",&insertdata0);
phead1=dataDelectFunc(head,insertdata0);
allhead=phead1;
break;}

case 3://函数3
{
struct link*new1;
new1=(struct link*)malloc(sizeof(struct link));
printf("请输入新链表的数据data");
scanf("%d",&(new1->data));
new1->next=NULL;
int insertdata1=0;
puts("请输入你要插入的数据位置");
scanf("%d",&insertdata1);
struct link *phead2;
phead2=froInsertFunc(head,insertdata1,new1);
allhead=phead2;
break;}
case 4://函数4
{
struct link*new2;
new2=(struct link*)malloc(sizeof(struct link));
printf("请输入新链表的数据data");
scanf("%d",&(new2->data));
new2->next=NULL;
int insertdata2=0;
puts("请输入你要插入的数据位置");
scanf("%d",&insertdata2);
struct link*phead3;
phead3=backInsertFunc(head,insertdata2,new2);
allhead=phead3;
break;
}
default:{ break;}
}
return allhead;
}//结束

 
int main ()
{//初始化链表
struct link t1={1,NULL};
struct link t2={2,NULL};
struct link t3={3,NULL};
struct link t4={4,NULL};
struct link t5={5,NULL};
t1.next=&t2;
t2.next=&t3;
t3.next=&t4;
t4.next=&t5;
struct link *head=&t1;
int totalnum =0;
totalnum=linkNum(head);
func_Display(head);
sele_Func();//功能指令序号
struct link* allhead;
allhead =func_All(head,totalnum);//根据用户的需求对链表进行操作
func_Display(allhead);//输出处理过的链表
return 0;
}

这个练习综合很多前期的知识,需要对C语言很多细节进行理解,通过此练习在关于Linux系统编写、对C语言的编写经验上有所提高。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值