【数据结构】单链表(无头节点)
2013-04-11 13:01
363 查看
#include "stdlib.h" #include "stdio.h" struct List { int xs; int zs; List *next; }; void InstList(List *&L, int n) { L = (List *)malloc(sizeof(List)); scanf("%d%d", &L->xs, &L->zs); L->next = NULL; List *p = L; for(int i = 0; i < n - 1; i++) { p->next = (List *)malloc(sizeof(List)); p = p->next; scanf("%d%d", &p->xs, &p->zs); p->next = NULL; } } List *ListAdd(List *a, List *b) { List *c, *t, *Lc; if(a->zs > b->zs) { Lc = b; } else { Lc = a; } while(a && b) { if(a->zs > b->zs) { c->next = b; c = c->next; b = b->next; } else if(a->zs < b->zs) { c->next = a; c = c->next; a = a->next; } else if(a->zs == b->zs) { a->xs += b->xs; if(0 == a->xs) { t = a; a = a->next; t = b; b = b->next; free(t); free(t); } else { c->next = a; a = a->next; b = b->next; c = c->next; } } } if(!a)c->next = b; else if(!b)c->next = a; return Lc; } int main(void) { List *La, *Lb, *Lc; int lenla, lenlb; scanf("%d", &lenla); if(lenla < 1001) { InstList(La, lenla); scanf("%d", &lenlb); if(lenlb < 1001) { InstList(Lb, lenlb); Lc = ListAdd(La, Lb); while(Lc) { printf("%d %d\n", Lc->xs, Lc->zs); Lc = Lc->next; } } } return 0; }
相关文章推荐
- 【1138】数据结构上机测试2-1:单链表操作A(顺序建表+删除节点)(SDUT)
- 数据结构—链表-单链表应用-删除元素最大的节点
- 数据结构之—线性表之—浅谈单链表有头结点和无头节点
- 数据结构——10 双向链表插入和删除节点
- 数据结构之链表(头节点链表)
- 【数据结构】单链表—求链表中间节点(只遍历一次链表)— 快慢指针
- 从无头单链表中删除节点 结构之法 4
- 数据结构——2 单链表插入和删除节点
- 数据结构双语课->不带头节点的单链表实现
- 数据结构:单链表(三)输出链表值最大的节点
- 数据结构——求单向链表的倒数第K个节点
- 数据结构_单链表(带头节点)
- 数据结构(第二天)单链表的基本操作,创建单链表,头插法,尾插法,删除节点,查询节点
- 结构之法------从无头单链表中删除节点(扩展问题)
- 若用链表存储一棵二叉树时,每个节点除数据域外,还有指向左孩子和右孩子的两个指针,在这种存储结构中,n歌节点的二叉树共有2N个指针域,其中有N-1个指针域存放了地址,有N+1个指针是空指针。
- 数据结构:单链表(五)之链表中寻找一个特定值,返回该节点值指针 回调函数
- 数据结构之链表面试题汇总(一)--查找单向链表的中间节点、倒数第K个节点
- 数据结构——12 删除两个双向链表中相同的节点
- 数据结构基础 - 链表内节点的删除
- 数据结构二(顺序表 & 链表;循环及无头循环链表 & 链栈 )