习题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();
}
有些地方感觉实现不好,等慢慢进步了再来优化吧。