链表 添加链表、搜索链表、删除链表

#include 
#include 
/* 利用可变数组的思想:Linked Block 当中不用copy value */
/* 可变数组的缺陷:1.新申请空间,然后往新空间赋值数据花费时间;2.数组增长时,可能遇见内存限制 */
/* 单向链表思想:指针->头节点(数据+指针)->节点(数据+指针)->节点(数据+指针)->尾节点(数据+指针)->NULL */
typedef struct _node
{
  int value;
  struct _node *next;//结构体指针,指向该类型的结构体
}Node;

typedef struct _list{
  Node *head;
  // Node *tail;
} List;

void add(List* pList, int number);
void print(List *pList);

int main(){
  int number;
  /* Node *head = NULL;//定义一个结构体指针,指向该结构体,头节点 */
  List list;
  list.head = NULL;
  printf("链表头节点地址为%X\n", list.head);
  do{
    printf("请用户向链表中输入数字\n");
    scanf("%d", &number);
    printf("用户当前向链表中输入数字为:%d\n", number);
    printf("\n");
    if(number!=-1){
      /* start:制造新节点 */
      /* Node *p = (Nope *)malloc(sizeof(Node));//申请一个NODE类型的结构体空间,中间节点
      p->value = number;//将新读入的数据存入新建的空间中
      p->next = NULL;
      Node *last = head;//尾节点
      if(last){//判断last是否为NULL
        while ( last->next ){
          last = last->next;
        }
        last->next = p;
      }else{
        heap = p;//让头节点等于P
      } */
      /* end:制造新节点 */
      add(&list, number);
    }
  } while (number != -1);

  /* start:链表的搜索 */
  /* Node *p;
  for (p = list.head; p;p=p->next){//遍历链表
    printf("%d", p->value);
  }
  printf("\n"); */
  /* end:链表的搜索 */
  print(&list);

  /* start:用户输入数字,链表找到它并且删除它 */
  printf("请用户输入要查找并删除的数字:\n");
  scanf("%d", &number);
  printf("用户输入要查找并删除的数字为%d\n",number);
  Node *p;
  int isFound = 1;

  /* 链表找到数字 */
  /* for (p = list.headhead; p ;p=p->next){//遍历链表
    if(p->value == number){
      printf("找到了\n");
      isFound = 0;
      break;
    }
  }
  if(isFound){
    printf("没找到\n");
  } */

/* 链表删除数字 */
  Node *q;
  for (q = NULL,p = list.head; p ;q=p,p=p->next){//遍历链表,Q指向的是P前面的那个节点
    if(p->value == number){
      printf("找到了用户输入的数字%d\n", number);
      if(q){//箭头左边的指针必须要检查,以防边界条件
        q->next = p->next;
        printf("交换节点NEXT指针指向\n");
      }
      else {
        list.head = p->next;
        printf("您输入的数字为第一个节点,更改头节点\n");
      }
      printf("开始释放链表地址为%X的单节点内存\n",p);
      free(p);
      printf("删除成功!\n");
      break;
    }
    else{
      isFound = 0;
    }
  }
  if(!isFound)
    printf("链表中没有你要找的数字!\n");
  /* end:用户输入数字,链表找到它并且删除它 */

  /* start:链表的清除 */
  for (p = list.head; p; p=q){
    q = p->next;
    free(p);
  }
  printf("释放链表全部内存\n");
  /* end:链表的清除 */
    return 0;
}

/* 新节点转换为函数 */
void add(List *pList ,int number){//因为函数只能传递值和地址,所以如果不传指针,head的地址传不出去
  Node *p = (Node *)malloc(sizeof(Node));
  p->value = number;
  printf("链表节点中的数字为%d\n", p->value);
  p->next = NULL;
  Node *last = pList->head;
  printf("链表尾节点地址为%X\n",pList->head);
  if(last){
    while ( last->next ){
      last = last->next;
    }
    last->next = p;
    printf("链表当前尾节点地址为%X\n", last->next);
  }
  else{
    pList->head = p; //*pHead 的值为p的地址;
    printf("链表当前头节点指向地址为%X\n", pList->head);
  }
  printf("\n");
}
/* 链表的搜索转变为函数 */
void print(List *pList){
  Node *p;
  printf("开始遍历链表并输出值:\n");
  for (p = pList->head; p;p=p->next){//遍历链表
    printf("%d ", p->value);
  }
  printf("\n");
}

你可能感兴趣的:(C语言编程)