k4info
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.

Bai cau truc du lieu

Go down

Cool Bai cau truc du lieu

Bài gửi by itsnotoverlbh Sun Mar 06, 2011 10:16 pm

Bài chi mang tính chất tham khao nha, nếu có bạn nào sửa chạy được thì pm cho tôi hay với nha
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MaxLength 500
 typedef char ElementType;
 /*********** dung danh sach********/
 typedef int Position;
 typedef struct{
        ElementType Elements[MaxLength];
        Position Last;} List;
 // tao danh sach rong
 void MakeNull_List(List *L){
      L->Last=0;}
 //kiem tra ds rong
 int Empty_List(List L){
    return L.Last==0; }
 // chen vao ds
 void Insert_List(ElementType word, Position P,List *L){
      if(L->Last==MaxLength)
      printf("DS day");
      else if((P<1) || (P>L->Last+1))
      printf("vi tri k hop le");
      else{
          Position Q;
          for(Q=(L->Last-1)+1;Q>P-1;Q--)
          L->Elements[Q]=L->Elements[Q-1];
          L->Elements[P-1]=word;
          L->Last++;}}
// in danh sach
void Print_List(List L){
    Position P;
    P = First(L);
    while(P!=EndList(L))
    printf("%d",retrieve(P,L));
    P=next(P,L);}
 // Nhap chuoi
 void Nhap_list( char *str, List *L){
      MakeNull_List(L);
      ElementType word;
      int i,len;
      len=strlen(str);
      i=0;
      while(i<len){
                  word = (char*)malloc(100)
                  scanf(str+i,"%s",word);
                  Insert_List(word,EndList(L),*L);
                  i+=strlen(word+1)}}*/
 /*************** hang doi*******/
 typedef struct{
        ElementType Elements[MaxLength];
        int Front,Rear;
        } Queue;
 //tao hang rong
 void MakeNull_Queue(Queue *Q){
      Q->Front=-1;
      Q->Rear=-1;}
 //tao hang rong
 int Empty_Queue(Queue Q){
    return Q.Front==-1;}
 // kiem tra hang day
 int Full_Queue(Queue Q){
    return (Q.Rear-Q.Front+1)==MaxLength;}
 // them phan tu vao hang
 void EnQueue(ElementType word,Queue *Q){
      if(!Full_Queue(*Q)){
                          if(Empty_Queue(*Q)) Q->Front=0;
                          if(Q->Rear==MaxLength-1){
                                                  for(int i=Q->Front;i<=Q->Rear;i++)
                                                  Q->Elements[i-Q->Front]=Q->Elements[i];
                                                  Q->Rear=MaxLength-Q->Front-1;
                                                  Q->Front=0;}
                                                  Q->Rear=Q->Rear+1;
                                                  Q->Elements[Q->Rear]=word;}
          else printf("hang day");}
  void Print_Queue(Queue *Q)
{
    Position Q;
    Q->Front;
    while(T!=End_List(Q))
    {  printf("%s", Front(Q));
        Q->Front=Q->Front->Next;}
  // trich lay mot chuoi
  void Trich_Queue(char *str,Queue *Q){
      MakeNull_Queue(Q);
      ElementType word;
      int i,len;
      len=strlen(str);i=0;
      while(i<len){
        word = (char*)malloc(10);
      scanf(str+i,"%s",word);
      EnQueue(word,Q);
      i+=strlen(word+1);}} 
  /********** ngan xep *******/
    typedef struct {
            ElementType Elements[MaxLength];
            int Top_idx;
            } Stack;
 // tao ngan xep rong
 void MakeNull_Stack(Stack *S){
      S->Top_idx=MaxLength;}
  // kiem tra ngan xep rong
  int Empty_Stack(Stack S){
      return S.Top_idx==MaxLength;}
  // kiem tra ngan xep day
  int Full_Stack(Stack S){
      return S.Top_idx==0;}
 // cho hang doi vao ngan xep
 void QueuetoStack(Queue Q, Stack *S){
      Queue Q;
      MakeNull_Queue(Q);
      Q1=Q;
      MakeNull_Stack(S);
      ElementType x;
      while(!Empty_Queue(Q1))
      x=(char*)malloc(255);
      Queue Q;
      MakeNull_Queue(&Q);
      Stack S;
      MakeNull_Stack(S);                                                   
itsnotoverlbh
itsnotoverlbh

Posts : 367
Thanked : 21
Gia Nhập 18/02/2011

Tài Sản
Thú nuôi:

http://www.ittd.freevnn.com

Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

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