两道数据结构的算法题,本人数据结构不给力。求各位大侠指点。在此先谢过了。上代码直接接分。。。

Mr_邪 2013-04-22 09:31:54
写出单链表的前插操作,将值为x的结点插入到值为y的结点之前,要求写出单链表的定义和前插操作的函数(其中包括找到要插入结点的位置)。


对有序表R进行折半查找,成功时返回记录在表中的位置,失败时返回0。(要求写出顺序表R结构体的定义,在表中查找关键字为k的结点)
...全文
264 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
honey_fansy 2013-04-27
  • 打赏
  • 举报
回复
#include <iostream>

using namespace std;

typedef struct node
{
	int data;
	node *next;
}*link, node;

node *preInsert(link head, int x, int y)
{
	link temp = head;
	while (temp && temp->next->data != y)
	{
		temp = temp->next;
	}
	if (temp)
	{
		node *insert = new node;
		if (!insert)
		{
			cout << "内存分配错误" << endl;
			exit(0);
		}
		insert->data = x;
		insert->next = temp->next;
		temp->next = insert;
	}
	return temp->next;
}

int main()
{
	link temp;
	node *head = new node;
	node *first = new node;
	node *second = new node;
	head->data = 1;
	head->next = first;
	first->data = 2;
	first->next = second;
	second->data = 4;
	second->next = NULL;
	temp = head;
	node *current = preInsert(temp, 3, 4);
	while (current)
	{
		cout << current->data << endl;
		current = current->next;
	}
	system("pause");
	return 0;
}
zhaokai115 2013-04-26
  • 打赏
  • 举报
回复
这个代码也可以?

int main() { printf("Hello, World!"); }
honey_fansy 2013-04-26
  • 打赏
  • 举报
回复
引用 2 楼 leizh007 的回复:
//题2
typedef struct link
{
	ElemType data;
}link;
int search(link a[],int m,int n,Elemtype x)
{
	int i=m,j=n;
	if(m>n)
		return -1;
	else
	{
		k=(i+j)/2;
		if(a[k]>x)
			return search(a,k+1,n,x);
		else
			if(a[k]==x)
				return k;
			else
				return search(a,m,k-1,x);
	}
}
第一:a[k]与x的类型不一样,比较不了。 第二:折半查找用二分法不是更好吗?何须递归呢?
lxy15329 2013-04-24
  • 打赏
  • 举报
回复
这个貌似是数据结构中比较基础的题目吧
leizh007 2013-04-23
  • 打赏
  • 举报
回复
//题2
typedef struct link
{
	ElemType data;
}link;
int search(link a[],int m,int n,Elemtype x)
{
	int i=m,j=n;
	if(m>n)
		return -1;
	else
	{
		k=(i+j)/2;
		if(a[k]>x)
			return search(a,k+1,n,x);
		else
			if(a[k]==x)
				return k;
			else
				return search(a,m,k-1,x);
	}
}
leizh007 2013-04-23
  • 打赏
  • 举报
回复
//题1
typedef struct linknode
{
	ElemType data;
	linknode *next;
}*linklist,linknode;
linklist inseart(linklist head,Elemtype x,Elemtype y)
{
	linklist p=head;
	while(p->next&&p->next->data!=y)
		p=p->next;
	if(p->next)
	{
		linklist q=(linklist)malloc(sizeof(linknode));
		q->data=x;
		q->next=p->next;
		p->next=q;
	}
	return head;
}

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧