首页 > 其他 > 详细

【练习】P62页3.2题

时间:2014-05-24 14:26:09      阅读:441      评论:0      收藏:0      [点我收藏+]


/*---给你一个链表L和另一个链表P,它们包含以升序排列的整数。操作PrintLots(L,P)
	将打印L中那些由P所指定位置上的元素。---*/
#include <stdio.h>
#include <stdlib.h>

struct Node{
	int val;
	struct Node *next;	
};

Node *findEnd(Node *list){	
	while(list->next) list = list->next;
	return list;
}

void insert(int val, Node *list){
	Node *p = (Node *)malloc(sizeof(Node));
	p->val = val; p->next = NULL;
	Node *end = findEnd(list);
	end->next = p;
}
Node *creatNewList(){
	return (Node *)malloc(sizeof(Node));
}

void deleteList(Node *list){
	Node *p;
	while(list){
		p = list->next;
		free(list);
		list = p;
	}
}

void print(Node *list, int k){
	int i = 0;
	while(i < k){
		list = list->next;
		++i;
		if(list == NULL){
			printf("Out of list length!");
			return;
		}
	}
	printf("%d ", list->val);
}

int main(){
	Node *L = creatNewList(), *ptr;
	Node *P = creatNewList();
	L->next = NULL;	
	int a;
	while(scanf("%d", &a) == 1)
		insert(a, L);
	P->next = NULL;
	while(scanf("%d", &a) == 1)
		insert(a, P);
	ptr = P->next;
	while(ptr){
		print(L, ptr->val);
		ptr = ptr->next;
	}
	
	deleteList(L);
	deleteList(P);
	return 0;
}


【练习】P62页3.2题,布布扣,bubuko.com

【练习】P62页3.2题

原文:http://blog.csdn.net/chang_mu/article/details/26668125

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!