Study

栈学习小结

栈的概念

栈是一种特殊的线性表,只有一头能存入、输出数据,符合Last in First out原则。

栈的类型

有数组栈和链式栈两种。本次小结只关于数组栈。

栈的定义

动态数组栈

typedef int StackDataType; //将int重定义为StackDataType,方便更改动态数组的数据类型
typedef struct Stack
{
	StackDataType* array; //指向动态数组的指针
	int top; //表示栈顶
	int capacity; //栈的长度
}Sta; //重定义为Sta,方便调用

静态数组栈

typedef int StackDataType;
typedef struct Stack
{
	StackDataType array[N];
	int top ;
}Sta;

栈的初始化

void StackInit(Sta* sql)
{
	assert(sql);//防止空栈
	sql->array = NULL;
	sql->top = 0;
	sql->capacity = 0;
}

栈的销毁

void StackDestory(Sta* sql)
{
	assert(sql);
	free(sql->array);
	sql->array = NULL;
	sql->top = sql->capacity = 0;
}

判断栈是否为空

bool StackIsEmpty(Sta* sql) //返回值类型为bool,要引用stdbool.h
{
	assert(sql);
	if (sql->top == 0) //如果sql->top 为0,说明栈内没有数据
		return true;
	else
		return flase;
}

入栈

void StackPush(Sta* sql,StackDataType x)
{
	assert(sql);

	if(sql->top == sql->capacity)//判断是否需要扩容
	{
		int new_capacity = sql->capacity == 0 ? 4 : 2*sql->capacity;
		StackDataType* new_array = relloc(sql->array,sizeof(StackDataType) * new_capacity);
		if (new_array == NULL)//以防扩容失败
		{
			printf("relloc failed\n");
			exit(-1);
		}
		sql->array = new_array;
		sql->capacity = new_capacity;
	}
	sql->array[sql->top] = x;//将数组中下标为sql->top赋值为x
	sql->top++;//栈顶加一
}

出栈

void StackPop(Sta* sql)
{
	assert(sql);

	sql->top--;
}

打印栈顶数据

void StackTop (Sta* sql)
{
	assert(sql);
	while (sql->top != 0)
	{
		printf("%d\n", sql->arrqy[sql->top - 1]);//初始化sql->top为0时需要-1。
		sql->top--;
	}
}

返回栈的大小

StackDataType StackSize (Sta* sql)
{
	assert(sql);
	
	return sql->top;//初始化sql->top为0时直接,sql->top就是大小。
}
06-22
### StudyOS Introduction and Usage Guide StudyOS is not a widely recognized term in the standard IT or software development literature. However, based on the context provided, it may refer to either an educational operating system designed for learning purposes or a specific software framework tailored for studying operating systems or related technologies. Below is a detailed exploration of what StudyOS could represent, combining general knowledge about similar tools and frameworks. #### 1. Understanding StudyOS as an Educational Operating System An educational operating system (OS) like StudyOS is typically designed to help students and developers understand the inner workings of operating systems. Such systems are often minimalistic, allowing users to explore core concepts such as process management, memory allocation, file systems, and inter-process communication without the complexity of a full-fledged OS[^4]. For example, Minix is one of the most well-known educational operating systems, which was used by Andrew S. Tanenbaum to teach operating system design principles. If StudyOS follows a similar paradigm, it would likely include features such as: - A simplified kernel architecture. - Built-in debugging tools for tracing system calls. - Modular design for easy experimentation with different components. #### 2. StudyOS as a Learning Framework for Software Development Alternatively, StudyOS might be a software framework aimed at facilitating the study of operating systems through simulations or virtual environments. This aligns with the approach described in some references, where frameworks provide tools for running experiments and aggregating results[^2]. For instance: - **Simulation Tools**: These allow users to simulate various scenarios, such as CPU scheduling algorithms or memory paging techniques. - **Experimentation Platforms**: Users can test custom code snippets within a controlled environment, ensuring that changes do not affect the host system. If StudyOS incorporates elements from ROS (Robot Operating System), as described in another reference[^3], it might also include: - **Package Management**: Similar to how ROS organizes its components into packages, StudyOS could offer pre-built modules for studying specific OS functionalities. - **Documentation Support**: Comprehensive guides and tutorials to assist learners in understanding complex topics. #### 3. Practical Usage Guide for StudyOS To effectively use StudyOS, consider the following aspects: - **Installation**: Depending on whether StudyOS is an OS or a framework, installation procedures may vary. For an OS, you might need to set up a virtual machine using tools like VirtualBox or VMware[^5]. For a framework, downloading and configuring the necessary dependencies would suffice. - **Configuration**: Customize settings according to your learning objectives. For example, if focusing on process scheduling, enable relevant modules and disable others to reduce clutter. - **Experimentation**: Leverage built-in tools to conduct experiments. Use logging mechanisms to track behavior and analyze outcomes. Below is an example of setting up a simple experiment in a hypothetical StudyOS framework: ```python # Example: Simulating a Round-Robin Scheduler in StudyOS def round_robin_scheduler(processes, time_quantum): queue = processes[:] while queue: current_process = queue.pop(0) if current_process['remaining_time'] > time_quantum: current_process['remaining_time'] -= time_quantum queue.append(current_process) else: print(f"Process {current_process['name']} completed.") ``` #### 4. Conclusion While StudyOS is not explicitly defined in the provided references, it can be inferred as either an educational operating system or a learning framework for studying operating systems. Both interpretations emphasize simplicity, modularity, and practical experimentation to enhance understanding.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值