算法设计作业-单链表

 

有一个不带头结点的单链表L,设计一个算法删除所有值为x的结点。
要求:(1)建立单链表,并输出链表中的所有数据;
                  (键盘输入数据:88,66,100,90,100,65,78,100,85,100)
           (2)删除所有值为100的数据,并输出链表中的所有数据。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

typedef struct LNode
{
	int data;
	struct LNode *next;
}LNode;

LNode *CreatList(LNode *h,int t,int n)
{
	int x;
	LNode *s = (LNode *)malloc(sizeof(LNode));
	if(t>n) return h;
	scanf("%d",&x);
	s->data = x;
	s->next = h;
	h = s;
	h = CreatList(h,t+1,n);
	return h;
}
void Output(LNode *h)
{
	while(h!=NULL){
		printf("%d ",h->data);
		h =  h->next;
	}
	printf("\n");
}
/* 非递归写法 
LNode *Delete(LNode *now,int num)
{	 
	int flag = 0;
	LNode *pre,*nhead,*p; 
	pre = NULL;nhead = NULL;
	while(now != NULL){
		if(now->data == num){
			p = now;
			if(pre == NULL){
				now = now->next;
			}
			else{
				pre->next = now->next;
				now = now->next;
			}
			free(p);
		}
		else{
			if(flag == 0){
				nhead = now;
				flag = 1;
			}
			pre = now;
			now = now->next;
		}
	}
	return nhead;
}*/
LNode *Delete(LNode *pre,LNode *now,int num,LNode *nhead,int flag)
{
	LNode *p;
	if(now == NULL) return nhead;
	if(now -> data == num){
		p = now;
		if(pre == NULL){
			now = now->next;
			nhead = Delete(pre,now,num,nhead,flag);
		}
		else{
			pre->next = now->next; 
			now = now->next;
			nhead = Delete(pre,now,num,nhead,flag);
		}
		free(p);
	}
	else{
		if(flag == 0){
			nhead = now;
			flag = 1;
		}
		pre = now;
		nhead = Delete(pre,now->next,num,nhead,flag);	
	} 
	return nhead;
}
int main()
{
	int n,x,num;
	printf("输入数据的数量为:"); 
	scanf("%d",&n);
	printf("请输入n个数据:"); 
	scanf("%d",&x);
	LNode *h = (LNode *)malloc(sizeof(LNode));
	h->data = x;
	h->next = NULL;
	LNode *head = CreatList(h,2,n);
	printf("当前链表为:");
	Output(head);
	printf("请输入要删除的数据:");
	scanf("%d",&num);
//	LNode *nhead = Delete(head,num);
	LNode *nhead = Delete(NULL,head,num,NULL,0);
	Output(nhead);
}

// 88 66 100 90 100 65 78 100 85 100

对于含n(n>0)个结点的二叉树,所有结点值为int类型,设计一个算法由其先序序列a和中序序列b创建对应的二叉链存储结构。
要求:(1)建立二叉链表存储的二叉树,先序序列是:1,2,4,8,9,5,10,11,3,6,12,7;

                                                          中序序列是:8,4,9,2,10,5,11,1,12,6,3,7.
          (2)分别输出输出二叉树的先序、中序、后序序列。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

typedef struct BTNode
{
	int data;
	struct BTNode *lchild,*rchild;
}BTNode;

BTNode *CreatBTree(int a[],int b[],int n)
{
	int k;
	if(n<=0) return NULL;
	int root = a[0];
	BTNode *bt = (BTNode *)malloc(sizeof(BTNode));
	bt->data = root;
	for(k=0;k<n;++k){
		if(b[k]==root)
			break;
	}
	bt->lchild = CreatBTree(a+1,b,k);
	bt->rchild = CreatBTree(a+k+1,b+k+1,n-k-1);
	return bt;
}
void Output_DLR(BTNode *bt)
{
	if(bt==NULL) return ;
	printf("%d ",bt->data);
	Output_DLR(bt->lchild);
	Output_DLR(bt->rchild);
}
void Output_LDR(BTNode *bt)
{
	if(bt==NULL) return ;
	Output_LDR(bt->lchild);
	printf("%d ",bt->data);
	Output_LDR(bt->rchild);
}
void Output_LRD(BTNode *bt)
{
	if(bt==NULL) return ;
	Output_LRD(bt->lchild);
	Output_LRD(bt->rchild);
	printf("%d ",bt->data);
}
int main()
{
	int n = 12;
	int a[] = {1,2,4,8,9,5,10,11,3,6,12,7};
	int b[] = {8,4,9,2,10,5,11,1,12,6,3,7};
	BTNode *bt = CreatBTree(a,b,n);
	printf("先序:");Output_DLR(bt);printf("\n");
	printf("中序:");Output_LDR(bt);printf("\n");
	printf("后序:");Output_LRD(bt);printf("\n");
}

总结:递归中,返回上一层时一定要用东西接住,比如

nhead = Delete(pre,now,num,nhead,flag);

否则有时候可能正确,但是很容易出错误。

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值