链表

来源:互联网 发布:拼模特卡软件 编辑:程序博客网 时间:2024/05/01 11:47
#include<iostream>#include<string>#include<random>using namespace std;#define Type int#define null NULLstruct nodeone{   //单向链表Type data;nodeone *next;};struct nodetwo {   //双向循环链表Type data;nodetwo*pre;nodetwo*next;};void initNode(nodeone *&L, int n) {   //初始化尾插法nodeone *q = L;nodeone *p = null;for (int i = 0; i < n; i++) {p = new nodeone;p->data = rand() % 100 + 1;q->next = p;q = p;}p->next = null;}void initNodeT(nodeone *&L, int n) {   //初始化头插法L = new nodeone;L->next = null;int da = 0;cout << "请输入一组有序数据:" << endl;for (int i = 0; i < n; i++) {nodeone *p = new nodeone;cin >> da;p->data = da;p->next = L->next;L->next = p;}}void initNode(nodetwo *&L, int n) {   L = new nodetwo;L->next = null;nodetwo *p = L;nodetwo *q = null;for (int i = 0; i < n; i++) {q = new nodetwo;q->data = rand() % 100 + 1;q->pre = p;p->next = q;p = q;}q->next = L;L->pre = q;}void insertNode(nodeone *&L, Type da, int position) {   //插入节点到具体位置nodeone *q = new nodeone;q->data = da;q->next = null;nodeone *p = L;for (int i = 0; i < position - 1; i++) {p = p->next;}q->next = p->next;p->next = q;}void insertNode(nodetwo *&L, Type da, int position) {nodetwo *p = L;nodetwo *q = new nodetwo;q->data = da;q->pre = null;q->next = null;for (int i = 0; i < position - 1; i++) {p = p->next;}q->next = p -> next;q->pre = p;p->next->pre = q;p->next = q;}Type getElem(nodeone *&L, int position) {   //返回固定位置的数据Type data;nodeone *p = L -> next;for (int i = 0; i < position - 1; i++) {p = p->next;}data = p->data;return data;}Type getElem(nodetwo *&L, int position) {Type data;nodetwo *p = L->next;for (int i = 0; i < position - 1; i++) {p = p->next;}data = p->data;return data;}void deleNode(nodeone *&L, int position) {        //删除固定位置的结点nodeone *p = L;for (int i = 0; i < position - 1; i++) {p = p->next;}p->next = p->next->next;p = null;delete p;}void deleNode(nodetwo *&L, int position) {nodetwo *p = L;for (int i = 0; i < position - 1; i++) {   //指向删除位置的前一个元素p = p->next;}p->next->next->pre = p;p->next = p->next->next;p = null;delete p;}void destroyList(nodeone *&L) {   //销毁while (L) {nodeone *p = L;L = L->next;delete p;p = L;}L = null;}void destroyList(nodetwo *&L) {nodetwo *p = L;while (L != p){nodetwo *p = L;L = L->next;delete p;p = L;}L = null;delete L;}void initArr(nodeone *&L) {cout << "请输入数据长度:" << endl;int num = 0;cin >> num;initNodeT(L, num);}nodeone* dealArr() {      nodeone *one = new nodeone, *two = new nodeone;one->next = null;two->next = null;initArr(one);initArr(two);nodeone* three = new nodeone;three->next = null;nodeone *tem = three;nodeone *q1 = null;one = one->next;two = two->next;while (one || two) {if (one && two) {q1 = new nodeone;if (one->data > two->data) {q1->data = two->data;two = two->next;tem->next = q1;tem = q1;      //随时更新tem位置,保持同步}else{q1->data = one->data;one = one->next;tem->next = q1;tem = q1;}}else {if (one) {tem->next = one;one = null;}if (two) {tem->next = two;two = null;}}}return three;}int main() {cout << "请输入两组整数数据:" << endl;nodeone *result = dealArr();nodeone *p = result->next;while (p) {cout << p->data << " ";p = p->next;}cout << endl;nodetwo *t = new nodetwo;t->next = null;int num = 0;cout << "请输入要新建数据的长度:" << endl;cin >> num;initNode(t, num);int p1 = 0, p2 = 0;cout << "请输入要删除的结点的位置:" << endl;cin >> p1;deleNode(t, p1);cout << "请输入要插入0的结点的位置:" << endl;cin >> p2;insertNode(t, 0, p2);int c = 0;cout << "请输入需要返回位置的数据的值:" << endl;cin >> c;int data = getElem(t, c);cout << "该值为:" << data << endl;cout << "处理后的数据为:" << endl;nodetwo *r = t->next;while (r != t) {cout << r->data << " ";r = r->next;}cout << endl;destroyList(t);return 0;}

0 0
原创粉丝点击