数据结构2_160802有头单向不循环

来源:互联网 发布:pscs3软件百度云 编辑:程序博客网 时间:2024/05/22 00:11

因为顺序表的插入和删除要操作大面积的数据,很不方便。链式存储就没这个问题了

链式结点有数据域和指针域。指针指向

链表分为有头的单链表和无头的单链表


单链表:单链表的问题是只能一个方向走

有头的:第一个结点是头结点,头结点后面跟数据结点。有头可以简化操作

无头的:第一个结点就是数据结点。头插的时候有所不同(无头的要用二维指针)

我写的代码:

lis.c

#include"list.h"
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
list *list_creat()

    list *me;
    me = malloc(sizeof(*me));
    if (me == NULL)
    {  
        printf("malloc failed\n");
        return NULL;
    }
    me->next = NULL;
    return me;

}

void list_display(list *me)
{

   
    list *node = me->next;
    if (list_isempty(me)==0)
        return;
    while(node)
    {
        printf("%d\t",node->data);
        node = node->next;
       
    }
    putchar('\n');

    return ;
}
void list_destroy(list *me)
{  

    list * save = NULL;


    while(me)
    {
        save = me->next;
        free(me);
        me = save;
       
    }


    return;

}

              
int list_insert_at(list *me, int i, datatype *data)
{
   

    int j = 0;
    list * node = me;
    list *newnode = NULL;
   
    if (i<0 )
        return -1;

    while(j < i && node!=NULL)
    {
        node = node ->next;
        ++j;
    }
   if (node!= NULL)
    {
       newnode = malloc(sizeof(*newnode));
       if(newnode == NULL)
            return -2;
       newnode->data = *data;
       newnode->next = node->next;
       node->next = newnode;
       return 0;

    }
   else
        return -3;

}
             
int list_order_insert(list *me, datatype *data)
{
   

    list *new = NULL;
    list *p = me;
    new = malloc(sizeof(*new));
    if( NULL == new)
        printf("malloc failed...\n");
    while(p->next && *data > p->next->data)
        p = p->next;

       
    new->data = *data;
    new->next = p->next;
    p->next = new;
    return 0;
}       

                
int list_delete_at(list *me, int i, datatype * data)
{
    list *p = me;
    list *q = NULL;
    int j = 0;
    while (p!= NULL && j<i)
    {
        p = p-> next;
        j ++;
    }
    if (p == NULL)
        return -1;
    else
    {
        q = p->next;
        p->next = q->next;
        *data = q->data;
        free(q);
    }
}
           
int list_delete(list *me, datatype *data)
{
    list *p = me;
    list *q = NULL;
    while( p!= NULL && p->next->data != *data)
        p = p->next;
    if(NULL == p)
    {
        printf("the data is not in the list\n");
        return -1;
    }
    else
        q = p ->next;
        p->next = q->next;
        free(q);
    return 0;
}
                      
int list_isempty(list *me)
{  
    if(me->next == NULL)
        return 0;
    else
        return -1;


}


main.c


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"list.h"

int main()
{
    list *l;
    l = list_creat();

    datatype arr[] = {12, 23, 34,5, 45, 56};
    int value = 34;//the data to  delete
   
    int i;
    for(i = 0;i<6;i++)
    {
        list_order_insert(l,&arr[i]);
        //list_insert_at(l,0,&arr[i]);
    }
    list_display(l);
   // list_delete(l, &value);
    list_delete_at(l, 3, &value);
    printf("the number is %d\n",value);
    list_display(l);
    list_destroy(l);
return 0;
   

}



0 0
原创粉丝点击