Diễn đàn hỏi đáp học thuật - Download Tài Liệu Miễn Phí
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

Diễn đàn hỏi đáp học thuật - Download Tài Liệu Miễn PhíĐăng Nhập

VỮNG TIN - TIẾP BƯỚC - THÀNH CÔNG


descriptionCài đặt tập hợp bằng List EmptyCài đặt tập hợp bằng List

more_horiz

Code:


#include<conio.h>
#include<stdio.h>
#include<alloc.h>
typedef int ElementType;
typedef struct Node{
   ElementType Element;
   Node*Next;
};

typedef Node* Set;

Set MakeNullSet(){
   Node* S=(Node*)malloc(sizeof(Node));
   S->Next=NULL;
   return S;
}

Node*Make_Node(ElementType X){
   Node* P=(Node*)malloc(sizeof(Node));
   P->Element=X;
   P->Next=NULL;
   return P;
}

char Empty_Set(Set S){
   return S->Next==NULL;
}

int Check(ElementType X,Set S){
   Set temp=S;
   while(!Empty_Set(temp)){
      if(temp->Next->Element == X)
         return 1;
      else
         temp=temp->Next;
   }
   return 0;
}

void InsertSet(ElementType X,Set S){
   Node *P=Make_Node(X);
   if(Check(X,S)==0){
      P->Next=S->Next;
      S->Next=P;
   }
}

void Union(Set A,Set B,Set &C){
   Set temp1,temp2;
   temp1=A->Next;
   temp2=B->Next;
   while(temp1!=NULL){
      InsertSet(temp1->Element,C);
      temp1=temp1->Next;
   }
   while(temp2!=NULL){
      InsertSet(temp2->Element,C);
      temp2=temp2->Next;
   }
}

void Intersection(Set A,Set B,Set &C){
   Set temp=A->Next;
   while(temp!=NULL){
      if(Check(temp->Element,B)==1)
         InsertSet(temp->Element,C);
      temp=temp->Next;
   }
}

void Difference(Set A,Set B,Set &C){
   Set temp=A->Next;
   while(temp!=NULL){
      if(Check(temp->Element,B)==0)
         InsertSet(temp->Element,C);
      temp=temp->Next;
   }
}

Set Input(){
   Set S=MakeNullSet();
   ElementType X;
   printf("\nNhap -1 de ket thuc\n");
   do{
      scanf("%d",&X);
      if(X!=-1)
         InsertSet(X,S);
   }while(X!=-1);
   return S;
}

void Output(Set S){
   Set temp=S;
   while(!Empty_Set(temp)){
      printf("%3d",temp->Next->Element);
      temp=temp->Next;
   }
}

void Swap(int &a,int &b){
   int temp=a;
   a=b;
   b=temp;
}

void Sort(Set &A){
      Set j,i=A->Next;
      while(i!=NULL){
         j=i->Next;
         while(j!=NULL){
            if(i->Element>j->Element)
               Swap(i->Element,j->Element);
            j=j->Next;
         }
         i=i->Next;
      }
}

void main(){
   clrscr();
   Set A,B,C;
   C = MakeNullSet();
   printf("\nNhap tap hop A");
   A = Input();
   Sort(A);
   Output(A);

   printf("\nNhap tap hop B");
   B = Input();
   Sort(B);
   Output(B);

   printf("\nHop cua hai tap hop A va B:");
   Union(A,B,C);
   Sort(C);
   Output(C);

   printf("\nGiao cua hai tap hop A va B:");
   C = MakeNullSet();
   Intersection(A,B,C);
   Sort(C);
   Output(C);

   printf("\nA\\B:");
   C = MakeNullSet();
   Difference(A,B,C);
   Sort(C);
   Output(C);
   printf("\n");
   getch();
}



Được sửa bởi nguyennhutkhanh ngày Thu Apr 07, 2011 11:16 pm; sửa lần 2.

descriptionCài đặt tập hợp bằng List EmptyRe: Cài đặt tập hợp bằng List

more_horiz
Bài tập này cài đặt khá tốt. Tuy nhiên có một vài điểm cần sửa như sau:
- Tên hàm hoặc thủ tục nên viết theo tiếng anh hoặc Việt (đừng nữa Tây nữa ta).
- Hàm chèn phần tử vào tập hợp chưa theo thứ tự (vì tập hợp phải có thứ tự).

descriptionCài đặt tập hợp bằng List EmptyCài đặt tập hợp bằng List,Stack,Queue vừa được cập nhật lại

more_horiz
Em đã sửa lại tất cả rồi thầy ơi!

descriptionCài đặt tập hợp bằng List EmptyRe: Cài đặt tập hợp bằng List

more_horiz
privacy_tip Permissions in this forum:
Bạn không có quyền trả lời bài viết
power_settings_newLogin to reply