Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

韩康泽 链表倒序 宿舍管理系统(未使用文件) #20

Open
daAzai opened this issue Nov 28, 2018 · 1 comment
Open

韩康泽 链表倒序 宿舍管理系统(未使用文件) #20

daAzai opened this issue Nov 28, 2018 · 1 comment
Labels
修改 代码出现问题,请修改后重新提交代码

Comments

@daAzai
Copy link

daAzai commented Nov 28, 2018

2.通过指针将一个链表倒序

#include<stdio.h>
#include<stdlib.h>
struct Data {
	char c;
	struct Data *next;
};

int n;

struct Data *creat1() {
	struct Data *head;
	struct Data *p1, *p2;
	n = 0;
	p1 = p2 = (struct Data *) malloc (sizeof (struct Data));
	scanf("%c", &p1->c);
	head = NULL;
	while (p1->c != '0') {
		n = n + 1;
		if (n == 1) {head = p1;}
		else {p2->next = p1;}
		p1->next = NULL;
		p2 = p1;
		p1 = (struct Data *) malloc (sizeof (struct Data));
		scanf("%c", &p1->c);
	}
	p2->next = NULL;
	return (head);
}

struct Data *creat2(struct Data *head) {
	struct Data *p1, *p2 = NULL;
	while (head) {
		p1 = head->next;
		head->next = p2;
		p2 = head;
		head = p1;
	}
	return p2;
}

void print(struct Data *head) {
	struct Data *p;
	p = head;
	if (head != NULL)
		do {
			printf("%c", p->c);
			p = p->next;
		}
		while(p != NULL);
}

int main() {
	struct Data *head;
	head = creat1();
	print(head);
	printf("\n");
	head = creat2(head);
	print(head);
	printf("\n");
	return 0;
}

3.宿舍管理系统(未使用文件)

#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct Student)

struct Student
{
	char name[20];
	int num;
	char sex[10];
	int age;
	int dormnum;
	int bednum;
	struct Student *next;
};
int n;

struct Student *creat ()
{
	struct Student *head;
	struct Student *p1, *p2;
	n = 0;
	p1 = p2 = (struct Student *) malloc (LEN);
	scanf("%s%d%s%d%d%d", p1->name, &p1->num, p1->sex, &p1->age, &p1->dormnum, &p1->bednum);
	head = NULL;
	while (p1->name[20] != '0')
	{
		n = n + 1;
		if (n == 1) head = p1;
		else p2->next = p1;
		p2 = p1;
		p1 = (struct Student *) malloc (LEN);
		scanf("%s%d%s%d%d%d", p1->name, &p1->num, p1->sex, &p1->age, &p1->dormnum, &p1->bednum);
	}
	p2->next = NULL;
	return(head);
}

struct Student *del(struct Student *head,int num)
{
	struct Student *p1, *p2;
	if (head == NULL)
	{
		printf("\nlist null!\n");
		return(head);
	}
	p1 = head;
	while(num != p1->num && p1->next != NULL)
	{
		p2 = p1;
		p1 = p1->next;
	}
	if (num == p1->num)
	{
		if (p1 == head) head = p1->next;
		else p2->next = p1->next;
		printf("delete:%d\n", num);
		n = n - 1;
	}
	else printf("%d not been found!\n", num);
	return(head);
}

struct Student *insert(struct Student *head,struct Student *stud)
{
	struct Student *p0, *p1, *p2;
	p1 = head;
	p0 = stud;
	if(head ==  NULL)
	{
		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 (head == p1) head = p0;
			else p2->next = p0;
		}
		else
		{
			p1->next = p0;
		}
		p0->next = p1;
	}
	n = n +1;
	return(head);
}

void print(struct Student *head)
{
	struct Student *p;
	printf("\nNow,These %d records are:\n", n);
	p = head;
	if (head != NULL)
		do
		{
			printf("%s,%d,%s,%d,%d,%d", p->name, p->num, p->sex, p->age, p->dormnum, p->bednum);
			p = p->next;
		}
		while(p != NULL);
}

struct Student *demand(struct Student *head,int num)
{
	struct Student *p;
    p = head->next;
    while( p != NULL && p->num != num)
    p = p->next;
return (p);
}

int main()
{
	struct Student *head,stu;
	int del_num, dam_num;
	printf("input records:\n");
	head = creat();
	print(head);
	printf("input the delete number:");
	scanf("%d", &del_num);
	head = del(head,del_num);
	print(head);
	printf("input the inserted records:");
	scanf("%s,%d,%s,%d,%d,%d", &stu.name, &stu.num, &stu.sex, &stu.age, &stu.dormnum, &stu.bednum);
	head = insert(head,&stu);
	print(head);
	printf("input the demand number:");
	scanf("%d", &dam_num);
	head = del(head,dam_num);
	print(head);
	return 0;
}
@wmpscc
Copy link
Member

wmpscc commented Dec 7, 2018

点评:

  • 通过指针将一个链表倒序
    • 将代码改为从键盘输入任意长度数据,将其倒序。
  • 宿舍管理系统
    • 无法结束输入
    • 其他功能未验证
    • 请根据题目要求完善功能,请修改!

修改!

@wmpscc wmpscc added the 修改 代码出现问题,请修改后重新提交代码 label Dec 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
修改 代码出现问题,请修改后重新提交代码
Projects
None yet
Development

No branches or pull requests

2 participants