简单的双链表实现 不足之处请留言指出。

来源:互联网 发布:ios映射软件 编辑:程序博客网 时间:2024/05/17 07:37

以下是头文件:

#pragma once#ifndef __LIST_H__#define __LIST_H__#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <memory.h> #endif


以下是代码实现:

#include "list.h"typedef int ElemType;typedef struct node {      //节点ElemType elem;struct node* next;   //直接后继struct node* prev;     //直接前驱}node, *pNode;typedef struct {       //链表pNode head,tail;int len;  //长度}DiList, *pDiList;enum { ERROR, OK };    //0  1pNode make_Node(const int data); //创建节点pDiList init_DiList();   //初始化void clear_DiList(pDiList L); //清空pNode find_DiList(const pDiList L, const int key); //查找int remove_DiList(pDiList L, const int key); //移除第一个出现的keyint instail_DiList(pDiList L, const int data); //插入尾部int inshead_DiList(pDiList L, const int data); //插入头部void destory_DiList(pDiList L);  //销毁int modify_DiList(pDiList L, const int key, const int data); //修改第一次出现的keyvoid print_List(const pDiList L);  //打印void test5();/*销毁:是先销毁了链表的头,然后接着一个一个的把后面的销毁了,这样这个链表就不能再使用了,即把包括头的所有节点全部释放。清空:是先保留了链表的头,然后把头后面的所有的都销毁,最后把头里指向下一个的指针设为空,这样就相当与清空了,但这个链表还在,还可以继续使用;即保留了头,后面的全部释放。清空是链表的头还在,可以继续插入节点;销毁就是链表没了,整个链表(包括头)的空间都被释放了,不能进行任何操作了。*///int main()//{//printf("------------------双链表测试------------------\n");//test5();//system("pause");//return 0;//}void test5(){pDiList list = init_DiList();inshead_DiList(list, 1); inshead_DiList(list, 2); inshead_DiList(list, 3);instail_DiList(list, 111); instail_DiList(list, 222); instail_DiList(list, 333);inshead_DiList(list, 1); inshead_DiList(list, 2); inshead_DiList(list, 3);print_List(list);printf("%p\n", find_DiList(list, 1));printf("%d\n", find_DiList(list, 1)->prev->elem);printf("remeve tail:%d\n", remove_DiList(list, 333));printf("remeve others:%d\n", remove_DiList(list, 33));printf("remeve head:%d\n", remove_DiList(list, 3));printf("remeve others:%d\n", remove_DiList(list, 3));print_List(list);printf("modify elem:%d\n", modify_DiList(list, 2, 4));print_List(list);clear_DiList(list);printf("after clear_DiList() list->len:%d\n", list->len);printf("---------------------list address :%p\n", list->head);destory_DiList(list);printf("after destory_DiList list address :%p\n", list->head);}pNode make_Node(const int data)//创建节点{pNode temp = (node*)malloc(sizeof(node));if (temp == NULL){exit(1);}temp->elem = data;temp->prev = NULL;temp->next = NULL;return temp;}pDiList init_DiList()   //初始化{pDiList L = (pDiList)malloc(sizeof(DiList));if (L == NULL){exit(1);}L->head = (node*)malloc(sizeof(node));L->tail = (node*)malloc(sizeof(node));L->head->next = NULL;L->tail->next = NULL;L->head->elem = 0;L->tail->elem = 0;L->len = 0;return L;}void clear_DiList(pDiList L){assert(L);pNode temp = NULL;pNode p = L->head->next;while (p != NULL){temp = p->next;free(p);p = temp;}L->head->next = NULL;L->tail->next = NULL;L->len = 0;}pNode find_DiList(const pDiList L, const int key)//查找{assert(L);pNode p = L->head->next;while (p != NULL){if (p->elem == key){return p;}p = p->next;}return NULL;}int remove_DiList(pDiList L, const int key)//移除第一个出现的key{pNode temp = find_DiList(L, key);if (temp == NULL){return ERROR;}if (temp->next != NULL && temp->prev != L->head)    //第一个和最后一个单独考虑{temp->prev->next = temp->next;temp->next->prev = temp->prev;free(temp);L->len--;return OK;}if (temp->prev == L->head)    //第一个{L->head->next = temp->next;L->head->next->prev = L->head;free(temp);L->len--;return OK;}if (temp->next == NULL)    //最后一个{L->tail = temp->prev;L->tail->next = NULL;free(temp);L->len--;return OK;}return ERROR;}int instail_DiList(pDiList L, const int data)//插入尾部{assert(L);pNode temp = make_Node(data);if (L->head->next != NULL)   //非空表{temp->prev = L->tail;L->tail->next = temp;L->tail = temp;L->len++;return OK;}L->tail = temp;L->head->next = temp;temp->prev = L->head;L->len++;return OK;}int inshead_DiList(pDiList L, const int data)//插入头部{assert(L);pNode temp = make_Node(data);if (L->head->next != NULL){L->head->next->prev = temp;temp->prev = L->head;temp->next = L->head->next;L->head->next = temp;L->len++;return OK;}L->head->next = temp;L->tail = temp;temp->prev = L->head;L->len++;return OK;}void destory_DiList(pDiList L)    //销毁{assert(L);//删除链表L中除了头节点之外的所有节点clear_DiList(L);free(L);L = NULL;}int modify_DiList(pDiList L, const int key, const int data)//修改第一次出现的key{assert(L);if (find_DiList(L, key) != NULL){find_DiList(L, key)->elem = data;return OK;}return ERROR;}void print_List(const pDiList L){assert(L);pNode p = L->head->next;while (p != NULL){printf("%d ", p->elem);p = p->next;}printf("\n");}


原创粉丝点击