您的位置:首页 > 其它

这个是今天上课自己编写的链表的程序。

2017-05-20 22:33 447 查看
#include<stdio.h>

#include<stdlib.h>

#define T 1

#define F -1

typedef int type;

struct Node

{

  type value;

  struct Node* next;

};

int init(struct Node** head);

int insert_tail(struct Node* head,type value); 

int insert_head(struct Node* head,type value);

int insert_index(struct Node* head,type value,int index);

int delete_index(struct Node* head,int index);

int delete_value(struct Node* head,type value); 

int update_index(struct Node* head,int index,type value);

int update_value(struct Node* head,type ole_value, type new_value);

int query_index(struct Node* head,int index);

int query_value(struct Node* head,type value);

int length(struct Node*head);

void print(struct Node* head);

main()

{

    int ret;

    int i;

    struct Node* head;

    ret = init(&head);

    if(F == ret)

    {

        return -1;

   }

    for(i = 0;i < 10;i++)

    {

     insert_tail(head, i);

    }

    for(i = 0;i < 10;i++)

    {

       insert_head(head, i);

    }

       print(head);

       printf("\n");

       printf("length = %d  \n",length(head));

       delete_index(head,1);

       delete_index(head,length(head)-2);

       delete_index(head,5);

       print(head);

       printf("\n");

       insert_index(head,99,0);

       insert_index(head,99,7);

       insert_index(head,99,length(head)-1);

       print(head);

       printf("\n");

       delete_value(head,99);

       print(head);

       printf("\n");

       update_index(head,8,-1);

       print(head);

       printf("\n");

       update_value(head,8,100);

       print(head);

       printf("\n");

       printf("the value is %d",query_index(head,16));

       printf("\n");

       query_value(head,0);

       printf("\n");

    return 0;

}

int init(struct Node** head)

{

    struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));

    if(newnode == NULL)

    {

        return F;

    }

    newnode->value = 0;

    newnode->next = NULL;

    (*head) = newnode;

    return T;

}

int insert_tail(struct Node* head,type value) 

{

     struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));

     if(NULL == newnode)

     {

        return F;

     }

     newnode->value = value;

     newnode->next = NULL;

     while(head->next!= NULL)

     {

         head = head->next;

     }

     head->next = newnode;

     return T;

}

int insert_head(struct Node* head,type value)

{

   struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));

   if(newnode == NULL)

   {

      return F;

   }

   newnode->value = value;

   newnode->next = head->next;

   head->next = newnode;

   return T;

}

int insert_index(struct Node* head,type value,int index)

{

   struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));

   if(NULL == newnode)

   {

      return F;

   }

   int i;

   if(index < 0 || index > length(head))

   {

      printf("out of range");

      return F;

   }

   for(i = 0;i < index;i++)

   {

       head = head->next;

   }

   newnode->value = value;

   newnode->next = head->next;

   head->next = newnode;

   return T;

}

int delete_index(struct Node* head,int index)

{

    int i;

    if(index < 0 || index >= length(head))

    {

       printf("out of r
a5df
ange\n");

       return F;

    }

    for(i = 0;i < index;i++)

        head = head->next;

    

    struct Node* temp = head->next->next;

    free(head->next);

    head->next = temp;

    return T;

}

int delete_value(struct Node* head,type value)

{

    int index ,i;

    int len;

    len = length(head);

     for(index = 0 ;index < len-1;index++)

     {  

        if(head->next->value == value)

        {

           struct Node* temp = head->next->next;

           free(head->next);

           head->next = temp;
}
else
{
head = head->next;
}
}

     return T;

}

int update_index(struct Node* head,int index,type value)

{

    if(index < 0 || index > length(head))

    {

       printf("out of the range");

    }

    int i;

    for(i = 0;i <= index;i++)

    {

        head = head->next;

    }

    head->value = value;

    return T;

}

int update_value(struct Node* head,type old_value, type new_value)

{

   int count=0,i;

   int len;

   len = length(head);

   for(i = 0;i < len;i++)

   {

       if(head->value == old_value)

       {

          count++;

          head->value = new_value;

       }

          head = head->next;

       

    }

    if(0 == count)

    {

        printf("no number\n");

    }

    return T;

}

int query_index(struct Node* head,int index)

{

    int i;

    for(i = 0;i <= index; i++)

    {

        head = head->next;

    }

    return head->value;

}

int query_value(struct Node* head,type value)

{

    int i,count=0;

    int len;

     len = length(head);

    for(i = 0;i < len-1; i++)

    {

        if(head->next->value == value)

         { 
   printf("there are in the %d\n",i);
}
head = head->next;

    }

}

int length(struct Node* head)

{

    int count = 0 ;

    while(head!= NULL)

    {

       count++;

       head = head->next;

    }

    return count;

}

void print(struct Node* head)

{

    while(head->next!=NULL)

    {

        printf("%d  ",head->next->value);
head = head->next;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: