7-6 程序设计综合实践 1.3

7-6 程序设计综合实践 1.3

1.3 在第1题( 编写程序,建立2个带头结点单链表,输入若干整数将正整数插入第1个单链表,将负整数插入第2个单链表,插入前和插入后单链表保持递增或相等次序,显示2个单链表,最后销毁。程序不可存在内存泄漏。)建立2个单链表基础上,设计和实现就地逆置单链表函数,即利用原单链表结点建立元素次序相反的单链表。编写程序,建立2个单链表,就地逆置这2个单链表,显示逆置前后的各单链表。注意不可存在内存泄漏。

输入格式

若干整数。

输出格式:

每个单链表输出占一行,元素间用分隔符分隔;两个初始单链表和两个就地逆置后单链表,4个单链表,共4行。

输入样例:

下面展示一些 内联代码片

100 2 3 -2 -8 -6 -9 -10 50 2 -1

输出样例:

2->2->3->50->100
-10->-9->-8->-6->-2->-1
100->50->3->2->2
-1->-2->-6->-8->-9->-10

代码如下:

#include<stdio.h>
#include<stdlib.h>
/*
	思路:
		(1)输入一排数据
		(2)申请两个链表,将正数和负数分别放入在两个链表中
		(3)将两条链表从低到高排序
		(4)输出两条链表
		(5)将两条链表逆序然受分别输出
*/
struct Node {
	int data;
	struct Node* next;
};
//创建头节点
struct Node* Create_head() {
	struct Node* head = (struct Node*)malloc(sizeof(struct Node));
	if (head != NULL) {
		head->next = NULL;
	}
	return head;
}
//创建新节点
struct Node* Create_node(int data) {
	struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
	if (new_node != NULL) {
		new_node->data = data;
		new_node->next = NULL;
	}
	return new_node;
}
//尾插法
struct Node* Insert_node(struct Node* head, struct Node* new_node) {
	struct Node* temp;
	temp = head;
	while (temp->next !=NULL) {
		temp = temp->next;
	}
	temp->next = new_node;
}
//打印链表
void Print_node(struct Node* head) {
    struct Node* temp = head;
    if(temp==NULL){//判断链表是否为空
        free(temp);
    }
    if(temp!=NULL&&temp->next!=NULL){
        temp=temp->next;
        printf("%d", temp->data);
    }
	while (temp->next != NULL) {
        temp = temp->next;
		printf("->%d", temp->data);
	}
	printf("\n");
}
//排序
void Sort_list(struct Node* head) {
	//对链表中的元素进行顺序排序
	struct Node* posFrontNode = head;
	struct Node* posNode;
	int t;
	for (posFrontNode = posFrontNode->next; posFrontNode != NULL; posFrontNode = posFrontNode->next) {
		for (posNode = posFrontNode->next; posNode != NULL; posNode = posNode->next) {
			if (posFrontNode->data > posNode->data) {
				t = posFrontNode->data;
				posFrontNode->data = posNode->data;
				posNode->data = t;
			}
		}
	}
}
//反转链表
struct Node* reverseList(struct Node* headNode) {
	struct Node* posFrontNode;
	//将第一个节点赋给posNode
	struct Node* posNode = headNode->next;
	//将传进来的链表变成空链表
	headNode->next = NULL;
	//此时的posNode代表传进来的链表(且从第一个链表开始)
	while (posNode != NULL) {
		posFrontNode = posNode;
		posNode = posNode->next;//此时的posNode代表posFrontNode后面的一个节点
		posFrontNode->next = headNode->next;
		headNode->next = posFrontNode;
	}
	return headNode;
}
//主函数
int main() {
	int n;
	struct Node* list_zheng = Create_head();
	struct Node* list_fu = Create_head();
	while (scanf("%d",&n)!=EOF) {
		struct Node* m = Create_node(n);
		if (n >= 0) {
			Insert_node(list_zheng, m);
		}
		else {
			Insert_node(list_fu, m);
		}
	}
	Sort_list(list_zheng);
	Sort_list(list_fu);
	Print_node(list_zheng);
	Print_node(list_fu);
	reverseList(list_zheng);
	reverseList(list_fu);
	Print_node(list_zheng);
	Print_node(list_fu);
	return 0;
}
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值