c++ 实训 链表8 9 10

第一 线性表应用一:栈

我们链表表现栈时,既可以将表头定为栈顶,也可以将表尾定为栈顶。我在这关中定的是前者

#include "mstack.h"
// 函数empty:判断栈sk是否为空
// 参数:sk-栈
// 返回值:true-sk为空,false-sk不为空
bool empty(intStack sk)
{
    // 请在此添加代码,补全函数empty
    /********** Begin *********/
	intStack  p=sk;
	if(p==NULL){
		return true;
	}
	return false;

    /********** End **********/
}
// 函数pop:弹栈
// 参数:sk-栈,传引用,弹栈可能会改变sk的值
// 返回值:弹栈的弹出的整数,如果栈空,返回-1
int pop(intStack &sk)
{
    // 请在此添加代码,补全函数pop
    /********** Begin *********/
    intStack  p=sk;
	if(p==NULL)return -1;
	int n;
	n=p->data;
	sk=p->next;//让sk的只发生改变
	delete p;
	return n;
    /********** End **********/
}
// 函数push:压栈,将整数n压入栈sk中
// 参数:sk-栈,传引用,压栈会改变sk的值,n-要压栈的整数
// 返回值:无,采用链表实现栈,只要还有内存,压栈都会成功
void push(intStack &sk, int n)
{
    // 请在此添加代码,补全函数push
    /********** Begin *********/
	intStack p=sk;
	if (sk==NULL){
		sk=new node;
		sk->data=n;
		sk->next=NULL;
	}else{
		intStack q=new node;
		q->data=n;
		q->next=p;
		sk=q;
	}
    /********** End **********/
}

第二 线性表应用二:队列

我将表头顶为队头,表尾定义为队尾。

#include "mqueue.h"

// 函数queueEmpty:判断队列iq是否为空
// 参数:iq-整数队列
// 返回值:true-队列iq为空,false-iq不为空
bool queueEmpty(intQueue iq)
{
    // 请在此添加代码,补全函数queueEmpty
    /********** Begin *********/ 
    if(iq==NULL) return true;
	else return false;
    /********** End **********/
}
// 函数enQueue:将整数num入列到iq
// 参数:iq-整数队列,传引用,入列有可能改变队列头指针,num-入列的整数
// 返回值:无,只要还有内存,入列总会成功
void enQueue(intQueue &iq, int num)
{
    // 请在此添加代码,补全函数enQueue
    /********** Begin *********/
    intQueue p=iq;
	if(p==NULL){
		iq=new node;
		iq->data=num;
		iq->next=NULL;
	}else{
		while(p->next){
			p=p->next;
		}
		intQueue q=new node;
		q->data=num;
		q->next=NULL;
		p->next=q;
	}


    /********** End **********/
}
// 函数deQueue:出列
// 参数:iq-整数队列,传引用,出列有可能改变队列头指针
// 返回值:出列结点的数据,如果队列为空,返回-1
int deQueue(intQueue &iq)
{
    // 请在此添加代码,补全函数deQueue
    /********** Begin *********/
	intQueue p=iq;
	if (p==NULL){
		return -1;
	}
	int n=p->data;
	iq=p->next;
	delete p;
	return n;
    /********** End **********/
}

第三 线性表应用三:集合

注意集合的无序性 不相同性

#include "mset.h"

// 函数unionSet:求集合a和b的并集
// 参数:a-集合,b-集合
// 返回值:集合(集合a和b的并集)
intSet unionSet(intSet a, intSet b)
{
    // 请在此添加代码,补全函数unionSet
    /********** Begin *********/
	//创造出一个新的p,不改变a,b,以便接下来的运算正确
	intSet p=NULL,ap=a,bp=b;
	if (a==NULL)return b;
	if (b==NULL)return a;
	//将a中的元素有序插入p中
	while(ap){
		//可以直接调用的函数在LinearList.h中有
		if (search(p, ap->data) == NULL){//注意集合的无序性
		intSet t=new node;//创造出一个节点
		t->data=ap->data;
		t->next=NULL;
		p=insertSort(p,t);//可以直接调用
		}
		ap=ap->next;
	}
	//将b中的元素有序插入p中
	while(bp){
		if (search(p, bp->data)==NULL){
		intSet t=new node;
		t->data=bp->data;
		t->next=NULL;
		p=insertSort(p,t);
		}	
		bp=bp->next;
	}
	return p;
}
// 函数intersection:求集合a和b的交集
// 参数:a-集合,b-集合
// 返回值:集合(集合a和b的交集)
intSet intersection(intSet a, intSet b)
{
    // 请在此添加代码,补全函数intersection
    /********** Begin *********/
    if(a==NULL || b==NULL)return NULL;
	intSet p=NULL,acur=a,bcur=b;
	//遍历b,将b中的元素在a中组个查找
	while(bcur){
		if(search(acur,bcur->data)){
			intSet t=new node;
			t->data=bcur->data;
			t->next=NULL;
			p=insertSort(p,t);
	    }
		acur=a;
		bcur=bcur->next;
	}
	return p;


    /********** End **********/
}
// 函数addElement:在集合is中增加元素num
// 参数:is-集合,num-要增加的元素
// 返回值:无
void addElement(intSet &is, int num)
{
    // 请在此添加代码,补全函数addElement
    /********** Begin *********/
	intSet t=new node;
	t->data=num;
	t->next=NULL;
	if (search(is, num) == NULL) is = insertSort(is, t);

    /********** End **********/
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值