⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list1.c

📁 链表操作集合,包括了所有链表的有关操作,用c编写
💻 C
字号:



#include<stdio.h>

struct Link
{
    int data;
    struct Link *next;
};





/*创建链表并将文件中的数据读入链表中*/ 

struct Link *CreatLink()
{
  struct Link *p,*head;
  FILE *fp;
  int num;
  char filename[20];

  printf("please input the filename:");
  scanf("%s",filename);

  if((fp=fopen(filename,"r"))==NULL)
  {
    printf("cannot open file\n");
    getch();
    exit(0);
  }
  head=(struct Link *)malloc(sizeof(struct Link));
  head->next=NULL;
  p=head;
  do
  {
   fscanf(fp,"%d",&p->data);
   p->next=(struct Link *)malloc(sizeof(struct Link));
   p=p->next;
   p->next=NULL;
  }while(!feof(fp));
  printf("you have create the link!\n");
  fclose(fp);
  return head;
}

/*此函数用于显示链表*/
void DispLink(struct Link *head)
{
  struct Link *q;
   q=head;

  do{
      printf("%d\t",q->data);
      q=q->next;
    }while(q->next!=NULL);
    printf("\n");

}



/*此函数用于删除链表中的某一元素,需要参数为链表头指针,要删除的元素*/
struct Link *DelNode(struct Link *head,long num)
{
  struct Link *p,*pr;

  if(head==NULL)
  {
    printf("\n No Linkde Table\n");
    return head;
  }
  p=head;

  while(num!=p->data&&p->next!=NULL)
  {
    pr=p;
    p=p->next;
  }
  if(num==p->data)
  {
    if(p==head)
      head=p->next;
    else
      pr->next=p->next;

    free(p);
    printf("delete the node\n");
  }
  else
  {
    printf("Not found the node\n");
  }
  return head;
}


/*这个函数用于查找链表中的元素*/
struct Link *FinNode(struct Link *head,long num)
{
  struct Link *p;
  int i=1;
  if(head==NULL)
  {
    printf(" No Linkde Table\n");
    return head;
  }
  p=head;
  while(num!=p->data&&p->next!=NULL)
  {
    p=p->next;
    i++;
  }
    if(num==p->data)
  {
    printf("it is the %d number\n",i);
  }
    else
    printf("Not found the node\n");

}


/*这个函数用于向链表中插入元素*/
struct Link *InsNode(struct Link *head)
{
  struct Link *p,*pr;
  int i=1,tdata,num;

  p=(struct Link*)malloc(sizeof(struct Link));
  p->next=NULL;
  if(p==NULL)
  {
    printf("Can't enough memory to alloc\n");
    exit(0);
  }


  pr=head;


  printf("please input the number you want to insert:");
  scanf("%d",&tdata);
  p->data=tdata;
  printf("please input the position you want to insert the number:");
  scanf("%d",&num);

    while(i<num&&pr->next!=NULL)
    {
     pr=pr->next;
     i++;
    }
    if(pr==head)
     {
      printf("you have input the wrong position!\n");
     }

     else
     {
       p->next=pr->next;
       pr->next=p;
     }

    return head;
}


/*这个函数用于计算链表的长度*/
void LonNode(struct Link *head)
{
    struct Link *p;
    int i=0;
    p=head;

    while(p->next!=NULL)
    {
        p=p->next;
        i++;
    }
    printf("the length of the link is %d\n",i);

}


/*这个函数用于实现链表的倒转*/
struct Link *ReNode(struct Link *head)
{
    struct Link *p,*q;
    q=(struct Link*)malloc(sizeof(struct Link));
    q->next=NULL;
    p=head->next;
    head->next=q;
    while(p->next!=NULL)
    {
        q=p->next;
        p->next=head;
        head=p;
        p=q;
    }
    free(p);
    return head;
}


 /*下面两个函数用于实现链表的升序*/
struct Link *Fin(struct Link *head)
{
    struct Link *p,*q;
    int n;
    p=head;
    n=p->data;

    for(;p->next!=NULL;p=p->next)
    {
        if(n>p->data)
        n=p->data;
    }
    while(n!=p->data)
    {
        q=p;
        p=p->next;
    }
    q->next=p->next;
    return p;

}
struct Link *Paixu(struct Link *head)
{
    struct Link *q,*p;
    p=Fin(head);
    q=p;
    do{
        q->next=Fin(head);
        q=q->next;
      }while(Fin(head)!=NULL);
    q->next=NULL;
    return p;
}

main()
{
  FILE *fp;
  char op;
  struct Link *head;
  int N;
  while(1)
  {
  printf("input 'D' to delete a node from the link,\n'I'to insert a node to the link,\n'F'to find the node from the link,\n'L'to show the length of the link,\n'R'to reaval the node of the link,\n'C'to creat a node,\n'P'to show the link,\n,'X' to make the link from small one to bigger one.\n'E' to exit \n");
  scanf("%1s",&op);
  switch (op)
  {
       case 'C':
             head=CreatLink();
             break;
      case 'D':
             printf("please input the node you want to delete:");
             scanf("%d",&N);
             head=DelNode(head,N);
             DispLink(head);
             break;
      case 'I':
             head=InsNode(head);
             DispLink(head);
             break;

      case 'F':printf("please input the number you want to find:");
             scanf("%d",&N);
             FinNode(head,N);
             break;
      case 'L':
             LonNode(head);
             break;
      case 'R':
             head=ReNode(head);
             DispLink(head);
             break;

      case 'P':
             DispLink(head);
             break;
      case 'X':
             head=Paixu(head);
             DispLink(head);
             break;
      case 'E':
             exit(0);
      default:
             printf("you have enter the wrong letter!\n");
   }    }
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -