链表学习 代码

来源:互联网 发布:java中文版 编辑:程序博客网 时间:2024/06/01 17:05


#include<stdio.h>
#include<stdlib.h>
struct node{
      int data;
      struct node *next;
};
typedef struct node  list_t;
/*--------------------------
  * 遍历链表,
  * 参数  链表头
  * 注意:表头元素是否有意义
   --------------------------*/
void list_disp(list_t *head)
{
 
   list_t *p=head->next;
  // list_t *p=head;
   while(p!=NULL)
    {
       printf(" %d\n",p->data);
       p=p->next;
    }
}
/*---------------------------
 * 查找链表元素
 * 参数:链表头,查找元素
 * 函数返回值:int 返回查找标志 flag
 * 注意:先遍历 在过程查找
 * --------------------------*/
int list_find(list_t *head,int value)
{
  int flag=1;
  list_t  *p;
  p=head;
  while(p!=NULL)
    {
     if(p->data==value)
      {
        flag=0;
        printf("found : %d\n",p->data);
        break;  //跳出while循环,结束查找
      }
     p=p->next;
    }
  return flag;
}
/*---------------------------------
 *  删除链表中指定的value
 *  函数参数:表头,删除的值
 *  函数返回值:空
 *  注意:建立两个有顺序链表指针pre p.遍历查找删除的元素
 *  (依次移动pre,p).pre->next=p->next
 *  --------------------------------*/
void list_delete(list_t *head,int value)
{
    list_t *p;
    p=head;
    list_t *pre=head;
    while(p!=NULL)
    {
     if(p->data==value)
      {
        printf("delete %d\n",p->data);
        pre->next=p->next;
        break;
      }
     else
      {
        pre=p;
        p=p->next;
      }
    }
}
/*------------------------------------
 * 在链表中插入数据
 * 函数参数:表头,插入值,函数返回值类型:空
 * 注意:新建结点,初始化节点,
 *       开始或表尾,复制节点
 *       表中,有序插入
 * ---------------------------------*/
void list_insert(list_t*head,int value)
{  
    list_t *newnode=(list_t*)malloc(sizeof(list_t)),*p,*pre;
    newnode->data=value;
    newnode->next=NULL;
    p=head->next;
    pre=head;
    while(p!=NULL)
     { 
        if(p->data>newnode->data)
          {
           printf("insert : %d\n",newnode->data);
           newnode->next = p;
           pre->next=newnode;
           break;
          }
        else
          {
          pre=p;
          p=p->next;
         
          }
    
      }
    if(p==NULL)
    {
      pre->next=newnode;

    }
   
}
/*-------------------------------
 *从尾部查找第k个结点
 *函数返回值为 int 函数参数:表头,位置k
 *注意:新建链表指针*p,*q
 *          0~k-1     k-NULL
 *     p     0           1
 *     q     1           1
 *                步数
 * -----------------------------*/
int  search_k_form_tail(list_t *head,int k)
{
  list_t *p=head,*q=head;
  int i=0;
  while(p!=NULL)
  {
    if(i<k)
    {
     p= p->next;
     i++;
    }
  else
    {
     p=p->next;
     q=q->next;
    }
  }
   return q->data;
}
/*-------------------------
 *查找链表中间结点
 *函数参数 表头,函数返回值类型int
 *注意:遍历链表,计算总数
 *      通过次数控制移动
 * ------------------------*/
int  list_find_mid(list_t *head)
{  int i=0;
   list_t *p=head;
   while(p!=NULL)
   {
      i++;
      p=p->next;
   }
   p=head;
   for(i=i/2;i>0;i--)  
       {
          p=p->next;
       }
   return p->data;
  
}
/*
 新建节点,初始化节点,明确指向
 函数返回值类型 list_t *   堆中分配内存
list_t *list_insert_nohead(list_t *head,int value)
{
   list_t *n1=(list_t*)malloc(sizeof(list_t));
   list_t *n2=(list_t*)malloc(sizeof(list_t));
   n1->data=5;
   n2->data=1;
   head->next=n1;
   n1->next=NULL;
   n2->next=head;
   head=n2;
   return head;
}
*/
int main(int argc ,char *argv[])
{   
    int k_value,k_m_value;
    list_t * head = (list_t*)malloc(sizeof(list_t));
    head->next=NULL;
  
    list_insert(head,1);
    list_insert(head,2);
    list_insert(head,3);
    list_insert(head,4);
    list_insert(head,10);
    list_insert(head,6);
    list_insert(head,7);
    list_insert(head,8);
    list_insert(head,9);
    list_disp(head);
   
    k_m_value=list_find_mid(head);
    printf("k_m_value=%d\n",k_m_value);
    k_value=search_k_form_tail(head,4);
    printf("k_value=%d\n",k_value);
    list_delete(head,2);
    list_disp(head);
    list_find(head,5);
    list_insert(head,2);
    list_disp(head);
   /*有头
   list_t *head=(list_t *)malloc(sizeof(list_t));
   head->next=NULL;
   head->data=3;
   head=list_insert_nohead(head,0);
   list_disp(head);
  */
   return 0;
}



0 0
原创粉丝点击