英文版数据结构算法题

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.Suppose we have a linked list.Eack node has three parts: one data field and two
linked fields.The list has been linked by the first linked field link1,but the list is disorder.Try your best to sort the list with the second linked field into an increasing list.
算法:
int creasort(struct node &head)
{
if (head.link1 == NULL) //没有结点
return ERROR;
head.link2 = head.link1; //设排列链表的第一个结点
head.link2->link2 = NULL;
p = head.link1->link1; //p为链表的第二个结点
q = head; //q代表p插入位置的前一个结点
if (!p) //只有一个结点
return OK;
while (p)
{
while ( q->link2 && (q->link2->data < p->data)) //找查找入位置
q = q->link2;
if (!q->link2) //插在最后
{
q->link2 = p;
}
else //插在队列中间
{
p->link2 = q->link2;
q->link2 = p;
}
p = p->link1;
q = head;
}
return OK;
}
2.Suppose we have a sequence with the terminal character @.The format of the
sequence can be S1&S2,S1 and S2 do not contain ‘&’, S2 is the reverse sequence of S1.
Try to write an algorithm to check the sequence if the format of the sequence obeys the above fules then the function return TRUE otherwise FALSE.
算法:
//s为待检查的字符串
int checkrules(char *s)
{
len = strlen(s);//用string.h自带函数取字符串s的长度
if (len % 2 == 0) //字符串是偶数
return FALSE;
i = 0; //i指向字符串第一个字符
n = len - 1;//n指向字符串最后一个字符
while (i < len / 2 && ( s[i] != '&' || s[n] != '&')) //对半劈
{
if (s[i] == s[n])
{
i++;
n--;
}
else
break;
}
if (i == len / 2 && s[i] == '&') //i指向中间元素且必须是’&’return TRUE;
else
return FALSE;
}
3.Suppose we have a double-linked ciroular list L.Eack node has four fields:two
linked fiels pre and next, one data field data and one frequency field freq.At the beginning the initial value of freq is 0 and after each opeation Locate(L,x),the value in frequency field of x plus 1,and then sort the list according to freq into a non-increasing list.The node which is most frequently accessed is the first node after the head node.
算法:
void Locate(struct doulin L, int x)
{
struct doulin *; //访问时用到的遍历指针
if (x > L.data) //无效的访问
return;
p = &L; //定位,p指向已经访问的指针
while(x--)
{
p = p->next;
}
p->freq++;
//因为形参L不在循环链表里,因此要用L.pre->next表示循环链表的头结点
while (p->pre ->freq < p->freq && p->pre != L.pre->next)
{
p->data <-> p->pre->data;
p->freq <-> p->pre->freq;
}
}
4.Suppose we have a disordered list of integers.The list has been stored in a stack.
You are supposed to sort the list with queue.
算法:
//表示将from队的元素倒到to中
void QueToQue(Queue *from, Queue *to, int ns)
{ tag = 0; //用于标注要排列的数是否进栈
while (!QueueEmpty(from))
{ DeQueue(from, &nq); //nq用于存取出队元素
if ((tag == 0 && nq > ns) || tag == 1) //不能入队
EnQueue(to, nq);
else //可以入队了
{ tag = 1; //设置标志tag为1,表示已经入队了
EnQueue(to, ns);EnQueue(to, nq);
}
}
if (tag == 0) //当ns比队中任何数都大时
EnQueue(to, ns);
}
//将队列from中的元素倒到to栈中
void QueToStack(Queue *from, Stack *to)
{ while (!QueueEmpty(from))
{ DeQueue(from, &nq);
Push(to, nq);
}
}
//排序的本体
void sort(Stack *S)
{ InitQueue(&Q1); InitQueue(&Q2); //初始化两个队列Q1,Q2
Pop(S, &ns); EnQueue(&Q1, ns); //先将一个元素出队进栈
while (!StackEmpty(S))
{ Pop(S,&ns);
if (!QueueEmpty(&Q1)) //若Q1队列不空(即Q2队列为空)
QueToQue(&Q1, &Q2, ns);
else //若Q2队列为空(即Q1队列不为空)
QueToQue(&Q2, &Q1, ns);
}
if (!QueueEmpty(&Q1)) //Q1不为空,也就是要把Q1队载入栈
QueToStack(&Q1, S);
else
QueToStack(&Q2, S);
}
5.颜色排列的
算法:
//colors是颜色数组,number是数组大小
void colorsort(int *colors, int number)
{
i = 0, j = number – 1;
while (colors[i] == 1) //找到第一个非1的单元
i++;
while (colors[j] == 3) //找到最后一个非3的单元
j--;
k = i; //遍历指针设为i
while(k <= j)
{
switch (colors[k])
{
case 1: colors[k]<->colors[i];
i++; k++;
break;
case 2: k++;
break;
case 3: colors[k]<->colors[j];
j--;
break;
}
}
}。

相关文档
最新文档