Bai cau truc du lieu
Trang 1 trong tổng số 1 trang
Bai cau truc du lieu
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);
Similar topics
» Cấu Trúc Dữ Liệu
» Cấu Trúc Dữ Liêu
» Một Số Tài Liệu Môn Cấu Trúc Dự liệu
» Bác nào còn nhớ về cấu trúc dữ liệu giúp với!
» Đề thi tham khảo môn Cấu trúc dữ liệu
» Cấu Trúc Dữ Liêu
» Một Số Tài Liệu Môn Cấu Trúc Dự liệu
» Bác nào còn nhớ về cấu trúc dữ liệu giúp với!
» Đề thi tham khảo môn Cấu trúc dữ liệu
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|