【C语言】简单链表操作

#include 
#include 
#include 
#include 

#define GlobalDebugFlag 1

#define DEBUG_PRINT_WITH_TIME(fmt, args...) { \
                    do \
                    { \
                        if (GlobalDebugFlag == 1 ) \
                        { \
                            time_t timep = {0}; \
                            struct tm *p = NULL; \
                            time(&timep); \
                            p=localtime(&timep); \
                            fprintf(stdout, "[%4d-%02d-%02d %02d:%02d:%02d %s %s %d] ", \
                            (1900+p->tm_year), \
                            (1+p->tm_mon), \
                            p->tm_mday,  \
                            p->tm_hour, \
                            p->tm_min,  \
                            p->tm_sec, \
                            __FILE__, \
                            __FUNCTION__, \
                            __LINE__); \
                           fprintf(stdout, fmt "\n", ##args); \
                        } \
                    } \
                    while(0); \
					}


//定义节点
struct _Node;
typedef struct _Node Node;

struct _Node
{
    int data;
    Node *next;
};

//定义列表
typedef struct _List 
{
    Node *head;  //记录头地址
    int count;
} List;

// 创建链表
List *ListCreate()
{
    List *list;
    list = (List *)malloc( sizeof(List) );
    list->count = 0;
    list->head = 0;
    
    return list;
}
// 删除链表
void ListDestroy(List *list)
{
    Node *current;
    Node *next;
    current = list->head;
    while (current->next != NULL)
	{
        next = current->next;
        free(current);
        current = next;
    }
	list->head = NULL;
    free(list);
    list = NULL;
}

// 查找元素
// situ=true时, 返回当前位置, false, 则返回上一个位置
Node *ListSearch(List *list, int data)
{
    Node *node;
    node = list->head;
    while ( node->next != NULL )
	{
        if ( node->data == data)
            return node;
        node = node->next;
    }
    return NULL;
}

// 查找元素
// situ=true时, 返回当前位置, false, 则返回上一个位置
Node *ListSearchBefore(List *list, int data)
{
    Node *node;
    node = list->head;
    while ( node->next != NULL )
	{
        if ( node->next->data == data)
            return node;
        node = node->next;
    }
    return NULL;
}

//删除节点
bool ListDelete( List* list, int data)
{
    Node *node;
    Node *tmp;
    node = list->head;
    
    // 判断这个节点是否是首节点
    if ( node->data == data )
	{
        free(list->head);
        list->head = NULL;
        list->count = 0;
        return true;
    }
    
    // 查找给定节点的前一个节点
    node = ListSearchBefore(list, data);
    
    // 找不到节点
    if (  node  == NULL){
        return false;
    }
    //删除
    tmp = node->next->next;
    free(node->next);
    node->next = tmp;
    return true;
}

//在给定元素前加节点
bool ListInsertBefore( List* list, int query, int data)
{
    Node *node;
    Node *new_node;

    // 为新节点分配内存
    new_node = (Node *)malloc( sizeof(Node) );
    if ( new_node == NULL) return false;
    new_node->data = data;

    node = list->head;
    // 判断这个节点是否是首节点
    if ( node->data == query )
	{
        new_node->next = node->next ;
        node->next = new_node;
        return true;
    }

    // 查找给定节点的前一个节点
    node = ListSearchBefore(list, query);
    if (  node  == NULL)// 找不到节点
	{
        return false;
    }
    new_node->next = node->next ;
    node->next = new_node;

    return true;
}

// 加入元素到末尾
bool ListAdd(List *list, int data)
{
    Node *node;
    Node *new_node;

    new_node = (Node *)malloc( sizeof(Node) );    
    if ( new_node == NULL) return false;
    
    new_node->data = data;
    new_node->next = NULL;

    //获取链表head
    node = list->head ;
    
    if (node == NULL)
    {
		list->head = new_node;
		return true;
	}
    
    while(node->next != NULL)
    {
		node = node->next;
	}
    	    
    node->next = new_node;
    list->count+=1;

    return true;
}

//打印整个链表
void ListPrint(List *list)
{
    Node *node;
    node = list->head;
    DEBUG_PRINT_WITH_TIME("Linked List:");
    int i = 1;
    while (node != NULL)
	{
        DEBUG_PRINT_WITH_TIME("%d: %d", i++, node->data);
        node = node->next;
    }
	DEBUG_PRINT_WITH_TIME("");
}

int main()
{    
	int data, data2;
	bool ret = 0;
    List *list;
    list = ListCreate();
    
	DEBUG_PRINT_WITH_TIME("Please input data(end with 0):");
    scanf("%d", &data);
    
    //读取数据
    while (data != 0)
	{
		ret = ListAdd(list, data);
        if (ret == false)
        {
			return 0;
		}
        scanf("%d", &data);
    }
    DEBUG_PRINT_WITH_TIME("current data ");
    ListPrint(list);
    	
	DEBUG_PRINT_WITH_TIME("Please input search data:");
	scanf("%d", &data);
    Node *node = ListSearch(list, data);
    if(node != NULL)
	{
		DEBUG_PRINT_WITH_TIME("ListSearch result successfully: %d", node->data);
	}
	else
	{
		DEBUG_PRINT_WITH_TIME("Can not found in List: %d", data);
	}
    
	DEBUG_PRINT_WITH_TIME("\nPlease input delete data:");
	scanf("%d", &data);
    ListDelete(list, data);
    ListPrint(list);
    
	DEBUG_PRINT_WITH_TIME("\nPlease input two data(Before data1 insert data2):");
	scanf("%d %d", &data, &data2);
    DEBUG_PRINT_WITH_TIME("insert %d before %d ", data, data2);
    ListInsertBefore(list, data, data2);
    ListPrint(list);
    
    ListDestroy(list);

    return 0;
}

运行效果:

[2023-05-31 21:18:28 F:\projects\c\llvmorg-16.0.3\clang\link2.c main 224] Please input data(end with 0):
1
2
3
4
5
0
[2023-05-31 21:18:34 F:\projects\c\llvmorg-16.0.3\clang\link2.c main 237] current data
[2023-05-31 21:18:34 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 207] Linked List:
[2023-05-31 21:18:34 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 1: 1
[2023-05-31 21:18:34 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 2: 2
[2023-05-31 21:18:34 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 3: 3
[2023-05-31 21:18:34 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 4: 4
[2023-05-31 21:18:34 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 5: 5
[2023-05-31 21:18:34 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 214]
[2023-05-31 21:18:34 F:\projects\c\llvmorg-16.0.3\clang\link2.c main 240] Please input search data:
3
[2023-05-31 21:18:39 F:\projects\c\llvmorg-16.0.3\clang\link2.c main 245] ListSearch result successfully: 3
[2023-05-31 21:18:39 F:\projects\c\llvmorg-16.0.3\clang\link2.c main 252]
Please input delete data:
2
[2023-05-31 21:18:43 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 207] Linked List:
[2023-05-31 21:18:43 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 1: 1
[2023-05-31 21:18:43 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 2: 3
[2023-05-31 21:18:43 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 3: 4
[2023-05-31 21:18:43 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 4: 5
[2023-05-31 21:18:43 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 214]
[2023-05-31 21:18:43 F:\projects\c\llvmorg-16.0.3\clang\link2.c main 257]
Please input two data(Before data1 insert data2):
4 2
[2023-05-31 21:19:02 F:\projects\c\llvmorg-16.0.3\clang\link2.c main 259] insert 4 before 2
[2023-05-31 21:19:02 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 207] Linked List:
[2023-05-31 21:19:02 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 1: 1
[2023-05-31 21:19:02 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 2: 3
[2023-05-31 21:19:02 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 3: 2
[2023-05-31 21:19:02 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 4: 4
[2023-05-31 21:19:02 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 211] 5: 5
[2023-05-31 21:19:02 F:\projects\c\llvmorg-16.0.3\clang\link2.c ListPrint 214]

--------------------------------
Process exited after 34.26 seconds with return value 0
请按任意键继续. . .

你可能感兴趣的:(链表,c语言,数据结构)