标签:
#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define MYOVERFLOW -2 typedef int Status; typedef int Elemtype;//用指定标识符Elemtype代表int类型,顾名思义表示元素类型为int型 typedef struct LNode{ Elemtype data; LNode *next; }*LinkList; Status visit(LinkList L);//对链表进行遍历的函数 void assignment(LinkList &L);//为链表附上初始值 Status GetElem_L(LinkList L, int i, Elemtype &e); //L为带头结点的单链表的头指针 //当第i个元素存在是,其值赋给e并返回OK,否则返回ERROR void ListTraverse(LinkList L, Status(*pf)(LinkList L));//依次对L的每个元素调用函数,一旦函数失败,则操作失败 Status ListInsert_L(LinkList &L, int i, Elemtype e);//在带头结点的单链线性表L中第i个位置之前插入元素e Status ListDelete_L(LinkList &L, int i, Elemtype &e);//在带头结点的单链线性表L中,删除第i个元素,并由e返回其值 void MergeList_L(LinkList &La, LinkList &Lb, LinkList &Lc); //已知单链线性表La和Lb的元素按值非递减排列 //归并La和Lb得到新的单链线性表Lc,Lc的元素也按值非递减排列 Status visit(LinkList L)//对链表进行遍历的函数 { if (L == NULL){ cout << "it is an empty list!" << endl;//L是空链表,遍历失败 return ERROR; } LinkList p; p = L; p = p->next; for (; p != NULL; p=p->next)//输出p->data知道p为NULL,此时链表已被遍历完全 cout << p->data << " "; cout << endl; return OK; } void assignment(LinkList &L)//为链表附上初始值 { cout << "please input the length of the linklist:"; int n; cin >> n;//输入链表的节点个数,不包括一个空的头结点 cout << endl; cout << "please input the data of the LNode:"; LNode *p; p = new LNode; p->data = NULL;//头结点的数据元素为NULL L = p; for (int i = 1; i <= n; i++){ p->next = new LNode;//建立新的节点 p = p->next; //指针后移 cin >> p->data; //输入节点的数据元素 } p->next = NULL; //最后一个节点指针域为空指针 cout << "the linklist‘s assignment is completed!" << endl; }void ListTraverse(LinkList L, Status(*pf)(LinkList L))//依次对L的每个元素调用函数,一旦函数失败,则操作失败 { visit(L); } Status GetElem_L(LinkList L, int i, Elemtype &e) //L为带头结点的单链表的头指针 //当第i个元素存在是,其值赋给e并返回OK,否则返回ERROR { if (i < 1){ cout << "the number of " << i << " you have input is wrong!" << endl;//位置<1则说明输入数据错误 return ERROR; } LinkList p; p = L; for (int j = 1; j <= i&&p!=NULL; j++){//将p移动到第i个结点(不包括头结点) p = p->next; } if (p == NULL){ //如果移动到最后一个结点还未到达第i个结点,则说明输入数据错误,超过了链表长度 cout << "can‘t find the position of i in the linklist!" << endl; return ERROR; } e = p->data;//将i位的数据元素赋值给e return OK; } Status ListInsert_L(LinkList &L, int i, Elemtype e)//在带头结点的单链线性表L中第i个位置之前插入元素e { LinkList p; p = L; int j = 1; if (i >= 1){ for (; j < i&&p; j++, p = p->next){}//找到i位的前一个结点 if (p){ LinkList temp = new LNode; temp->data = e; temp->next = p->next;//修改指针,使新建的结点插入到链表中 p->next = temp; return OK; } else { //如果到达最后一个结点还没到达i的前一个结点,则说明输入数据错误 cout << "the position " << i << " you have input is wrong!" << endl; return ERROR; } } else { cout << "the position " << i << " you have input is wrong!" << endl; return ERROR; } } Status ListDelete_L(LinkList &L, int i, Elemtype &e)//在带头结点的单链线性表L中,删除第i个元素,并由e返回其值 { LinkList p; p = L; int j = 1; if (i >= 1){ for (; j <i&&p->next; j++, p = p->next){}//找到i位的前一个结点 if (p->next){ LinkList temp; temp = p->next; e = temp->data; p->next=p->next->next;//修改指针,使新建的结点插入到链表中 delete temp; return OK; } else { //如果到达最后一个结点还没到达i的前一个结点,则说明输入数据错误 cout << "the position " << i << " you have input is wrong!" << endl; return ERROR; } } else { cout << "the position " << i << " you have input is wrong!" << endl; return ERROR; } } void MergeList_L(LinkList &La, LinkList &Lb, LinkList &Lc) //已知单链线性表La和Lb的元素按值非递减排列 //归并La和Lb得到新的单链线性表Lc,Lc的元素也按值非递减排列 { LinkList pa, pb, pc; pa = La->next; pb = Lb->next; pc = Lc; for (; pa&&pb;){ if (pa->data < pb->data){ pc->next = pa; pa = pa->next; pc = pc->next; } else{ pc->next = pb; pb = pb->next; pc = pc->next; } } if (pa) pc -> next = pa; else pc->next = pb; }
标签:
原文地址:http://www.cnblogs.com/csudanli/p/4795822.html