数据结构第一次月考


值得一提的是在第三题的归并排序里面设置了一个dummy Node 可以让程序更加的简洁。


1 (30分)
typedef int Node_entry;

struct Node {
	Node_entry data;
	struct Node *next;
};

void delredundant(struct Node * head)
{
   struct Node *p,*q,*s;
   for(p=head;p!=0;p=p->next)
   {
     for(q=p->next,s=p;q!=0; )
     if (q->data==p->data) 
     {
          s->next=q->next; 
          free(q);
          q=s->next;
	 }
     else {
          s=q;         // s作为q的前一个节点的指针。
          q=q->next;
	 }
   }
}











2. (30分)
typedef int Node_entry;

struct Node {
	Node_entry data;
	struct Node *next;
};

1->2->3-> 4->5
void Inverse (struct Node ** head){ 
 // 如果用一个*的时候,则参数无法传递进来。
	struct Node *h=*head;
	struct Node *pre, *tmp;
    if(h==NULL)return;
	pre=NULL;
	tmp=h->next;
	while(tmp!=NULL){
		h->next=pre;
		pre=h;
		h=tmp;
		tmp=tmp->next;
	} 
	h->next=pre;
	*head=h;
}


















3. (40分)
typedef int Node_entry;

struct Node {
	Node_entry entry;
	struct Node *next;
};

struct Node * merge(struct Node *first, struct Node *second)
{
	struct Node *last_sorted; // points to the last node of sorted list
	struct Node combined;   // dummy first node, points to merged list
//这是一个哑节点
	last_sorted = &combined;  //有这样的一个节点可以为后面节省一些判断的语句!!!好聪明的方法啊!!!可以让代码变得简洁许多!!!
	while (first != NULL && second != NULL) { // Attach node with smaller key
		if (first->entry <= second->entry) {
			last_sorted->next = first;
			last_sorted = first;
			first = first->next; // Advance to the next unmerged node.
		} 
		else {
			last_sorted->next = second;
			last_sorted = second;
			second = second->next;
		}  
	}  
// After one list ends, attach the remainder of the other.
	if (first == NULL)
		last_sorted->next = second;
	else
		last_sorted->next = first;
	return combined.next;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值