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


descriptionBài 12. Xoá các phần tử trùng trong List EmptyBài 12. Xoá các phần tử trùng trong List

more_horiz
XOÁ CÁC PHẦN TỬ TRÙNG TRONG LIST

THỦ TỤC XOÁ CÁC PHẦN TỬ TRÙNG TRONG LIST

Code:

//Result a position of x in list
Position Position_Value(List L, ElementType x)
{
   Position temp = 0;
   List S = L->Next;
   while(S!=NULL)
   {
      temp++;
      if(S->Element == x)
         return temp;
      S = S->Next;
   }
   return 0;
}
//Delete the identical values in list
void Delete_Identical_Value(List L) {
   List temp = L->Next;
   while(temp!=NULL) {
      Position k = Position_Value(temp, temp->Element);
      if(k!=0)
         Delete_List(temp,k);
      else
         temp = temp->Next;
   }
}

CHƯƠNG TRÌNH MẪU

Code:

#include "conio.h"
#include "stdio.h"
#include "alloc.h"
typedef unsigned int Position;
typedef int ElementType;
typedef struct Node{
   ElementType Element;
   Node* Next;
};
typedef Node * List;

Node * Make_Node(ElementType x) {
   Node * temp = (Node*)malloc(sizeof(Node));
   temp->Element = x;
   return temp;
}
List MakeNull_List() {
   List temp = (List)malloc(sizeof(List));
   temp->Next = NULL;
   return temp;
}
char Empty_List(List L) {
   return L->Next == NULL;
}
void Insert(List L, Node * p) {
   p->Next = L->Next;
   L->Next = p;
}
void Insert(List L, Node * p, Position i) {
   List temp = L;
   Position j = 1;
   while(j<i && temp!=NULL) {
      temp = temp->Next;
      j++;
   }
   if(i==j)
      Insert(temp,p);
   else
      printf("\nThis position is not true.");
}
List Input_List(unsigned int n) {
   List temp = MakeNull_List();
   ElementType x;
   unsigned int i = 1;
   while(i<=n) {
      printf("\tValue %d = ",i);
      scanf("%d",&x);
      Insert(temp,Make_Node(x));
      i++;
   }
   return temp;
}
void Out_List(List L) {
   List temp = L;
   while(!Empty_List(temp)) {
      printf("%d\t",temp->Next->Element);
      temp = temp->Next;
   }
}
void Delete_List(List L) {
   Node * p = L->Next;
   L->Next = p->Next;
   free(p);
}
void Delete_List(List L, Position i) {
   List temp = L;
   Position j = 1;
   while(j<i && temp!=NULL) {
      temp = temp->Next;
      j++;
   }
   if(i==j)
      Delete_List(temp);
   else
      printf("\nThis position is not true.");
}
//Result a position of x in list
Position Position_Value(List L, ElementType x)
{
   Position temp = 0;
   List S = L->Next;
   while(S!=NULL)
   {
      temp++;
      if(S->Element == x)
         return temp;
      S = S->Next;
   }
   return 0;
}
//Delete the identical values in list
void Delete_Identical_Value(List L) {
   List temp = L->Next;
   while(temp!=NULL) {
      Position k = Position_Value(temp, temp->Element);
      if(k!=0)
         Delete_List(temp,k);
      else
         temp = temp->Next;
   }
}
//the main programming
void main() {
   clrscr();
   unsigned int n;
   Position p;
   ElementType x;
   printf("Nhap n = ");
   scanf("%d",&n);
   List S = Input_List(n);
   printf("This List: \n");
   Out_List(S);
   Delete_Identical_Value(S);
   printf("\nA new list is resulted:\n");
   Out_List(S);
   getch();
}

descriptionBài 12. Xoá các phần tử trùng trong List EmptyThuật toán 2

more_horiz

Code:

//Delete the identical values in list
void Delete_Indentical_Values(List L) {
   List j, k, i = L->Next;
   while(i!=NULL)
   {
      j = i->Next;
      k = i;
      while(j!=NULL)
      {
         if(i->Element==j->Element)
         {
            j = j->Next;
            Delete_List(k);
         }
         else {
            j = j->Next;
            k = k->Next;
         }
      }
      i = i->Next;
   }
}
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