单链表 建立, 查找, 删除, 插入 操作

来源:互联网 发布:c语言double怎么用 编辑:程序博客网 时间:2024/06/05 15:55

完善一下单链表操作.


linklist.c


#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define     DEBUG_PRT(fmt,arg...)    printf(fmt,##arg)

typedef struct linklist
{
    int value;
    struct linklist *next;
}Mylist_s;

Mylist_s *create_list(int number)
{
    Mylist_s *head = NULL;
    Mylist_s *p = NULL;
    Mylist_s *s= NULL;
    int i = 0;
    
    if(NULL == (head = (Mylist_s *)malloc(sizeof(Mylist_s))))
    {
        DEBUG_PRT("malloc error \n");
        exit(1);
    }
    head->value = -1;
    head->next = NULL;
    
    p = head;
    for(i=0; i<number; i++)
    {
        if(NULL == (s = (Mylist_s *)malloc(sizeof(Mylist_s))))
        {
            DEBUG_PRT("malloc error \n");
            exit(1);
        }
        s->value = i;
        s->next = NULL;
        p->next = s;
        p = s;
    }

    return head;
}

void print_list(Mylist_s *head)
{
    Mylist_s *p = NULL;
    p = head->next;
    while(p != NULL)
    {
        DEBUG_PRT("%d ", p->value);
        p = p->next;
    }
    DEBUG_PRT("\n");
}

/*
* search s, return s
*   s -> r
*/
Mylist_s *search_list(Mylist_s *head, int value)
{
    Mylist_s *p = NULL;
    p = head->next;
    while(p != NULL)
    {
        if(value == p->value)
        {
            return p;
        }
        p = p->next;
    }

    return NULL;
}

/*
* search s, return t
*   t -> s -> r
*/
Mylist_s *search_pre_node(Mylist_s *head, int value)
{
    Mylist_s *p = NULL;
    Mylist_s *pre = NULL;
    p = head->next;
    pre = p;

    while(p != NULL)
    {
        if(value == p->value)
        {
            return pre;
        }
        pre = p;
        p = p->next;
    }

    return NULL;
}

/*
* del s
*   t -> s -> r
* =>t -> r
*/
void del_node(Mylist_s *t)
{
    Mylist_s *p = NULL;
    p = t->next;
    t->next = p->next;
    free(p);
}

/*
* insert s follow t
*   t -> r
* =>t -> s -> r
*/
void insert_node(Mylist_s *s, Mylist_s *t)
{
    s->next = t->next;
    t->next = s;
}

void main(void)
{
    Mylist_s *head = NULL;
    Mylist_s *pre = NULL;
    Mylist_s *new_node = NULL;
    
    head = create_list(10);
    DEBUG_PRT("Init list: \n");
    print_list(head);

    if(NULL == search_list(head, 7))
    {
        DEBUG_PRT("no such value \n");
        exit(1);
    }
    DEBUG_PRT("search_list() ok \n");
    
    if(NULL == (pre = search_pre_node(head, 7)))
    {
        DEBUG_PRT("no such value \n");     
        exit(1);
    }
    DEBUG_PRT("search_pre_node() ok \n");
    del_node(pre);
    DEBUG_PRT("After del 7, list: \n");
    print_list(head);
    
    if(NULL == (new_node = (Mylist_s *)malloc(sizeof(Mylist_s))))
    {
        DEBUG_PRT("can not malloc new_node \n");
        exit(1);
    }
    new_node->value = 100;
    new_node->next = NULL;
    insert_node(new_node, pre);
    DEBUG_PRT("After insert 100 follow 6, list: \n");
    print_list(head);
}


Makefile:


 BIN = /usr/bin/
 GCC = $(BIN)gcc
 CFLAG =
 INC = -I.
 LIB = -L.
 SRC = linklist.c
 TAG = linklist
 RM = /bin/rm
 CP = /bin/cp
 
 all:
     $(GCC) $(CFLAG) $(INC) $(LIB) $(SRC) -o $(TAG)
 clean:
     $(RM) -rf *.o $(TAG)


运行结果:

./linklist
Init list:
0 1 2 3 4 5 6 7 8 9
search_list() ok
search_pre_node() ok
After del 7, list:
0 1 2 3 4 5 6 8 9
After insert 100 follow 6, list:
0 1 2 3 4 5 6 100 8 9


原创粉丝点击