已知p指向双向循环链表中的一个结点,其结点结构为data、prior、next三个域,实现交换p所指向的结点和它的前缀结点的顺序。
输入格式:
第一行输入元素个数,第二行输入元素值,第三行输入要交换的元素值,第四行输出结果。
输出格式:
输出交换后的结果,中间不用空格分隔,若要交换的值:4,不在数组终,则输出“未找到4”
代码如下#include<iostream> #define ElemType int using namespace std; typedef struct DuLNode { ElemType elem; struct DuLNode* prior; struct DuLNode* next; }DuLNode,*DuLinkList; DuLinkList input(DuLinkList& head, int x) { head = new DuLNode; head->next = NULL; head->prior = NULL; head->elem = x; DuLNode* p, * q; p = head; for (int i = 0; i < x; i++) { q = new DuLNode; q->next = NULL; q->prior = p; p->next = q; cin >> q->elem; p = q; } return head; } DuLNode* getelem(DuLinkList L, int e) { DuLNode* p; p = L->next; while (p && p->elem != e) p = p->next; return p; } void output(DuLinkList& head, int y) { DuLNode* q; DuLNode* z = getelem(head, y); q = head->next; if (z == NULL) { cout << "未找到" << y; return; } while (q && q != NULL) { if (q != z->prior && q != z) cout << q->elem; else if (q == z->prior) cout << q->next->elem; else if (q == z) cout << q->prior->elem; q = q->next; } } int main() { int a, b; DuLinkList head; cin >> a; head = input(head, a); cin >> b; output(head, b); return 0; }
标签:head,int,elem,next,链表,prior,循环,双向,DuLNode From: https://www.cnblogs.com/psh888/p/17001623.html