数据结构与算法C语言描述 第三章练习

    习题3.2 3.4 3.5 ; 3.5的实现是错误的,后面再来改吧,因为部分习题使用的是相同的数据结构,所以就把他们都放在一起,这里的基本数据结构是 循环链表

   头文件 包含基本的数据结构实现 以及习题里所要求实现的函数

  

#ifndef CHAIN_
#define CHAIN_
struct ChainNode;
struct listStruct;
typedef ChainNode *Node;
typedef listStruct *list;
struct ChainNode
{
	float element;
	Node next;
	Node Previous;
};
//the whole struct of the list
struct listStruct
{
	Node header;
	int word;
	int maxWord;
};

Node Find(float item,list l);
void Delete(list l);
//return 1,successed.return 0,failed
int Add(float item,list l);
//return 1,successed.return 0,failed
int Lnitial(list l,int max);
Node SearchItem(float item,list l);
void PrintAll(list l);
/*3.2 打印由L中指定的位置上的元素*/
void Print(list l,int*p);
/* exercise 3.5 给定两个已排序的表 L1 L2,只使用基本的表操作编写计算L1并L2*/
void Union(list l1,list l2);
/*exercise 3.4 intersect 给定已排序的链表,只使用基本的表操作编写计算L1交L2*/
list Intersect(list l1,list l2);
#endif

具体实现

#include "chain.h"
#include"stdio.h"
#include "stdlib.h"

//return 1,successed.return 0,failed
int Lnitial(list l,int max)
{
	Node pnode;
	pnode=(Node)malloc(sizeof(ChainNode));
	if(pnode==NULL)
	{
		printf("\nerror to get space");
		getchar();
		return 0;
	}
	//called sildier
	pnode->next=pnode;
	pnode->Previous=pnode;
	l->header=pnode;
	l->maxWord=max;

	l->word=0;

	return 1;
}
//return 1,successed.return 0,failed

int Add(float item,list l)
{
	Node pnode;
	pnode=(Node)malloc(sizeof(ChainNode));
	if (pnode==NULL)
	{
		return 0;
	}
	(l->word)++;
	if (l->word>l->maxWord) 
	{
			(l->word)--;
			return 0;
	}
	pnode->element=item;
	//key points,intepret this,intepret all,how to operate a header
	//point to header
	//set pnode
	pnode->next=l->header;
	pnode->Previous=l->header->Previous;
	//set previous item of pnode
	l->header->Previous->next=pnode;
	l->header->Previous=pnode;//point to the new node in that set in this function
    return 1;		
}

Node SearchItem(float item,list l)
{
	Node curser;
	if(l->word==0)
		return (Node)0;
	curser=l->header->next;
	while(curser->next!=l->header)
	{
		printf("\nheh");
		if(curser->element==item)
			return curser;
		curser=curser->next;
	}
	
}
//exercise 3.2
void Print(list l,int*p)
{
	
	int counter;
	while(*p!=NULL)
	{
		Node cursor=l->header->next;
		counter=0;
		while(cursor->next!=l->header)
		{
			if(counter==*p)
			{
				printf("\n%f",cursor->element);
				break;
			}
			counter++;
			cursor=cursor->next;
		}
		p++;
	}
}

void PrintAll(list l)
{
	Node cursor=l->header->next;
	while(cursor->next!=l->header)
	{
		printf("\n%f",cursor->element);
        	 cursor=cursor->next;
	}
}


void Union(list l1,list l2)
{
	l1->header->Previous->next=l2->header->next;
	l2->header->Previous->next=l1->header;
	//l1->header->Previous=l2->header->next;

}
list Intersect(list l1,list l2)
{
	list l3=(list)malloc(sizeof(listStruct));
	Lnitial(l3,100);
	Node cursor1=l1->header->next;
	Node cursor2=l2->header->next;
	if(l1->word<l2->word)
	{
		
		while (cursor1->next!=l1->header) {	
			if (cursor1->element==cursor2->element) {
				Add(cursor1->element,l3);
			}
			cursor1=cursor1->next;
			cursor2=cursor2->next;
			printf("\n:heh");
		}
	}
	else
	{
			
		while (cursor2->next!=l2->header) {	
			if (cursor1->element==cursor2->element) {
				Add(cursor1->element,l3);
			}
			cursor1=cursor1->next;
			cursor2=cursor2->next;
			printf("\n:heh");
		}
	}
	PrintAll(l3);
	getchar();
	return l3;
	

}


测试

#include "chain.h"
#include "stdlib.h"
#include"stdio.h"
int main()
{
	list l1=(list)malloc(sizeof(listStruct));
	Lnitial(l1,100);
	for(int i=5;i<10;i++)
	{
		Add(i,l1);
	}
	list l2=(list)malloc(sizeof(listStruct));
	Lnitial(l2,100);
	for(int j=5;j<20;j++)
	{
		Add(j,l2);
	}
	Intersect(l1,l2);
	
	/*test3.4 done
	list l1=(list)malloc(sizeof(listStruct));
	Lnitial(l1,100);
	for(int i=0;i<10;i++)
	{
		Add(i,l1);
	}
	list l2=(list)malloc(sizeof(listStruct));
	Lnitial(l2,100);
	for(int j=10;j<20;j++)
	{
		Add(j,l2);
	}
   Union(l1,l2);
   PrintAll(l1);*/
	

	/*3.2 test done
   int *p=new int[4];
   p[0]=3;
   p[1]=67;
   p[2]=33;
   p[3]=54;
   Print(l,p);
   //printf("cao? %f,%f,%f",pnode->element,pnode->next->element,pnode->Previous->element);
   //PrintAll(l);*/
   
	
   
   getchar();
	

}


   有些地方感觉实现不好,等慢慢进步了再来优化吧。

   

 

1. 经过以下栈运算后,x的值是( )。 InitStack(s); Push(s,'a'); Push(s,'b'); Pop(s,x); Gettop(s,x); A. a B. b C. 1 D. 0 2.循环队列存储在数组A[0..m]中,则入队时的操作为( )。 A.rear=rear+1 B. rear=(rear+1) mod(m-1) C. rear=(rear+1)mod m D. rear=(rear+1) mod(m+1) 3. 栈和队列的共同点是( )。 A.都是先进先出 B.都是先进后出 C.只允许在端点处插入和删除元素 D.没有共同点 4. 若用一个大小为6的数组来实现循环队列,且当 rear 和 front 的值分别为 0 和 3。当从队列中删除一个元素,再插入两个元素后,rear 和 front 的值分别为:( )。 A.1 和 5 B.2 和 4 C.4 和 2 D.5 和 1 5. 程序填顺序循环队列的类型定义如下: typedef int ET; typedef struct{ ET *base; int Front; int Rear; int Size; }Queue; Queue Q; 队列 Q 是否“满”的条件判断为( C )。 A.(Q.Front+1)=Q.Rear B.Q.Front=(Q.Rear+1) C.Q.Front=(Q.Rear+1)% Q.size D.(Q.Front+1) % Q.Size=(Q.Rear+1)% Q.size 6. 若进栈序列为1,2,3,4,进栈过程中可以出栈,则( )不可能是一个出栈序列。 A.3,4,2,1 B.2,4,3,1 C.1,4,2,3 D.3,2,1,4 7. 向顺序存储的循环队列 Q 中插入新元素的过程分为三步: ( )。 A.进行队列是否空的判断,存入新元素,移动队尾指针 B.进行队列是否满的判断,移动队尾指针,存入新元素 C.进行队列是否空的判断,移动队尾指针,存入新元素 D.进行队列是否满的判断,存入新元素,移动队尾指针 8. 关于栈和队列,( )说法不妥。 A. 栈是后进先出表 B. 队列是先进先出表 C. 递归函数在执行时用到栈 D. 队列非常适用于表达式求值的算符优先法 9. 若用数组S[0..m]作为两个栈S1和S2的共同存储结构,对任何一个栈,只有当S全满时才不能作入栈操作。为这两个栈分配空间的最佳方案是( )。 A.S1的栈底位置为0,S2的栈底位置为m B.S1的栈底位置为0,S2的栈底位置为m/2 C.S1的栈底位置为1,S2的栈底位置为m D.S1的栈底位置为1,S2的栈底位置为m/2 二、程序填空题(没特别标注分数的空的为3分,共 23 分)。 1.下面的算法是将一个整数e压入堆栈S,请在空格处填上适当的语句实现该操作。 typedef struct{ int *base; int *top; int stacksize; }SqStack; int Push(SqStack S,int e) { if ( S.top- S.base>= S.stacksize ) { S.base=(int *) realloc(S.base,(S.stacksize+1)*sizeof(int)); if( !S.base ) { printf(“Not Enough Memory!\n”); return(0); } S.top= S.base+ S.stacksize ; S.stacksize= S.stacksize+1 ; } * S.top++=e ; return 1; } 2. 在表达式:6+5+3*7/(4+9/3-2)求值过程中,处理到2时刻,运算符栈的状态为: + / ( - (4分),操作数栈的内容为11,21,7,2(4分)。 3. 递调用时,处理参数及返回地址,要用一种称为 栈 的数据结构。 4. 设循环队列中数组的下标范围是1-n,其头尾指针分别为f和r,则其元素个数为_(r-f+n) mod n。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值