东北大学2014数据结构大题

//第一题 
//指针指向单链表的首结点,从表la中删除第i个元素起共len个元素(带头结点) 
#include<stdio.h>
#include<stdlib.h>

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {} 
}*LinkList;


ListNode* del(ListNode *head,int i,int len)
{
	ListNode *p = head->next;
	ListNode *pre = head;
	while(i!=1 && i > 0)
	{
		pre = pre->next;
		p = p->next;
		--i;
	}
	while(len!=1 && len > 0)
	{
		p = p->next;
		--len; 
	}
	pre->next = p->next;
	return head;
}
int main()
{
	ListNode *head = (ListNode*)malloc(sizeof(ListNode));
	ListNode *b = new ListNode(4);
	ListNode *c = new ListNode(8);
	ListNode *d = new ListNode(2);
	ListNode *e = new ListNode(9);
	head->next = b;
	b->next = c;
	c->next = d;
	d->next = e;
	ListNode *p = del(head,1,1);
	p = p->next;
	while(p)
	{
		printf("%d ",p->val);
		p = p->next;
	}
    return 0;
}


//第二题
//在二叉树中查找值为X的结点  并求该结点在树中的层数 
int search(BiTree t,int x,int count)
{
	BiTree p;
	p = t;
	if(p == nullptr)
		return 0;
	if(x == p->data)
	{
		return count;
	}else
	{
		int l = search(p->lchild,x,count+1);
		if(l != 0)
			return l;
		else
			return search(p->rchild,x,count+1);
	}
}

posted @ 2020-09-02 19:27  Akmf's_blog  阅读(131)  评论(0)    收藏  举报