2.4作业

程序代码:

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

typedef char datatype;

//定义双向链表结构体
typedef struct Node
{
	datatype data;          //数据域:存储数据元素
	struct Node *next;      //指针域:下一个节点的地址
	struct Node *prev;      //指针域:上一个节点的地址
}*DoubleLink;


//创建
DoubleLink create()
{
	DoubleLink s=(DoubleLink)malloc(sizeof(struct Node));
	if(s==NULL)
		return NULL;
	s->data=0;
	s->next=s->prev=NULL;
	return s;
}


//头插
DoubleLink insert_head(DoubleLink head,datatype element)
{
	//创建新节点
	DoubleLink s=create();
	s->data=element;
	//判断链表是否为空
	if(NULL==head)
		head=s;
	else
	{
		s->next=head;
		head->prev=s;
		head=s;
	}
	return head;
}


//尾插
DoubleLink insert_rear(DoubleLink head,datatype element)
{
	DoubleLink s=create();
    s->data=element;
	DoubleLink p=head;
	if(NULL==head)
	    head=s;
	else
	{
		while(p->next!=NULL)
		{
			p=p->next;
		}
		p->next=s;
		s->prev=p;	
	}
	return head;
}



//循环遍历
void output(DoubleLink head)
{
	//判断是否为空
	if(NULL==head)
	{
		puts("empty");
		return;
	}
	DoubleLink p=head;
	//正向输出
	while(p->next!=NULL)
	{
		printf("%c ",p->data);
		p=p->next;
	}
	printf("%c\n ",p->data);

	//逆向输出
//	while(p!=NULL)
//	{
//		printf("%c ",p->data);
//			p=p->prev;
//	}

	puts("");
}



//头删
DoubleLink delete_head(DoubleLink head)
{
	if(NULL==head)
		return head;
	else
	{
		DoubleLink del=head;
		head=del->next;
		free(del);
		del=NULL;
		head->prev=NULL;
		return head;
	}
	
}



//尾删
DoubleLink delete_rear(DoubleLink head)
{
	if(NULL==head)
		return head;
	else if(head->next==NULL)
	{
		free(head);
		head=NULL;
		return head;
	}
	else
	{
		DoubleLink p=head;
		while(p->next!=NULL)
		{
			p=p->next;
		}
		p->prev->next=NULL;
		free(p);
		p=NULL;
		return head;
	}

}




//按任意位置删除

DoubleLink delete_element(DoubleLink head,int pos,int n)
{
	if(NULL==head)
		return head;
	if(pos==1)
	{
		head=delete_head(head);
		return head;
	}
	if(pos==n)
	{
		head=delete_rear(head);
		return head;
	}
	int count=1;
	DoubleLink p=head;
	while(count!=pos)
	{
		p=p->next;
		count++;
	}
	p->prev->next=p->next;
	p->next->prev=p->prev;
	free(p);
	p=NULL;
	return head;
}




//按任意位置插入
DoubleLink insert_element(DoubleLink head,int pos,int n,datatype element)
{
	if(NULL==head)
		return head;
	if(pos==1)
	{
		head=insert_head(head,element);
		return head;
	}
	if(pos==n)
	{
		head=insert_rear(head,element);
		return head;
	}
	int count=1;
	DoubleLink p=head;
	DoubleLink s=create();
    s->data=element;
	while(count!=pos)
	{
		p=p->next;
		count++;
	}
	s->next=p;
	s->prev=p->prev;
	p->prev->next=s;
	p->prev=s;
	return head;
}


//按任意位置查找
DoubleLink find_element(DoubleLink head,int pos)
{
	if(NULL==head)
		return head;
	int count=1;
	DoubleLink p=head;
	while(count!=pos)
	{
		p=p->next;
		count++;
	}
	return p;
}





//按任意位置修改
DoubleLink change_element(DoubleLink head,int pos,datatype element)
{
	if(NULL==head)
		return head;
	int count=1;
	DoubleLink p=head;
	while(count!=pos)
	{
		p=p->next;
		count++;
	}
	p->data=element;
	return head;
}


int main(int argc, const char *argv[])
{
	DoubleLink head =NULL;
	int n;
	datatype element;
	printf("please input n:");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		printf("please input %d num:",i+1);
		scanf(" %c",&element);
//		head=insert_head(head,element);//头插
		head=insert_rear(head,element);//尾插
	}
	output(head);

	head=delete_head(head);//头删
	head=delete_rear(head);//尾删
	output(head);

	//按任意位置删除
	int pos;
	printf("please input delete pos:");
	scanf("%d",&pos);
	head=delete_element(head,pos,n);
	output(head);




	//按任意位置插入
	printf("please input insert pos:");
	scanf("%d",&pos);
	printf("please input insert element:");
	scanf(" %c",&element);
	head=insert_element(head,pos,n,element);
	output(head);



	//按任意位置查找
	printf("please input find pos:");
	scanf("%d",&pos);
	DoubleLink p=find_element(head,pos);
	printf("%c\n",p->data);
	output(head);



	//按任意位置修改
	printf("please input change pos:");
	scanf("%d",&pos);
	printf("please input change element:");
	scanf(" %c",&element);
	change_element(head,pos,element);
	output(head);

	return 0;
}

运行结果:

3.请简述栈和队列的区别

栈:只允许在表尾进行插入和删除的操作受限的线性表。在一端(栈顶)实现插入和删除,

逻辑结构:线性结构(一对一)

存储结构:顺序存储(顺序栈),链式存储(链栈)

栈的特点是先进后出

队列:只允许在表头删除,表尾插入操作受限的线性表。在两端(队头,队尾)实现插入和删除,

逻辑结构:线性结构(一对一)

存储结构:顺序存储(顺序队列,循环队列),链式存储(链式队列)

队列特点是先进先出 

4.请简述什么内存泄露?

     在释放空间时,指针位置并不在头部,指针之前的空间申请了内存空间,但没有释放,导致这部分内存无法再被其他程序使用。

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值