单向链表

#include <stdio.h>
#include <stdlib.h>//使用malloc函数时需要用到
struct node
{
  int data;
  struct node *next;
};//创建一个结构体用来表示链表的结点类型
void PrintList(struct node *L)//输出链表中的所有数
{
  struct node *p=L;
  while(p!=NULL)
  {
    printf("%d ",p->data);
    p=p->next;
  }
  printf("\n");
}
struct node *CreateList();
struct node *DeleteList(struct node *head,int x);
struct node *GetOdd(struct node **L);
struct node *InsertList(struct node *head,struct node *new_node);
int main()
{
  struct node *L;
  L=CreateList();//创建一个单向链表
  PrintList(L);
  
  int m;
  scanf("%d",&m);
  L=DeleteList(L,m);//删除链表中的特定元素
  PrintList(L);

  L=GetOdd(&L);//得到奇数值结点链表
  PrintList(L);

  struct node *new_node=(struct node*)malloc((sizeof(struct node)));;
  scanf("%d",&new_node->data);
  L=InsertList(L,new_node);
  PrintList(L);

  return 0;
}
struct node *CreateList()
{
  int data;
  struct node *head=NULL;//头指针初始为空
  struct node *q;

  while(scanf("%d",&data)&&data!=-1)
  {
    struct node *p=(struct node*)malloc((sizeof(struct node)));
    //动态申请一个空间,用来存放一个结点,并用临时指针p指向这个结点

    if(p!=NULL)//如果申请成功
    {
      p->data=data;//将数据存储到当前结点的data域中
      p->next=NULL;//设置当前结点的后继指针指向空,即当前结点的下一个结点为空
    }
    else exit(1);//否则终止程序

    if(head==NULL)//如果这是第一个创建的结点
    {
      head=p;//则头指针指向这个结点
    }
    else q->next=p;//否则将上一个结点的后继指针指向这个结点
    q=p;//指针q也指向当前结点,因为临时指针p将会指向新创建的结点
  }
  return head;//返回头指针
}
struct node *DeleteHead(struct node *head)//删除符合条件的首元素
{
  struct node *p=head;
  head=head->next;
  delete p;
  p=NULL;

  return head;//返回新的头指针
}

struct node *DeleteList(struct node *head,int x)
{
  struct node *pre,*p;

  while(head!=NULL&&head->data==x)//判断链表首元素是否符合条件
  {
    head=DeleteHead(head);
  }

  if(head!=NULL)//如果新的头指针不为空
  {
    pre=head;
    p=head->next;
    while(p!=NULL)//如果后继指针不为空
    {
      while(p!=NULL&&p->data==x)//如果后继指针所指向的data域符合条件
      {
        pre->next=p->next;
        delete p;
        p=NULL;
        p=pre->next;
      }
      if(p==NULL) break;//如果后继指针为空,则跳出循环
      pre=pre->next;
      p=p->next;
    }
  }
  return head;//返回头指针
}
struct node *GetOdd(struct node **L)
{
  struct node *head=NULL;
  struct node *q;

  while((*L)!=NULL)
  {
    int data=(*L)->data;
    struct node *p=(struct node*)malloc(sizeof(struct node));

    if(data&1)
    {
      if(p!=NULL)
      {
        p->data=data;
        p->next=NULL;
      }
      else exit(1);

      if(head==NULL)
      {
        head=p;
      }
      else q->next=p;
      q=p;
    }
   *L=(*L)->next;
  }
 return head;
}
struct node *InsertList(struct node *head,struct node *new_node)
{
  struct node *p=NULL;
  struct node *q=head;

  if(head==NULL)//如果头指针为空,直接插入
  {
    head=new_node;
    new_node->next=NULL;
    return head;
  }

  while((q->data)<=(new_node->data)&&(q->next)!=NULL)//遍历链表
  {
    p=q;
    q=q->next;
  }

  if((new_node->data)<(q->data))//如果找到一个q指向的结点的data比新的结点的data大,插在q结点的前面
  {
    if(q==head)//如果找到的结点是头结点,插到头结点的前面
    {
      head=new_node;
      new_node->next=q;
    }
    else//如果找到的是中间结点,插在中间结点q的前面
    {
      p->next=new_node;
      new_node->next=q;
    }
  }

  else//如果没有找到一个结点的data比新的结点的data大,插在尾部
  {
    q->next=new_node;
    new_node->next=NULL;
  }
  return head;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值