算法不了解,在IT这个圈子不好混啊!
有空还是巩固下基础吧。
今天从单链表做起。
#include <stdio.h>
#include <stdlib.h>

typedef struct Student
{
  int data;
  struct Student* next;
}node;

node* CreateLink(node* head)
{
  node* p = NULL;
  node* s = NULL;
  head = p;
  int cycle = 1;
  int x = 0;

  while(cycle != 0)
  {
    scanf_s("%d", &x);
    if(x != 0)
    {
      s = (node*)malloc(sizeof(node));
      s->data = x;
      if(NULL == head)
      {
        p = s;
        head = p;
      }
      else
      {
        p->next = s;
        p = s;
      }
    }
    else
    {
      cycle = 0;
    }
  }
  p->next = NULL;
  return head;
}

int LenLink(node* const head)
{
  node* p = head;
  int n = 0;

  if(p == NULL)
  {
    printf("This is a empty link.\n");
    return 0;
  }
  while(p->next != NULL)
  {
    n++;
    p = p->next;
  }
  return n+1;
}

void Print(node* const head)
{
  node* p = head;
  if(p == NULL)
  {
    printf("This is a empty link.\n");
    return;
  }
  while(p->next != NULL)
  {
    printf("%d\n", p->data);
    p = p->next;
  }
  printf("%d\n", p->data);
}
node*    DelNode(node* head, int var)
{
  node* p = head;
  node* s = p;
  if(NULL == head)
  {
    printf("This is a empty link.\n");
    return head = NULL;
  }
  else
  {
    while(p->next != NULL)
    {
      if(p->data == var)
      {
        if(p == head)
        {
          head = p->next;
          free(p);
          p = head;
          s = p;
        }
        else
        {
          s->next = p->next;
          free(p);
          p = s->next;
        }
      }
      else
      {
        s = p;
        p = p->next;
      }
    }
    if(p == head && p->data == var)
    {
      free(p);
      return head = NULL;
    }
    else
    {
      if(p->data == var)
      {
        s->next = NULL;
        free(p);
        p = NULL;
      }
    }
  }
  return head;
}

node* InsNode(node* head, int i)
{
  node* p1 = head;
  node* p2 = head;
  node* s = (node*)malloc(sizeof(node));
  s->data = i;

  if(NULL == head)
  {
    printf("This is a empty link.\n");
    head = s;
    return head;
  }
  else
  {
    while(s->data > p1->data && p1 != NULL)
    {
      p2 = p1;
      p1 = p1->next;
    }
    if(s->data <= p1->data)
    {
      if(head == p1)
      {
        head = s;
        s->next = p1;
      }
      else
      {
        p2->next = s;
        s->next = p1;
      }
    }
    else
    {
      p1->next = s;
      s->next = NULL;
    }
  }
  return head;
}

node* RevLink(node* head)
{
  node* p1 = head;
  node* p2 = head;
  node* p3 = head;

  if(NULL == head || NULL == head->next)
  {
    return head;
  }
  else
  {
    p2 = p1->next;
    while(p2)
    {
      p3 = p2->next;
      p2->next = p1;
      p1 = p2;
      p2 = p3;
    }
    head->next = NULL;
    head = p1;
    return head;
  }
}

void Release(node* head)
{
  node* p = head;

  if(NULL == head)
  {
    printf("This is a empty link.\n");
    return;
  }

  while(p->next != NULL)
  {
    head = p->next;
    free(p);
    p = head;
  }
  free(head);
}

int main(int argc, char* argv[])
{
  node* head = NULL;
  //创建单链表
  head = CreateLink(head);
  //打印链表长度
  printf("Length of single link: %d\n", LenLink(head));
  //打印链表
  Print(head);
  head = DelNode(head, 5);
  //打印链表长度
  printf("Length of single link after delete node: %d\n", LenLink(head));
  //打印链表
  Print(head);
  head = InsNode(head, 5);
  //打印链表长度
  printf("Length of single link after insert node: %d\n", LenLink(head));
  //打印链表
  Print(head);
  head = RevLink(head);
  //打印链表长度
  printf("Length of single link after reverse: %d\n", LenLink(head));
  //打印链表
  Print(head);
  //释放链表堆空间
  Release(head);
  return 0;
}