今天主要学习的是 链表,单链,循环链表和双向链表,总体感觉还可以,还是有点懵懵的,需要勤加练习。

此程序主要是实现删,查,插,其中分为按位置和按值域两种方式,还有其他的方式。


以下程序可能细节不是太完善。


#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_index(struct Node* head,type value,int index);
int insert_head(struct Node* head,type value);
int print(struct Node* head);


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 newvalue);
int update_value(struct Node* head,type oldvalue,type newvalue);
int query_index(struct Node* head,int index);
int query_value(struct Node* head,type value);




int length(struct Node* head);


main()
{
   struct Node* head;
     
    init(&head);
    int i;
    for(i = 0;i < 10; i++)
    {
       insert_tail(head,i);
    }
    print(head);  
    for(i = 0;i < 10; i++)
    {
       insert_head(head,i);
    }
    print(head);  
    
    insert_index(head,100,0);
    insert_index(head,100,length(head));
    insert_index(head,100,11);
    print(head);  


    delete_index(head,3);
    delete_value(head,100);
    delete_value(head,0);
    print(head);
    printf("length=%d\n",length(head));


    update_index(head,5,89);
    update_value(head,6,66);
    print(head);


    query_index(head,10);
    query_value(head,66);
    print(head);


    return 0;
}


int insert_index(struct Node* head,type value,int index)
{
    struct Node* newnode = (struct Node* )malloc(sizeof(struct Node));
    if(NULL == newnode)
    {
       printf("error");
       return F;
    }
    if(index < 0 || index >length(head))
    {
       printf("out of range\n");
       return F;
    }
    int i;
    for(i = 0;i < index;i++)
    {
       head = head->next;
    }
    newnode->value = value;
    newnode->next = 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(NULL == newnode)
    {
       printf("error");
       return F;
    }
    newnode->value = value;
    newnode->next = head->next;
    head->next = newnode;
    return T;
}


int init(struct Node** head)
{
    struct Node* newnode = (struct Node* )malloc(sizeof(struct Node));
    if(NULL == newnode)
    {
       printf("error");
       return F;
    }
    newnode->value = 0 ;
    newnode->next = newnode;
    *head = newnode;
    return T;
}


int insert_tail(struct Node* head,type value)
{
   struct Node* temp = head;
   struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
   if(NULL == newnode)
   {
      return F;
   }
   while(head->next != temp)
   {
      head = head->next;
   }
   newnode->value = value;
   newnode->next = temp;
   head->next = newnode;
   return T;


}


int delete_index(struct Node* head,int index)
{
    if(index < 0 || index > length(head))
    {
        printf("error");
return F;
    }
    int i;
    for(i = 0;i < index ;i++)
    {
        head = head->next;
    }
    struct Node* temp = head->next;
    head->next = head->next->next;
    free(temp);
    return T;
}
int delete_value(struct Node* head,type value)
{
   int i ;
   for(i = 0;i < length(head);i++)
   {
       if(head->next->value == value)
{
 struct Node* temp2 = head->next;
 head->next = head->next->next;
 free(temp2);
}

           head = head->next;
    }
}


int update_index(struct Node* head,int index,type newvalue)
{
    if(index < 0 || index > length(head))
    {
       return F;
    }
    int i;
    for(i = 0; i <= index;i++ )
    {
       head = head->next;
    }
    head->value = newvalue;
}
int update_value(struct Node* head,type oldvalue,type newvalue)
{
    int i;
    for(i = 0;i < length(head);i++)
    {
        if(head->next->value == oldvalue)
{
   head->next->value = newvalue;
}
head = head->next;
    }
}
int query_index(struct Node* head,int index)
{
    if(index < 0 || index > length(head))
    {
       return F;
    }
    int i;
    for(i = 0; i <= index;i++ )
    {
       head = head->next;
    }
    printf("the value is=%d\n",head->value);
}
int query_value(struct Node* head,type value)
{


    int i;
    for(i = 0;i < length(head);i++)
    {
        if(head->next->value ==  value)
{
   printf("the index is=%d",i);
}
    }
}


int length(struct Node* head)
{
    int count=0;
    struct Node* temp = head;
    while(head->next != temp)
    {
    count++;
    head = head->next;
    }
    return count;
}
print(struct Node* head)
{
  struct Node* temp = head;
  while(temp->next!=head)
  {
     printf("%d  ",temp->next->value);
     temp = temp->next;
   }
   printf("\n");
  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值