寒假 day1

1、请简述栈区和堆区的区别?

2、有一个整形数组:int arr[](数组的值由外部输入决定),一个整型变量: x(也

由外部输入决定)。要求:
1)删除数组中与x的值相等的元素
2)不得创建新的数组
3)最多只允许使用单层循环
4)无需考虑超出新数组长度后面的元素,所以,请返回新数组的长度
例如: (1,2,3,5,7,3,5,9) x=3
原数组的有效部分变为:1,2,5,7,5,9)

cpp 复制代码
#include<stdio.h>
#include<string.h>	
#include<stdlib.h>
typedef int datatype; 
#define MAXSIZE 8
enum num
{
	FALSE=-1,
	SUCCESS
};
typedef struct List
{
	datatype data[MAXSIZE];
	int len;
}slist;
slist* create()
{
	slist *list=(slist*)malloc(sizeof(slist));
	if(list==NULL)
		return NULL;
	memset(list->data,0,sizeof(list->data));
	list->len=0;
	return list;
}
int full(slist *list)
{
	return list->len==MAXSIZE?FALSE:SUCCESS;
}
int insert_rear(datatype element,slist *list)
{
	if(NULL==list||full(list))
		return FALSE;
	list->data[list->len++]=element;
	return SUCCESS;
}
int empty(slist*list)
{
	return list->len==0?FALSE:SUCCESS;
}
int output(slist*list)
{
	if(NULL==list||empty(list))
		return FALSE;
	for(int i=0;i<list->len;i++)
	{
		printf("%-5d",list->data[i]);
	}
	puts("");
	return SUCCESS;
}
void det_index(slist*list,int index)
{
	if(NULL==list||empty(list)||index<0||index>list->len)
	return ;
	for(int i=index+1;i<list->len;i++)
	{
		list->data[i-1]=list->data[i];
	}
	list->len--;
}
void det_key(slist*list,datatype key)
{
	if(NULL==list||empty(list))
		return ;
	for(int i=0;i<list->len-1;i++)
	{
		
		if(list->data[i]==key)	
		{
			det_index(list,i);
			i--;
		}
	}
}
int main(int argc, const char *argv[])
{
	slist *list=create();
	int arr[MAXSIZE];
	for(int i=0;i<MAXSIZE;i++)
	{
		printf("please enter %d element:",i+1);
		scanf("%d",&arr[i]);
	}
	int len=sizeof(arr)/sizeof(arr[0]);
	for(int i=0;i<len;i++)
	{
		int flag=insert_rear(arr[i],list);
		if(flag==FALSE)
		{
			puts("NULL or full");
			break;
		}
	}
	int key;
	printf("please enter det key:");
	scanf("%d",&key);
	det_key(list,key);
	output(list);
	return 0;
}

3、请编程实现单链表的头插,头删、尾插、尾删

cpp 复制代码
#include<stdio.h>
#include<string.h>	
#include<stdlib.h>
enum{FALSE=-1,SUCCESS};
typedef int datatype;
//定义节点结构体
//节点:数据域、指针域
typedef struct Node
{
	//数据域:存储数据元素
	datatype data;
	//指针域:存储下一个节点的地址
	struct Node *next;
}*Linklist;
Linklist insert_head(Linklist head,datatype element);
Linklist create();
void output(Linklist head);
Linklist insert_rear(Linklist head,datatype element);
Linklist det_head(Linklist head);
Linklist det_rear(Linklist head);
int main(int argc, const char *argv[])
{
	Linklist head=NULL;
	int n;
	datatype element;
	printf("please enter n;");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		printf("please enter %d element:",i+1);
		scanf("%d",&element);
	//	head=insert_head(head,element);//头插
		head=insert_rear(head,element);//尾插
	}
	//遍历
	output(head);
	//头删
//	head=det_head(head);
//	output(head);
	//尾删
	head=det_rear(head);
	output(head);
    return 0;
}
//创建新节点
Linklist create()
{
	Linklist s=(Linklist)malloc(sizeof(struct Node));
	if(NULL==s)
		return NULL;
	s->data=0;
	s->next=NULL;
	return s;
}
//头插入
Linklist insert_head(Linklist head,datatype element)
{
	//创建新节点
	Linklist s=create();
	s->data=element;

	//判断链表是否为空
	if(NULL==head)
	{
		head=s;
	}
	else
	{
		s->next=head;
		head=s;
	}
	return head;
}
//遍历输出
void output(Linklist head)
{
	//判断链表是否为空
	if(NULL==head)
	{
		puts("error");
		return;
	}
	//输出
	Linklist p=head;
	while(p!=NULL)
	{
		printf("%d ",p->data);
		p=p->next;//后移
	}
	puts("");
}
//尾插
Linklist insert_rear(Linklist head,datatype element)
{
	//创建新节点
	Linklist s=create();
	s->data=element;

	//判断链表是否为空
	if(NULL==head)
	{
		head=s;
	}
	else //存在多个链表
	{
		Linklist p=head;
		while(p->next!=NULL)
		{
			p=p->next;
		}
		p->next=s;
	}
	return head;
}
//头删
Linklist det_head(Linklist head)
{
	//判断链表是否为空
	if(NULL==head)
		return head;
	//存在多个节点 >=1
	Linklist del=head;
	head=head->next;
	free(del);
	del=NULL;
	return head;
}
//尾删
Linklist det_rear(Linklist head)
{
	//判断链表是否为空
	if(NULL==head)
		return head;
	//一个节点
	else if(head->next==NULL)
	{
		free(head);
		head=NULL;
		return head;
	}
	//多个节点 >=2
	else
	{
		Linklist del=head;
		while(del->next->next!=NULL)
		{
			del=del->next;
		}
		free(del->next);
		del->next=NULL;
		return head;
	}
}
相关推荐
浩浩测试一下34 分钟前
Windows 与 Linux 内核安全及 Metasploit/LinEnum 在渗透测试中的综合应用
linux·运维·windows·web安全·网络安全·系统安全·安全架构
188_djh4 小时前
# 电脑每次重启都自动启动惠普安装程序,怎么取消?
windows·任务管理器·惠普打印机·msconfig·hpsmartinstall·安装程序自动启动·系统配置工具
甜甜不吃芥末5 小时前
Windows 应用程序的 UI 框架:WPF、WinUI 3 和 UWP的差异区别
windows·ui·wpf
ljh57464911914 小时前
Airtest 的 Poco 框架中,offspring()
windows
斜月三1 天前
windows部署多实例filebeat监控相同路径下文件
windows·filebeat
尽兴-1 天前
如何将多个.sql文件合并成一个:Windows和Linux/Mac详细指南
linux·数据库·windows·sql·macos
chilavert3181 天前
技术演进中的开发沉思-30 MFC系列:五大机制
c++·windows
荔枝吻2 天前
【保姆级喂饭教程】Windows下安装Git Flow
windows·git·git flow
深盾科技2 天前
深入解析 .NET 泛型:从原理到实战优化
windows·.net
石头wang2 天前
如何在idea里快速地切换Windows CMD、git bash、powershell
windows·git·bash·intellij-idea