已有a,b两个链表,每个链表中的结点包括学号,成绩。要求把两个链表合并。按学号升序排列.
#include <stdio.h>
#define SIZE sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
struct student * create();
struct student * input();
void print(struct student *);
struct student * union_linktable(struct student *,struct student *);
struct student * insert(struct student *,struct student *);
int main(int argc,char *argv[])
{
struct student *head1,*head2;
printf("please input linktable1:\n");
head1 = (struct student *)create();
printf("the lintable1 data is \n");
print(head1);
printf("please input linktable2:\n");
head2 = (struct student *)create();
printf("the lintable2 data is \n");
print(head2);
printf("union linktable1 and linktable2:\n");
head1 = union_linktable(head1,head2);
print(head1);
system("pause");
return 0;
}
struct student * create()
{
int n = 0;
struct student *head;
struct student *p1,*p2;
head = NULL;
do
{
p1 = input();
if (0 == p1->num)
{
break;
}
else
{
if (0 == n)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
n++;
}
}while(1);
p2->next = NULL;
return head;
}
struct student * input()
{
struct student *p;
p = (struct student *)malloc(SIZE);
printf("please input num,score:");
scanf("%ld,%f",&p->num,&p->score);
return p;
}
void print(struct student *head)
{
struct student *p;
p = head;
if (head != NULL)
{
do
{
printf("num:%ld,score:%5.1f\n",p->num,p->score);
p = p->next;
}while(p != NULL);
}
else
{
printf("error:linktable data is NULL.\n");
}
}
struct student * union_linktable(struct student *head1,struct student *head2)
{
struct student *pa1,*pa2,*pb1,*pb2;
pa1 = pa2 = head1;
pb1 = pb2 = head2;
do
{
while ((pb1->num > pa1->num) && (pa1->next) != NULL)
{
pa2 = pa1;
pa1 = pa1->next;
}
if (pb1->num <= pa1->num)
{
if(head1 == pa1)
{
head1 = pb1;
}
else
{
pa2->next = pb1;
}
pb2->next = pa1;
pa2 = pb2;
pb2 = pb1;
}
}while((pa1->next != NULL) || (pa1 == NULL && pb1 != NULL));
if ((pb1 != NULL) && (pb1->num > pa1->num) &&(pa1->next == NULL))
{
pa1->next = pb1;
}
return head1;
}
struct student *insert(struct student *head,struct student *stu)
{
struct student *p0,*p1,*p2;
p1 = head;
p0 = stu;
if(NULL == head)
{
head = p0;
p0->next = NULL;
}
else
{
while (p0->num > p1->num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (p0->num <= p1->num)
{
if (p1 == head)
{
head = p0;
}
else
{
p2->next = p0;
}
p0->next = p1;
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
return head;
}
已有a,b两个链表,每个链表中的结点包括学号,成绩。要求把两个链表合并。按学号升序排列.的更多相关文章
- 已有a,b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列
1.我的思路先将b链表连接在a链表的后面,这个很容易实现,将a链表最后的结点中的p.next改为指向b链表的头结点即可. 再将这个新链表用选择排序即可. 代码如下: #include<stdio ...
- [PHP] 算法-请找出带环链表的环的入口结点的PHP实现
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null 1.找链表倒数第k个结点,输入一个链表,输出该链表中倒数第k个结点.第一个指针走(k-1)步,到达第k个节点,两个指针同时往后 ...
- 35.两链表的第一个公共结点[Find the first common node of two linked list]
[题目] 两个单向链表,找出它们的第一个公共结点. 链表的结点定义为: C++ Code 123456 struct ListNode { int m_nKey; ...
- 九度OJ 1505 两个链表的第一个公共结点 【数据结构】
题目地址:http://ac.jobdu.com/problem.php?pid=1505 题目描述: 输入两个链表,找出它们的第一个公共结点. 输入: 输入可能包含多个测试样例. 对于每个测试案例, ...
- 【剑指offer】面试题37:两个链表的第一个公共结点
题目: 输入两个链表,找出它们的第一个公共结点. 思路: 由链表的定义知是单链表.对于单链表,如果两个链表有公共结点,则两个链表必然是像Y型相交.则先计算出各个链表的长度,让长链表的头指针先走多出来的 ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- [PHP] 算法-找出两个链表的第一个公共结点的PHP实现
输入两个链表,找出它们的第一个公共结点 1.两个单链表,有公共结点,那么必然,尾部公用 2.找出链表1的长度,找出链表2的长度,长的链表减去短的链表得出一个n值 3.长的链表先走n步,两个链表再同时移 ...
- 【Java】 剑指offer(52) 两个链表的第一个公共结点
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 输入两个链表,找出它们的第一个公共结点. 思路 蛮力法:遍历第一个 ...
- 《剑指offer》第五十二题(两个链表的第一个公共结点)
// 面试题52:两个链表的第一个公共结点 // 题目:输入两个链表,找出它们的第一个公共结点. #include <iostream> #include "List.h&quo ...
随机推荐
- mytbatis小问题
使用mybatis出现以下异常 SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, Postgr ...
- Error 0x800704cf
重装了系统,改过网络配置,结果共享和打印机连不上,显示Error 0x800704cf 在本地连接的属性里将"Client for Microsoft Networks"勾选上就可 ...
- Svn + tomcat + Hudson持续集成部署
1.首先下载hudson 2. 我这里使用hudson-3.0.1版本 3. 下载后hudson是一个 war 包 4. 操作部署: (1). 直接将hudson的war包复制到tomcat的weba ...
- Effective Java 读书笔记之九 并发
一.访问共享的可变数据时要同步 1.synchronized关键字既然保证访问的可见性也能保证原子性.而volatile修饰符只能保证变量的线程可见性. 2.增量操作符等不是原子性,多线程操作时可能导 ...
- angularjs入门基础一
app.controller('firstController',function($scope,$rootScope){ $scope.name='张三'; $rootScope.age='30'; ...
- 160809209_李梦鑫_C语言程序设计实验2+选择结构程序设计_进阶
<C语言程序设计>实验报告 学 号 160809209 姓 名 李梦鑫 专业.班 计科16-2班 学 期 2016-2017 第1学期 指导教师 黄俊莲 吴喆 实验地点 C05 机 ...
- 2 GPS utility methods
Methond 1 is to check whether the GPS is on: 1 2 3 4 5 public boolean isGPSIsOn() { LocationManager ...
- Linux下编译安装Apache Http Server
Linux下编译安装Apache Http Server [TOC] 1.下载httpd-2.4.12.tar.bz2 wget http://mirror.bit.edu.cn/apache/htt ...
- html之左边不动右边内容自动修剪并出现滚动轮查看剩余内容
<html lang="en"> <head> <meta charset="UTF-8"> <title>Ti ...
- java复制文件
package com.test.tes; import java.io.File; import java.io.FileInputStream; import java.io.FileOutput ...