一个简单的带头尾指针单向链表(C实现)

类别:编程语言 点击:0 评论:0 推荐:

        用C写了一个带头尾指针的单向链表,仅在尾部进行插入操作,在任意位置进行删除操作。因为只用到这么些功能,又因为懒,所以没有扩展。因为插入是固定在尾部进行,带一个尾指针的好处是显而易见的。当然删除时要付出一些开销。

list.h

-------------------------------------------

/* list.h
** Copyright 2004 Coon Xu.
** Author: Coon Xu
** Date: 06 Sep 2004
*/

#ifndef LIST_H
#define LIST_H

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

struct listnode
{
    struct listnode* next;
    int data;
};

struct list
{
    struct listnode* head;
    struct listnode* tail;
    int count;
};

void list_init(struct list*);
void list_insert(struct list*, struct listnode*);
int list_delete(struct list*, struct listnode*);

#endif

------------------------------------------

list.c

------------------------------------------

/* list.c
** Copyright 2004 Coon Xu.
** Author: Coon Xu
** Date: 06 Sep 2004
*/
#include "list.h"

void list_init(struct list* myroot)
{
    myroot->count = 0;
    myroot->head = NULL;
    myroot->tail = NULL;
}

void list_insert(struct list* myroot, struct listnode* mylistnode)
{
    myroot->count++;
   
    mylistnode->next = NULL;
    if(myroot->head == NULL)
    {
        myroot->head = mylistnode;
        myroot->tail = mylistnode;
    }
    else
    {
        myroot->tail->next = mylistnode;
        myroot->tail = mylistnode;
    }   
}

int list_delete(struct list* myroot, struct listnode* mylistnode)
{
    struct listnode* p_listnode = myroot->head;
    struct listnode* pre_listnode;
   
    //myroot is empty
    if(p_listnode == NULL)
    {
        return 0;
    }
   
    if(p_listnode == mylistnode)
    {
        myroot->count--;
        //myroot has only one node
        if(myroot->tail == mylistnode)
        {
            myroot->head = NULL;
            myroot->tail = NULL;
        }
        else
        {
            myroot->head = p_listnode->next;
        }
        return 1;
    }
   
    while(p_listnode != mylistnode && p_listnode->next != NULL)
    {
        pre_listnode = p_listnode;
        p_listnode = p_listnode->next;
    }
    if(p_listnode == mylistnode)
    {
        pre_listnode->next = p_listnode->next;
        myroot->count--;
        return 1;
    }
    else
    {
        return 0;
    }
   
}

-------------------------------------------

main.c

-------------------------------------------

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

int main(int argc, char *argv[])
{
    struct list l;
    list_init(&l);
    int ix = 0;
   
    for(ix = 0; ix < 10; ix++)
    {
        struct listnode* p_node = malloc(sizeof(struct listnode));
        p_node->data = ix;
        list_insert(&l, p_node);
    }
   
    struct listnode* p_listnode = l.head;
    while(p_listnode != NULL)
    {
        printf("%d\n", p_listnode->data);
        p_listnode = p_listnode->next;
    }
   
    int input;
    printf("input the number you want delete:\n");
    scanf("%d", &input);
   
    while(input > 0)
    {
        p_listnode = l.head;
        while(p_listnode->next != NULL && p_listnode->data != input)
        {
            p_listnode = p_listnode->next;
        }
       
        if(p_listnode->data == input)
        {
            printf("Delete success!!\nprint the list!!\n");
            list_delete(&l, p_listnode);
            free(p_listnode);
            p_listnode = l.head;
            while(p_listnode != NULL)
            {
                printf("%d\n", p_listnode->data);
                p_listnode = p_listnode->next;
            }       
        }
        else
        {
            printf("not find %d\n", input);
        }  
        scanf("%d", &input);
    } 
  system("PAUSE"); 
  return 0;
}

本文地址:http://com.8s8s.com/it/it27252.htm