单向线性链表的实现

来源:互联网 发布:大数据产业园怎么样 编辑:程序博客网 时间:2024/05/17 01:22

用c实现的单向线性链表,支持追加,插入节点,删除节点,清空,删除匹配节点,链表反转,有序插入等操作。分为三个文件list.h包含链表结构和函数的声明。list.cpp是各种函数的实现,test.cpp包含测试代码。


/*list.h*/
#ifndef _LIST_H#define _LIST_H#include <iostream>using namespace std;typedef struct node{int data;struct node * next;}ListNode;typedef struct list{ListNode * head;ListNode * tail;}List;/*初始化链表*/void List_init(List * list);/*释放链表*/void List_deinit(List * list);/*追加元素*/void List_append(List * list,int data);/*指定位置插入元素*/void List_insert(List * list,int data,int pos);/*删除指定位置的元素*/void List_remove(List * list,int pos);/*链表长度*/size_t List_size(List * list);/*正向打印链表*/void List_print(List * list);/*反向打印链表*/void List_unprint(List * list);/*链表反转*/void List_reverse(List * list);/*链表的有序插入*/void Listsort_insert(List * list,int data);/*将两个有序链表合并成一个有序的链表*/void List_merge(List * list1,List * list2);/*判断链表是否回文*/bool huiwen(ListNode * pHead);/*将链表中大于val的节点放在右边,小于等于的放在左边*/ListNode* listDivide(ListNode* head, int val);#endif

/*list.cpp*/#include "list.h"#include <stdlib.h>/*创建一个链表节点*/static ListNode * create_node(ListNode * next,int data){ListNode * node = (ListNode *)malloc(sizeof(ListNode));node->data = data;node->next = next;return node;}/*删除链表节点*/static ListNode * delete_node(ListNode * node){ListNode * next = node->next;free(node);return next;}/*初始化链表*/void List_init(List * list){list->head = NULL;list->tail = NULL;}/*释放链表*/void List_deinit(List * list){while (list->head){list->head = delete_node(list->head);}}/*追加元素*/void List_append(List * list,int data){ListNode * node = create_node(NULL,data);if (list->tail)list->tail->next = node;elselist->head = list->tail = node;list->tail = node;}/*指定位置插入元素*/void List_insert(List * list,int data,int pos){ListNode * node = create_node(NULL,data);ListNode * temp = NULL;if (pos <=0 ){printf("pos的位置不合理!\n");return;}else if (pos == 1){node->next = list->head;list->head = node;}else {ListNode * find = NULL;for (find = list->head;find;find = find->next){pos--;/*pos=0表示find指针到了最后的位置*/if (pos == 0)break;else if (pos == 1)temp = find;else if (pos > 0 && find == list->tail){printf("插入的位置pos不合理!\n");return;}}temp->next = node;node->next = find;}}/*删除指定位置的元素*/void List_remove(List * list,int pos){/*优先处理边界条件,养成这样的习惯*//*边界条件包括:删除的是第一个元素,删除的是最后一个元素,链表还是空的时候就进行删除操作*/ListNode * temp = NULL;if (pos <= 0){printf("删除的位置不合理!\n");return;}else if (pos == 1){list->head = delete_node(list->head);return;}else{ListNode * find = NULL;for (find = list->head;find;find = find->next){pos--;if (pos == 1)temp = find;if (pos == 0 && !find->next){delete_node(list->tail);list->tail = temp;return;}if (pos == 0 && find->next){temp->next = delete_node(find);break;}if (!find->next && pos > 0){printf("输入的位置不合理!\n");return;}}}}/*链表长度*/size_t List_size(List * list){ListNode * find = NULL;size_t count = 0;for (find = list->head;find;find = find->next)count++;return count;}/*正向打印链表*/void List_print(List * list){for (ListNode * find = list->head;find;find = find->next){printf("%d  ",find->data);}printf("\n");}/*反向打印以node为起始节点的链表*/void print(ListNode * node){if (node){print(node->next);printf("%d ",node->data);}}/*反向打印链表*/void List_unprint(List * list){print(list->head);printf("\n");}/*逆转以node为起始节点的链表*/void reverse(ListNode * node){if (node && node->next){reverse(node->next);node->next->next = node;}}/*链表反转*/void List_reverse(List * list){reverse(list->head);ListNode * temp = list->head;list->head = list->tail;list->tail = temp;list->tail->next = NULL;}/*判断链表是否回文*/bool huiwen(ListNode * pHead){    ListNode * find = NULL;        ListNode * start = NULL;        ListNode * last = NULL;        int size = 0;        /*统计链表元素个数*/        for (find = pHead;find;find=find->next){            size++;            /*保存末端节点*/            if (!find->next)                last = find;        }                   /*从第temp个节点开始逆转*/        int temp = size/2 + 1;        int temp1 = temp;        /*找到开始逆转的节点*/        for (find = pHead;find;find=find->next){            temp--;            if (temp == 0){                start = find;break;            }        }        reverse(start);        start->next = NULL;        temp1--;        for (ListNode*first = pHead,*end = last;;first=first->next,end=end->next){            temp1--;            if (first->data != end->data)                return false;            if (!temp1)                break;        }        return true;}/*链表的有序插入*/void Listsort_insert(List * list,int data){ListNode * node = create_node(NULL,data);/*边界条件的处理*/if (!list->head && !list->head)list->head = list->tail = node;else if (node->data <= list->head->data){node->next = list->head;list->head = node;}else if (node->data >= list->tail->data){list->tail->next = node;list->tail = node;}/*一般情况的处理,前面排除了所有情况,即插入节点不可能在最前面,也不可能在最后面,以保证下面的循环没有错*/else{ListNode * find = NULL;for (find = list->head;find;find = find->next)if (node->data <= find->next->data){node->next = find->next;find->next = node;break;}}}/*合并以node1和node2为起始节点的链表,并且返回合并后的首节点地址*/ListNode* Node_merge(ListNode * node1,ListNode * node2){/*边界条件的处理*/if (!node1 && !node2)return NULL;if (!node1)return node2;if (!node2)return node1;ListNode * node = NULL;if (node1->data <= node2->data){node = node1;node->next = Node_merge(node1->next,node2);}else if (node1->data > node2->data){node = node2;node->next = Node_merge(node1,node2->next);}return node;}/*将两个有序链表合并成一个有序的链表*/void List_merge(List * list1,List * list2){list1->head = Node_merge(list1->head,list2->head);if (list1->tail->data < list2->tail->data)list1->tail = list2->tail;list2->head = list2->tail = NULL;}/*将链表中大于val的节点放在右边,小于等于的放在左边*/ListNode* listDivide(ListNode* head, int val) {        // write code here       ListNode * less_equ_head=NULL;       ListNode * less_equ_tail=NULL;       ListNode * more_head = NULL;       ListNode * more_tail = NULL;             // ListNode * find = head;       while (head){           /*备份当前节点的下一个节点*/           ListNode * temp = head->next;           /*分离当前节点*/           head->next=NULL;           /*判断当前节点的值*/           if (head->data<=val){               if (!less_equ_head)                   less_equ_head=less_equ_tail=head;               less_equ_tail->next = head;               less_equ_tail = head;           }           if (head->data>val){               if (!more_head)                   more_head=more_tail=head;               more_tail->next=head;               more_tail=head;           }           head=temp;       }        if (less_equ_tail)            less_equ_tail->next=more_head;        return less_equ_head?less_equ_head:more_head;    }


/*test.cpp*/#include "list.h"#include <stdlib.h>#include <time.h>int main(){srand(time(NULL));List list1,list2,list;List_init(&list1);List_init(&list2);List_init(&list);/*链表追加元素的验证*/for (int i = 1;i < 10;i++)List_append(&list,i);/*链表指定位置插入*/List_insert(&list,15,1);List_insert(&list,155,3);List_insert(&list,66,5);List_insert(&list,100,15);/*链表打印*/List_print(&list);/*删除链表元素测试*/List_remove(&list,3);List_remove(&list,1);List_print(&list);/*链表反向打印*///List_unprint(&list);/*链表逆转*///List_reverse(&list);//List_print(&list);/*链表长度*///printf("%u\n",List_size(&list));/*为链表加入随机数*/for (int i = 0; i < 10;i++)Listsort_insert(&list1,rand() % 100 + 1);for (int i = 0; i < 5;i++)Listsort_insert(&list2,rand() % 100 + 1);printf("list[1]:\n");List_print(&list1);printf("list[2]:\n");List_print(&list2);/*对两个有序链表合并成一个有序链表*/List_merge(&list1,&list2);printf("list[1]:\n");List_print(&list1);printf("list[2]:\n");//printf("list1 size = %u\n",List_size(&list1));List_print(&list2);////////////////////////////////////////////////////////////////////////////////////cout << "------------------------------------------------------------------" << endl;/*判断链表是不是回文结构*/List huiwenlist;List_init(&huiwenlist);List_append(&huiwenlist,1);List_append(&huiwenlist,2);List_append(&huiwenlist,3);List_append(&huiwenlist,3);List_append(&huiwenlist,2);List_append(&huiwenlist,1);List_print(&huiwenlist);cout << huiwen(huiwenlist.head) << endl; cout <<"-------------------------------------------------"<<endl;List list3;List_init(&list3);for (int i = 0; i < 10;i++)List_append(&list3,rand() % 100 + 1);List_print(&list3);ListNode * head = listDivide(list3.head,50);for (ListNode * find = head;find;find=find->next)cout << find->data<<"  ";cout << endl;List_deinit(&list3);List_deinit(&list1);List_deinit(&list2);List_deinit(&list);List_deinit(&huiwenlist);}





1 0
原创粉丝点击