20230206 作业

作业

顺序表相关内容:

head.h

#ifndef head_h
#define head_h


#include <stdio.h>
#include <stdlib.h>

typedef int dt;

typedef struct Node{
	union{
		int len;
		dt data;
	};
	struct Node *last;
	struct Node *next;
}Node,*LoopDbLink;

LoopDbLink applyOne();

void *freeOne(void *one);

void *freeAll(LoopDbLink head);

void initHead(LoopDbLink head);

int isEmpty(LoopDbLink head);
int isInRange(LoopDbLink head,int pos);
LoopDbLink findOneByPos(LoopDbLink head,
		int pos);
LoopDbLink fineOneByData(LoopDbLink head,
		dt data);

int insertOneByIndex(LoopDbLink head,
		int pos,dt data);

int insertOne(LoopDbLink head,LoopDbLink last,
		LoopDbLink next,dt data);

void inputFromTail(LoopDbLink head,int n);

void outputInfo1(LoopDbLink head);

void inputFromHead(LoopDbLink head,int n);

void outputInfo2(LoopDbLink head);

void deleteFromHead(LoopDbLink head);
void deleteFromTail(LoopDbLink head);
int deleteOneByIndex(LoopDbLink db,int pos);
int deleteOneByData(LoopDbLink db,dt data);
void deleteOne(LoopDbLink head,LoopDbLink one);

void initList(LoopDbLink head,int n);
void JosephCircle(LoopDbLink head,int m);

void sortList(LoopDbLink head);

#endif

test.c

#include "head.h"
LoopDbLink applyOne(){
	LoopDbLink dbLink = (LoopDbLink)malloc(sizeof(Node));
	return dbLink;
}

void *freeOne(void *one){
	if(one != NULL)
		free(one);
	return NULL;
}

void *freeAll(LoopDbLink head){
	LoopDbLink one=head->next,t;
	while(one){
		t = one->next;
		printf("free node(%d)\n",one->data);
		one = freeOne(one);
		one = t;
		if(one == head)
			break;
	}
	printf("free head node\n");
	head = freeOne(head);
	return NULL;
}

void initHead(LoopDbLink head){
	head->len = 0;
	head->last = head;
	head->next = head;
}

int isEmpty(LoopDbLink head){
	if(head->len == 0)
		return 1;
	return 0;
}
int isInRange(LoopDbLink head,int pos){
	if(pos>=1 && pos<=head->len)
		return 1;
	return 0;
}
LoopDbLink findOneByPos(LoopDbLink head,int pos){
	if(pos > head->len || pos<=0)
		return NULL;
	LoopDbLink db = head;
	int i = 0;
	for(;i<pos;i++)
		db = db->next;
	printf("find node(%d)\n",db->data);
	return db;
}
LoopDbLink fineOneByData(LoopDbLink head,dt data){
	LoopDbLink node = head;
	int i=0;
	for(;i<head->len;i++){
		node = node->next;
		if(node->data == data)
			return node;
	}
	return NULL;
}

int insertOneByIndex(LoopDbLink head,int pos,dt data){
	LoopDbLink db = findOneByPos(head,pos);
	if(db == NULL)
		return 0;
	return insertOne(head,db->last,db,data);
}

int insertOne(LoopDbLink head,LoopDbLink last,LoopDbLink next,dt data){
	LoopDbLink dbLink = applyOne();
	if(dbLink == NULL){
		return 0;
	}
	last->next = dbLink;
	next->last = dbLink;
	dbLink->data = data;
	dbLink->last = last;
	dbLink->next = next;

	head->len++;
	return 1;
}

void inputFromTail(LoopDbLink head,int n){
	int i = 0;
	dt data;
	LoopDbLink last = head;
	for(;i<n;i++){
		scanf("%d",&data);
		insertOne(head,last,head,data);
		last = last->next;
	}
}

void initList(LoopDbLink head,int n){
	int i = 0;
	LoopDbLink last = head;
	for(;i<n;i++){
		insertOne(head,last,head,i+1);
		last = last->next;
	}
}

void inputFromHead(LoopDbLink head,int n){
	int i = 0;
	dt data;
	LoopDbLink next = head;
	for(;i<n;i++){
		scanf("%d",&data);
		insertOne(head,head,next,data);
		next = head->next;
	}
}

void deleteFromHead(LoopDbLink head){
	if(isEmpty(head))
		return;
	deleteOne(head,head->next);
}

void deleteFromTail(LoopDbLink head){
	if(isEmpty(head))
		return;
	deleteOne(head,head->last);
}

int deleteOneByIndex(LoopDbLink head,int pos){
	LoopDbLink db = findOneByPos(head,pos);
	if(db == NULL){
		return 0;
	}
	deleteOne(head,db);
	return 1;
}
int deleteOneByData(LoopDbLink head,dt data){
	LoopDbLink node = fineOneByData(head,data);
	if(node == NULL){
		printf("%d not found!\n",data);
		return 0;
	}
	deleteOne(head,node);
	return 1;
}
void deleteOne(LoopDbLink head,LoopDbLink one){
	if(head == NULL || one == NULL || isEmpty(head))
		return;
	LoopDbLink last = one->last;
	LoopDbLink next = one->next;
	next->last = one->last;
	last->next = one->next;
	one = freeOne(one);
	head->len--;
	if(head->len == 0){
		initHead(head);
	}
}

void outputInfo1(LoopDbLink head){
	printf("(顺时针):len=%d %p %p %p\n",head->len,head,head->last,head->next);
	LoopDbLink one = head;
	int i=0;
	for(;i<head->len;i++){
		one = one ->next;
		printf("data=%d address=%p last=%p next=%p\n",one->data,one,one->last,one->next);
	}
}

void outputInfo2(LoopDbLink head){
	printf("(逆时针):len=%d %p %p %p\n",head->len,head,head->last,head->next);
	LoopDbLink one = head;
	int i=0;
	for(;i<head->len;i++){
		one = one->next;
		printf("data=%d address=%p last=%p next=%p\n",one->data,one,one->last,one->next);
	}
}

void JosephCircle(LoopDbLink head,int m){
	printf("circle start:\n");
	int len = head->len;
	LoopDbLink node = head,last=head;
	int i,count;
	for(i=1;i<=len-1;i++){
		count = 0;
		while(count < m){
			if(node->next == head){
				node = node->next;
				continue;
			}
			last = node;
			node = node->next;
			count++;
		}
		printf("delete %d\n",node->data);
		deleteOne(head,node);
	}
	printf("winner is %d!\n",head->next->data);
}
void sortList(LoopDbLink head){
	dt data;
	LoopDbLink node = head->next,temp,min;
	while(node && node != head){
		min = node;
		temp = node->next;
		while(temp && temp != head){
			if(min->data > temp->data){
				min = temp;
			}
			temp = temp->next;
		}
		if(min != node){
			data = node->data;
			node->data = min->data;
			min->data = data;
		}
		node = node->next;
	}
}

main.c

#include "head.h"
int main(int argc, const char *argv[])
{
	LoopDbLink head = applyOne();
	if(head == NULL){
		printf("Head is NULL!\n");
		return -1;
	}else{
		initHead(head);
	}

	int n = 2;
	printf("inputFromTail(head,%d)\n",n);
	inputFromTail(head,n);
	outputInfo1(head);
	
	deleteFromHead(head);
	printf("deleteFromHead\n");
	outputInfo1(head);

	deleteFromTail(head);
	printf("deleteFromTail\n");
	outputInfo1(head);
	
	int n2 = 5;
	printf("inputFromHead(head,%d)\n",n2);
	inputFromHead(head,n2);
	outputInfo1(head);
	outputInfo2(head);
	
	printf("作业2实现双向链表按位置插入:\n");
	printf("insertOneByIndex(head,2,99)=%d\n",
			insertOneByIndex(head,2,99));
	outputInfo1(head);
	
	printf("deleteOneByIndex(head,2)=%d\n",
			deleteOneByIndex(head,2));
	outputInfo1(head);
	
	printf("作业1实现双向链表按元素删除:\n");
	printf("deleteOneByData(head,5)=%d\n",
			deleteOneByData(head,5));
	outputInfo1(head);

	printf("作业5 实现链表的简单排序(通用):\n");
	sortList(head);
	outputInfo1(head);

	printf("作业4删除链表中的所有节点:\n");
	head = freeAll(head);
	
	printf("作业3约瑟夫环问题:\n");
	int nn,mm;
	printf("请输入约瑟夫环n和m的值:\n");
	scanf("%d %d",&nn,&mm);
	LoopDbLink headx = applyOne();
	if(headx == NULL){
		return -1;
	}else{
		initHead(headx);
	}
	printf("初始化initList(head,%d):\n",nn);
	initList(headx,nn);
	outputInfo1(headx);
	
	JosephCircle(headx,mm);

	headx = freeAll(headx);
	
	return 0;
}

result:

inputFromTail(head,2)
1
2
(顺时针):len=2 0x55c029e77260 0x55c029e77ac0 0x55c029e77aa0
data=1 address=0x55c029e77aa0 last=0x55c029e77260 next=0x55c029e77ac0
data=2 address=0x55c029e77ac0 last=0x55c029e77aa0 next=0x55c029e77260
deleteFromHead
(顺时针):len=1 0x55c029e77260 0x55c029e77ac0 0x55c029e77ac0
data=2 address=0x55c029e77ac0 last=0x55c029e77260 next=0x55c029e77260
deleteFromTail
(顺时针):len=0 0x55c029e77260 0x55c029e77260 0x55c029e77260
inputFromHead(head,5)
1
2
3
4
5
(顺时针):len=5 0x55c029e77260 0x55c029e77ac0 0x55c029e77b20
data=5 address=0x55c029e77b20 last=0x55c029e77260 next=0x55c029e77b00
data=4 address=0x55c029e77b00 last=0x55c029e77b20 next=0x55c029e77ae0
data=3 address=0x55c029e77ae0 last=0x55c029e77b00 next=0x55c029e77aa0
data=2 address=0x55c029e77aa0 last=0x55c029e77ae0 next=0x55c029e77ac0
data=1 address=0x55c029e77ac0 last=0x55c029e77aa0 next=0x55c029e77260
(逆时针):len=5 0x55c029e77260 0x55c029e77ac0 0x55c029e77b20
data=5 address=0x55c029e77b20 last=0x55c029e77260 next=0x55c029e77b00
data=4 address=0x55c029e77b00 last=0x55c029e77b20 next=0x55c029e77ae0
data=3 address=0x55c029e77ae0 last=0x55c029e77b00 next=0x55c029e77aa0
data=2 address=0x55c029e77aa0 last=0x55c029e77ae0 next=0x55c029e77ac0
data=1 address=0x55c029e77ac0 last=0x55c029e77aa0 next=0x55c029e77260
作业2实现双向链表按位置插入:
find node(4)
insertOneByIndex(head,2,99)=1
(顺时针):len=6 0x55c029e77260 0x55c029e77ac0 0x55c029e77b20
data=5 address=0x55c029e77b20 last=0x55c029e77260 next=0x55c029e77b40
data=99 address=0x55c029e77b40 last=0x55c029e77b20 next=0x55c029e77b00
data=4 address=0x55c029e77b00 last=0x55c029e77b40 next=0x55c029e77ae0
data=3 address=0x55c029e77ae0 last=0x55c029e77b00 next=0x55c029e77aa0
data=2 address=0x55c029e77aa0 last=0x55c029e77ae0 next=0x55c029e77ac0
data=1 address=0x55c029e77ac0 last=0x55c029e77aa0 next=0x55c029e77260
find node(99)
deleteOneByIndex(head,2)=1
(顺时针):len=5 0x55c029e77260 0x55c029e77ac0 0x55c029e77b20
data=5 address=0x55c029e77b20 last=0x55c029e77260 next=0x55c029e77b00
data=4 address=0x55c029e77b00 last=0x55c029e77b20 next=0x55c029e77ae0
data=3 address=0x55c029e77ae0 last=0x55c029e77b00 next=0x55c029e77aa0
data=2 address=0x55c029e77aa0 last=0x55c029e77ae0 next=0x55c029e77ac0
data=1 address=0x55c029e77ac0 last=0x55c029e77aa0 next=0x55c029e77260
作业1实现双向链表按元素删除:
deleteOneByData(head,5)=1
(顺时针):len=4 0x55c029e77260 0x55c029e77ac0 0x55c029e77b00
data=4 address=0x55c029e77b00 last=0x55c029e77260 next=0x55c029e77ae0
data=3 address=0x55c029e77ae0 last=0x55c029e77b00 next=0x55c029e77aa0
data=2 address=0x55c029e77aa0 last=0x55c029e77ae0 next=0x55c029e77ac0
data=1 address=0x55c029e77ac0 last=0x55c029e77aa0 next=0x55c029e77260
作业5 实现链表的简单排序(通用):
(顺时针):len=4 0x55c029e77260 0x55c029e77ac0 0x55c029e77b00
data=1 address=0x55c029e77b00 last=0x55c029e77260 next=0x55c029e77ae0
data=2 address=0x55c029e77ae0 last=0x55c029e77b00 next=0x55c029e77aa0
data=3 address=0x55c029e77aa0 last=0x55c029e77ae0 next=0x55c029e77ac0
data=4 address=0x55c029e77ac0 last=0x55c029e77aa0 next=0x55c029e77260
作业4删除链表中的所有节点:
free node(1)
free node(2)
free node(3)
free node(4)
free head node
作业3约瑟夫环问题:
请输入约瑟夫环n和m的值:
6
5
初始化initList(head,6):
(顺时针):len=6 0x55c029e77260 0x55c029e77b40 0x55c029e77ac0
data=1 address=0x55c029e77ac0 last=0x55c029e77260 next=0x55c029e77aa0
data=2 address=0x55c029e77aa0 last=0x55c029e77ac0 next=0x55c029e77ae0
data=3 address=0x55c029e77ae0 last=0x55c029e77aa0 next=0x55c029e77b00
data=4 address=0x55c029e77b00 last=0x55c029e77ae0 next=0x55c029e77b20
data=5 address=0x55c029e77b20 last=0x55c029e77b00 next=0x55c029e77b40
data=6 address=0x55c029e77b40 last=0x55c029e77b20 next=0x55c029e77260
circle start:
delete 5
delete 4
delete 6
delete 2
delete 3
winner is 1!
free node(1)
free head node


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值