链表——新建链表

2020-09-07 11:07:49 浏览数 (1)

代码语言:javascript复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
    int data;
    struct node *pNext;
};

//新建node
struct node * create_node(int data)
{
    struct node *p = (struct node *) malloc (sizeof(struct node));
	
    if(NULL == p)
    {
        printf("malloc error!.n");
        return NULL;
    }
    memset(p, 0, sizeof(struct node));
    p->data = data;
    p->pNext = NULL;

    return p;
}

void insert_tail(struct node *pH, struct node *newp)
{
    struct node * p = pH;
    while(NULL != p->pNext)
    {
        p = p->pNext;
    }
    p->pNext = newp;
}

void insert_head(struct node * pH, struct node *newp)
{
    newp->pNext = pH->pNext;
    pH->pNext = newp;
}

//excluding header data
void list_for_each_1(struct node *pH)
{
    struct node *p = pH->pNext;
    printf("------------begin------------n");
    while(NULL != p->pNext)
    {
        printf("node data : %d.n", p->data);
        p = p->pNext;
    }
    printf("node data : %d.n", p->data);
    printf("------------end------------n");
}

//excluding header data version 2 simplify
void list_for_each_3(struct node *pH)
{
    struct node *p = pH;
    printf("------------begin------------n");
    while(NULL != p->pNext)
    {
        p = p->pNext;
        printf("node data : %d.n", p->data);
    }
    printf("------------end------------n");
}

//including header data
int list_for_each_2(struct node *pH)
{
    struct node *p = pH;
    printf("------------begin------------n");
    if(NULL == p)
    {
    	printf("nothing.n");
    	return -1;
	}
    while(NULL != p->pNext)
    {
        printf("node data : %d.n", p->data);
        p = p->pNext;
    }
    printf("node data : %d.n", p->data);
    printf("------------end------------n");
    return 0;
}

//can not delete header node data
int delete_node(struct node * pH, int data)
{
    struct node *p = pH;
    struct node *pPrev = NULL;
    while(NULL != p->pNext)
    {
        pPrev = p;
        p = p->pNext;
        if(p->data == data)
        {
            if(NULL == p->pNext)
            {
                pPrev->pNext = NULL;
                free(p);
            }
            else
            {
                pPrev->pNext = p->pNext;
                free(p);
            }
			return 0;
        }
    }
    printf("no node deleted.n");
    return -1;
}

//can delete all node including the tail node and header node
struct node * delete_node_2(struct node * pH, int data)
{
    struct node *p = pH;
    struct node *pPrev = NULL;
	pPrev = p;
    while(NULL != p) //traserval to tail
    {
		if(p->data == data)
		{
			//delete header node
			if(p->data == pH->data)
			{
				pH = p->pNext;
				free(p);
				printf("node %d deleted ok.n",data);
				return pH; //delete ok!
			}
			else
			{
				//delete tail node
				if(NULL == p->pNext)
				{
					pPrev->pNext = NULL;
					free(p);
				}
				else
				{
					pPrev->pNext = p->pNext;
					free(p);
				}
				printf("node %d deleted ok.n",data);
				return pH;
			}
			printf("node %d deleted ok.n",data);
		}
		pPrev = p;
		p = p->pNext;
    }
    return pH;
}


int main()
{
    struct node * pHeader = create_node(1);
	printf("Hello world!n");

    insert_tail(pHeader, create_node(2));
    insert_tail(pHeader, create_node(3));
    insert_head(pHeader, create_node(4));
    insert_head(pHeader, create_node(5));
    list_for_each_2(pHeader);
	pHeader = delete_node_2(pHeader, 3);
	pHeader = delete_node_2(pHeader, 2);
	pHeader = delete_node_2(pHeader, 5);
	pHeader = delete_node_2(pHeader, 4);
	pHeader = delete_node_2(pHeader, 1);
	list_for_each_2(pHeader);
//    printf("1 = %dn",pHeader->data);
//    printf("2 = %dn",pHeader->pNext->data);
//    printf("3 = %dn",pHeader->pNext->pNext->data);
//    printf("4 = %dn",pHeader->pNext->pNext->pNext->data);
//    printf("5 = %dn",pHeader->pNext->pNext->pNext->pNext->data);

    return 0;
}

0 人点赞