📄 jihe.cpp
字号:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct LNode{
int data;
struct LNode *next;
}*Link;
typedef struct{
Link head;
int len;
}LinkList;
typedef LinkList LinkSet;
int InitList(LinkList *L){
Link p;
p=(Link)malloc(sizeof(LNode));
if(!p){
printf("\nNo enough memory to alloc.\n");
return 0;
}
L->head=p;
L->len=0;
p->next=NULL;
return 1;
}
int IsElement(LinkList *L,int e){
Link p;
p=L->head->next;
if(!p||(L->len==0)) return 0;
while(p){
if(p->data==e) return 1;
p=p->next;
}
return 0;
}
void Append(LinkList *L,int e){
Link s,p,q;
p=L->head;
s=(Link)malloc(sizeof(LNode));
s->data=e;
if(L&&s){
if(!p->next){
p->next=s;
s->next=NULL;
L->len++;
return;
}
else{
if(!IsElement(L,e)){
p=p->next;
q=p->next;
if((p->data)>e){
s->next=p;
L->head->next=s;
L->len++;
return;
}
while((p->data)<e){
if(q){
if((q->data)>e){
s->next=q;
p->next=s;
L->len++;
return;
}
else{
p=p->next;
q=q->next;
continue;
}
}
else{
p->next=s;
s->next=NULL;
L->len++;
return;
}
}
}
else printf("\nThe number %d has already existed.\n",e);
return;
}
}
}
void Delete(LinkList *L,int e){
Link p,q;
q=L->head;
p=q->next;
if(!IsElement(L,e)){
printf("\nThe number %d doesn't exist.\n",e);
return;
}
while(p){
if(p->data==e) break;
q=q->next;
p=q->next;
}
q->next=p->next;
free(p);
}
void CreateSet(LinkSet *T){
int i,NUM;
int s[255];
printf("\nInput how many numbers you want to enter.\n");
scanf("%d",&NUM);
printf("\nPlease input the numbers.\n");
for(i=0;i<NUM;i++){ scanf("%d",&s[i]);}
if(T){
for(i=0;i<NUM;i++)
Append(T,s[i]);
}
}
void Union(LinkSet *S1,LinkSet *S2)
{
Link p2;
if(!S1||!S2) printf("\nThe List is empty.\n");
p2=S2->head->next;
while(p2){
if(IsElement(S1,p2->data)==0) Append(S1,p2->data);
p2=p2->next;
}
}
void Intersection(LinkSet *S1,LinkSet *S2,LinkSet *S3){
Link p2;
p2=S2->head->next;
while(p2){
if(IsElement(S1,p2->data)) { Append(S3,p2->data);}
p2=p2->next;
}
}
void PrintSet(LinkSet *T){
Link p;
if(!T){
printf("\nThe list is empty.\n");
return;
}
p=T->head->next;
while(p){
printf("%5d",(p->data));
p=p->next;
}
printf("\n");
}
void Exit(){
char c;
printf("\nAre you sure to exit?(Y/N)\n");
c=getch();
if(c=='y'||c=='Y') exit(0);
else return;
}
char Menu(){
int i;
clrscr();
for(i=0;i<77;i++) printf("*");
printf("\n1.MakeSet1\t2.MakeSet2\t3.IsElement\t4.Append\t5.Delete\n6.Union\t\t7.Intersection\t8.PrintSet1\t9.PrintSet2\t0.Exit\n");
for(i=0;i<77;i++) printf("*");
printf("\nPlease enter the number(0 to 9) to select a function.\n");
return getch();
}
void main(){
char c;
int x;
LinkSet *Set1,*Set2,*Set3;
InitList(Set1);
InitList(Set2);
InitList(Set3);
while(1){
c=Menu();
if(c<'0'||c>'9') continue;
switch(c){
case'1': printf("\nYou have chosen to run CreateSet1.\n");
CreateSet(Set1);
printf("\nSet1:"); PrintSet(Set1);
break;
case'2': printf("\nYou have chosen to run CreateSet2.\n");
CreateSet(Set2);
printf("\nSet2:"); PrintSet(Set2);
break;
case'3': printf("\nYou have chosen to run IsElement.\nPlease enter the number.\n");
scanf("%d",&x);
if(IsElement(Set1,x)){ printf("\n%d is an element.\n",x); }
else printf("\n%d isn't an element.\n",x);
break;
case'4': printf("\nYou have chosen to run Append in Set1.\nPlease enter the number you want to append.\n");
scanf("%d",&x);
Append(Set1,x);
break;
case'5': printf("\nYou have chosen to run Delete in Set1.\nPlease enter the number you want to delete.\n");
scanf("%d",&x);
Delete(Set1,x);
break;
case'6': printf("\nSet1:"); PrintSet(Set1);
printf("\nSet2:"); PrintSet(Set2);
printf("\nYou have chosen to run Union.\nNow you will union set1 and Set2,with set1 returned\n");
Union(Set1,Set2);
printf("\nAfter Union:");
PrintSet(Set1);
break;
case'7': printf("\nSet1:"); PrintSet(Set1);
printf("\nSet2:"); PrintSet(Set2);
printf("\nYou have chosen to run Intersection.\nNow you will intersection set1 and set2,with set1 returned\n");
Intersection(Set1,Set2,Set3);
printf("\nAfter Intersection:"); PrintSet(Set3);
break;
case'8': printf("\nSet1:"); PrintSet(Set1);
break;
case'9': printf("\nSet2:"); PrintSet(Set2);
break;
case'0': Exit();
break;
}
printf("\nPress anykey to main menu.\n");
getch();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -