DuLinkList

来源:互联网 发布:未来世界网络黄金 编辑:程序博客网 时间:2024/04/28 08:30
#include <iostream>using namespace std;#define OK 1#define ERROR -1#define TRUE 1#define FALSE 0#define OVERFLOW -2typedef char ElemType;typedef struct DuLNode {/************************************************************************//*定义线性双向链表的结构体                                                  *//*Went 2011-10-24 10:49  *//************************************************************************/ElemType data;struct DuLNode *prior;struct DuLNode *next;}DuLNode, *DuLinkList;int InitList_DuL(DuLinkList &L) {//initiate a listif (!(L = (DuLinkList)malloc(sizeof(DuLNode)))) {//head nodeprintf("error occured in InitList\n");return ERROR;}L -> prior = L;L -> next = L;printf("initiate successfully!\n");return OK;}int CreateList_DuL(DuLinkList &L, int n) {//逆位序输入n个元素的值,建立带头节点的单链线性表L。int i;DuLinkList p;if (!(L = (DuLinkList)malloc(sizeof(DuLNode)))) {printf("error occured in CreateList\n");return ERROR;}L -> prior = L;L -> next = L;//head nodeprintf("input the element:\n");for(i = n; i > 0; i--) {if (!(p = (DuLinkList)malloc(sizeof(DuLNode))))return ERROR;cin >> p -> data;p -> next = L -> next;L -> next -> prior = p;L -> next = p;p -> prior = L;}printf("create sccessfully!\n");return OK;}int DestroyList_DuL(DuLinkList &L) {//destroy the listDuLinkList p, q;p = L -> next;while(p != L) {q = p -> next;free(p);p = q;}free(L);L = NULL;printf("destroy successfully!\n");return OK;}int ClearList_DuL(DuLinkList &L) {//clear the listDuLinkList p, q;p = L -> next;while(p != L) {q = p -> next;free(p);p = q;}L -> prior = L;L -> next = L;printf("clear successfully!\n");return OK;}int ListEmpty_DuL(DuLinkList L) {//whether the list is emptyif (L -> next == L && L -> prior == L)return TRUE;elsereturn FALSE;}int ListLength_DuL(DuLinkList L) {//return length of the listDuLinkList p;int count;p = L -> next;count = 0;while(p != L) {count ++;p = p -> next;}return count;}int GetElem_DuL(DuLinkList L, int i, ElemType &e) {//L为带头结点的双链循环线性表的头指针//当第i个元素存在时,其值赋给e并返回OK,否则返回ERRORDuLinkList p;int j;if (i < 1 || i > ListLength_DuL(L))return ERROR;p = L -> next;j = 1;while(p != L && j < i) {p = p -> next;j++;}if (p == L || j > i)return ERROR;e = p -> data;return OK;}int PriorElem_DuL(DuLinkList L, ElemType cur_e, ElemType &pre_e) {//get the prior element if it existsDuLinkList p;p = L -> next -> next;while(p != L) {if (p -> data == cur_e) {pre_e = p -> prior -> data;return TRUE;}p = p -> next;}return FALSE;}int NextElem_DuL(DuLinkList L, ElemType cur_e, ElemType &next_e) {//get the next element if it existsDuLinkList p;p = L -> next;while(p != L) {if (p -> data == cur_e) {next_e = p -> next -> data;return TRUE;}p = p -> next;}return FALSE;}DuLinkList GetElemP_DuL(DuLinkList L, int i) {//在双链循环线性表L中返回第i个元素的位置指针int j;DuLinkList p;p = L;for(j = 1; j <= i; j++) p = p -> next;return p;}int ListInsert_DuL(DuLinkList &L, int i, ElemType e) {//在带头结点的双链循环线性表L中第i个位置之前插入元素eDuLinkList p, s;if ((i < 1 || i > ListLength_DuL(L)) && (ListLength_DuL(L) != 0)) {return ERROR;}if (!(p = GetElemP_DuL(L, i)))return ERROR;if (!(s = (DuLinkList)malloc(sizeof(DuLNode))))return OVERFLOW;s -> data = e;s -> prior = p -> prior;p -> prior -> next = s;s -> next = p;p -> prior = s;return OK;}int ListDelete_DuL(DuLinkList &L, int i, ElemType &e) {//在带头结点的双链循环线性表L中删除第i个元素,其值赋给eDuLinkList p;if (i < 1 || i > ListLength_DuL(L))return ERROR;if (!(p = GetElemP_DuL(L, i)))return ERROR;e = p -> data;p -> prior -> next = p -> next;p -> next -> prior = p -> prior;free(p);return OK;}int ListPrint_DuL(DuLinkList L) {//print element of the listDuLinkList p;p = L -> next;while(p != L) {cout << p -> data << " ";p = p -> next;}printf("\n");return 1;}int main() {DuLinkList list1;ElemType e;InitList_DuL(list1);DestroyList_DuL(list1);//test1DuLinkList list2;CreateList_DuL(list2, 5);ListInsert_DuL(list2, 1, 'a');ListPrint_DuL(list2);cout << "There are " << ListLength_DuL(list2) << " elements in the list" << endl;ListDelete_DuL(list2, 2, e);cout << "There are " << ListLength_DuL(list2) << " elements in the list" << endl;ListPrint_DuL(list2);GetElem_DuL(list2, 5, e);cout << e << endl;ClearList_DuL(list2);ListPrint_DuL(list2);cout << "There are " << ListLength_DuL(list2) << " elements in the list" << endl;ListInsert_DuL(list2, 1, 'a');cout << "There are " << ListLength_DuL(list2) << " elements in the list" << endl;ListPrint_DuL(list2);DestroyList_DuL(list2);//test2system("pause");return 0;}

原创粉丝点击