建立单链表的2种实现方法-->单链表操作

#include <stdio.h>

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

int createListFromHead(struct myList **list, int n)
{
    struct myList *p = NULL;  
    int i = 0;
    *list = (struct myList *)malloc(sizeof(struct myList));
    if(NULL == *list)
    {
        printf("Can not malloc header memory. \n");
        return -1;
    }
    (*list)->next = NULL;
    for (i=0;i<n;i++)  
    {
        p = (struct myList *)malloc(sizeof(struct myList));
        if(NULL == p)
        {
            printf("Can not malloc Node %d memory. \n", i);
            return -1;
        }
        p->data = i;
        p->next = (*list)->next;
        (*list)->next = p;
    }
    return 0;
}

int createListFromTail(struct myList **list, int n)  
{
    struct myList *p = NULL;
    struct myList *r = NULL;
    int i = 0;
    *list = (struct myList *)malloc(sizeof(struct myList));
    if(NULL == *list)
    {
        printf("Can not malloc header memory. \n");
        return -1;
    }
    r = *list;
    for (i=0;i<n;i++)
    {
         p = (struct myList *)malloc(sizeof(struct myList));
         if(NULL == p)
         {
            printf("Can not malloc Node %d memory. \n", i);
            return -1;
         }
         p->data = i;
         r->next = p;
         r = p;
    }
    r->next = NULL;
    return 0;
}

void printList(struct myList *list)
{
    struct myList *p = NULL;
    p = list;
    while(p != NULL)
    {
        printf("p->data = %d \n", p->data);
        p = p->next;
    }
}

int main(int argc, char **argv)
{   
    struct myList *testList;
    int tag = 0;
    int number = 0;
    
    if(3 != argc)
    {
        printf("Run: ./listTest 1(or 2) listNumber \n");
        exit(1);
    }
    tag = atoi(argv[1]);
    number = atoi(argv[2]);

    if (1 == tag)
    {
        createListFromHead(&testList, number);
    }
    else
    {
        createListFromTail(&testList, number);
    }
    
    printList(testList);
    
    return 0;
}


运行结果:


./listTest 1 5
p->data = 0
p->data = 4
p->data = 3
p->data = 2
p->data = 1
p->data = 0
./listTest 2 5
p->data = 0
p->data = 0
p->data = 1
p->data = 2
p->data = 3
p->data = 4



今天再补充几个函数,要开工干活了,没时间debug,先贴上来.


#include <stdio.h>

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

int createListFromHead(struct myList **list, int n)
{
    struct myList *p = NULL;  
    int i = 0;
    *list = (struct myList *)malloc(sizeof(struct myList));
    if(NULL == *list)
    {
        printf("Can not malloc header memory. \n");
        return -1;
    }
    (*list)->next = NULL;
    for (i=0;i<n;i++)  
    {
        p = (struct myList *)malloc(sizeof(struct myList));
        if(NULL == p)
        {
            printf("Can not malloc Node %d memory. \n", i);
            return -1;
        }
        p->data = i;
        p->next = (*list)->next;
        (*list)->next = p;
    }
    return 0;
}

int createListFromTail(struct myList **list, int n)  
{
    struct myList *p = NULL;
    struct myList *r = NULL;
    int i = 0;
    *list = (struct myList *)malloc(sizeof(struct myList));
    if(NULL == *list)
    {
        printf("Can not malloc header memory. \n");
        return -1;
    }
    r = *list;
    for (i=0;i<n;i++)
    {
         p = (struct myList *)malloc(sizeof(struct myList));
         if(NULL == p)
         {
            printf("Can not malloc Node %d memory. \n", i);
            return -1;
         }
         p->data = i;
         r->next = p;
         r = p;
    }
    r->next = NULL;
    return 0;
}

void printList(struct myList *list)
{
    struct myList *p = NULL;
    p = list;
    while(p != NULL)
    {
        printf("p->data = %d \n", p->data);
        p = p->next;
    }
}

/*
* Insert node before index
*/
void insertNode(struct myList *list, int index, struct myList node)
{
    struct myList *p = NULL;
    
    ASSERT(NULL != list);

    p = list;
    int count = 0;
    
    //node becomes header
    if(index <= 0)
    {
        node.next = *p;
        list = node;
        return;
    }

    while(NULL != p)
    {
        p = p->next;
        count++;
    }
    
    //add to the tail
    if(index > count)
    {
        p->next = node;
        node.next = NULL;
        return;
    }
    else
    {
        p = list;
        count = 0;
        while(NULL != p)
        {
            p = p->next;
            count++;
            if(count = (index - 1))
            {
                break;
            }
        }
        node.next = p->next->next;
        p->next = node;
        return;
    }
    return;
}

int findNode(struct myList *list, int value)
{
    struct myList *p = NULL;
    int index = 0;

    ASSERT(NULL != list);
    
    p = list;
    while(NULL != p)
    {
        index++;
        if(p->data == value)
        {
            return index;
        }
    }
    return -1;
}

int delNode(struct myList *list, int index)
{
    struct myList *p = NULL;
    struct myList *q = NULL;
    int count = 0;
    int count_1 = 0;

    ASSERT(NULL != list);

    p = list;
    if(0 == index)
    {
        list = p->next;
        free(p);
        return 0;
    }

    while(NULL != p)
    {
        count++;
        p = p->next;
    }
    count_1 = count;
    if(index > count)
    {
        p = list;
        count = 0;
        while(NULL != p)
        {
            count++;
            p = p->next;
            if(count == (count_1 - 1))
            {
                break;
            }
        }
        free(p->next);
        p->next = NULL;
        return 0;
    }
    else
    {
        p = list;
        count = 0;
        while(NULL != p)
        {
            count++;
            p = p->next;
            if(count == (index - 1))
            {
                break;
            }
        }
        q = p->next;
        p->next = q->next;
        free(q);
        return 0;
    }
    
    return 0;
}

int main(int argc, char **argv)
{   
    struct myList *testList;
    int tag = 0;
    int number = 0;
    int action = 0;
    
    if((3 != argc) || (4 != argc))
    {
        printf("Run: ./listTest 1/2/ listNumber (3/4/5) \n");
        printf("1: createListFromHead \n");
        printf("2: createListFromTail \n");
        printf("3: insert new node before index node \n");
        printf("4: find node in index \n");
        printf("5: delete node in index \n");
        exit(1);
    }
    tag = atoi(argv[1]);
    number = atoi(argv[2]);
    action = atoi(argv[3]);

    if (1 == tag)
    {
        createListFromHead(&testList, number);
    }
    else if(2 == tag)
    {
        createListFromTail(&testList, number);
    }
    
    printList(testList);
    
    if(3 == action)
    {
    
    }
    else if(4 == action)
    {
    
    }
    else if(5 == action)
    {
        
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值