【id:23】【20分】C. DS单链表–合并
题目描述
假定两个单链表是递增有序,定义并实现以下函数,完成两个单链表的合并,继续保持递增有序
int LL_merge(ListNode *La, ListNode *Lb)
输入
第1行先输入n表示有n个数据,接着输入n个数据
第2行先输入m表示有M个数据,接着输入m个数据
输出
输出合并后的单链表数据,数据之间用空格隔开
样例查看模式
正常显示
查看格式
输入样例1 <-复制
3 11 33 55n
4 22 44 66 88n
输出样例1
11 22 33 44 55 66 88 n
#include#include #include #include using namespace std; class ListNode { public: int data; ListNode* next; ListNode() { next = NULL; } ListNode(int i){ this->data=i; next=NULL; } }; class LinkList { public: ListNode* head; int len; //操作定义 LinkList(); ~LinkList(); LinkList* LL_index(int i);//返回第i个节点的指针,如果不存在返回NULL int LL_get(int i); //获取第i个元素的数据 int LL_insrt(int i, int item); int LL_del(int i); void LL_display(); int LL_swap(int pa, int pb){ if(pa>pb) return LL_swap(pb,pa); if (pa<1 || pa>len||pb<1||pb>len) return 0; ListNode* cur1, * pre1=head, *cur2,*pre2=pre1,*tail; cur1 = head->next; cur2 = head->next; for (int q = 1; q < pa; q++) { if (cur1 != NULL) { pre1 = cur1; cur1 = cur1->next; } } for (int q = 1; q < pb; q++) { if (cur2 != NULL) { pre2 = cur2; cur2 = cur2->next; } } tail=cur2->next; cur2->next=cur1->next; pre1->next=cur2; cur1->next=tail; pre2->next=cur1; return 1; } }; LinkList::LinkList() { head = new ListNode(); len = 0; } LinkList::~LinkList() { ListNode* p, * q; p = head; while (p != NULL) { q = p; p = p->next; delete q; } len = 0; head = NULL; } void LinkList::LL_display() { ListNode* p; p = head->next; while (p) { cout << p->data << " "; p = p->next; } cout << endl; } int LinkList::LL_insrt(int i, int item) { if (i<1 || i>len + 1) return 0; ListNode* cur, * pre=head, * now; cur = head->next; for (int q = 1; q < i; q++) { if (cur != NULL) { pre = cur; cur = cur->next; } } now = new ListNode; now->data = item; pre->next = now; now->next = cur; len++; return 1; } int LinkList::LL_del(int i) { if (i<1 || i>len) return 0; ListNode* cur, * pre = head, * now; cur = head->next; for (int q = 1; q < i; q++) { if (cur != NULL) { pre = cur; cur = cur->next; } } pre->next = cur->next; len--; //delete cur; return 1; } int LinkList::LL_get(int i) { if (i<1 || i>len) return 0; ListNode* cur, * pre = head, * now; cur = head->next; for (int q = 1; q < i; q++) { if (cur != NULL) { pre = cur; cur = cur->next; } } return cur->data; } ListNode* LL_merge(ListNode *l1, ListNode *l2){ ListNode* preHead = new ListNode(-1); ListNode* prev = preHead; while (l1 != NULL && l2 != NULL) { if (l1->data < l2->data) { prev->next = l1; //cout< data<<" "; l1 = l1->next; } else { prev->next = l2; //cout< data<<" "; l2 = l2->next; } prev = prev->next; } // 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可 prev->next = l1 == NULL ? l2 : l1; return preHead->next; } int main() { ListNode *l1,*l2,*l3,*cur; l1=new ListNode(-1); l2=new ListNode(-1); int num,lenth; cin >> lenth; cur=l1; for (int i = 0; i < lenth; i++) { cin >> num; cur->next=new ListNode(num); cur=cur->next; } cin >> lenth; cur=l2; for (int i = 0; i < lenth; i++) { cin >> num; cur->next=new ListNode(num); cur=cur->next; } l3=LL_merge(l1->next,l2->next); while(l3){ cout< data<<" "; l3=l3->next; } return 0; }



