Bài tập cấu trúc dữ liệu

5 322 0
Bài tập cấu trúc dữ liệu

Đang tải... (xem toàn văn)

Thông tin tài liệu

1 Chương trình xây dưng in hình phần tử danh sách # include # include struct node { int data; struct node *next; }; struct node *insert(struct node *p, int n) { struct node *temp; if(p==NULL) { p=(struct node *)malloc(sizeof(struct node)); if(p==NULL) { printf("Error\n"); exit(0); } p-> data = n; p-> next = NULL; } else { temp = p; while (temp-> next != p) temp = temp-> next; temp-> next = (struct node *)malloc(sizeof(struct node)); if(temp -> next == NULL) { printf("Error\n"); exit(0); } temp = temp-> next; temp-> data = n; temp-> next = p; } return (p); } void printlist ( struct node *p ) { struct node *temp; temp = p; printf("Cac gia tri du lieu danh sach la:\n"); if(p!= NULL) { { printf("%d\t",temp->data); temp=temp->next; } while (temp!= p); } else printf("Danh sach rong!\n"); } void main() { int n; int x; struct node *start = NULL ; printf("Hay nhap so nut can duoc tao \n"); scanf("%d",&n); while ( n > ) { printf( "Hay nhap cac gia tri du lieu duoc dat vao nut:\n"); scanf("%d",&x); start = insert ( start, x ); } printf("Danh sach duoc tao la:\n"); printlist ( start ); } Chương trình chèn thêm nút vào danh sách cách dùng đệ quy # include # include struct node { int data; struct node *next; }; struct node *insert(struct node *p, int n) { struct node *temp; if(p==NULL) { p=(struct node *)malloc(sizeof(struct node)); if(p==NULL) { printf("Error\n"); exit(0); } p-> data = n; p-> next = NULL; } else p->next = insert(p->next,n); return (p); } void printlist ( struct node *p ) { printf("Cac gia tri du lieu danh sach la:\n"); while (p!= NULL) { printf("%d\t",p-> data); p = p-> next; } } void main() { int n; int x; struct node *start = NULL ; printf("Hay nhap so nut can duoc tao: \n"); scanf("%d",&n); while ( n- > ) { printf( " Hay nhap cac gia tri du lieu duoc dat vao nut:\n"); scanf("%d",&x); start = insert ( start, x ); } printf("Danh sach duoc tao la:\n"); printlist ( start ); } Hàm xóa nút cho trước (đối số thứ 2) struct node *delet ( struct node *p, int node_no ) { struct node *prev, *curr ; int i; if (p == NULL ) { printf("There is no node to be deleted \n"); } else { if ( node_no > length (p)) { printf("Error\n"); } else { prev = NULL; curr = p; i = ; while ( i < node_no ) { prev = curr; curr = curr-> next; i = i+1; } if ( prev == NULL ) { p = curr -> next; free ( curr ); } else { prev -> next = curr -> next ; free ( curr ); } } } return(p); } Hàm tính chiều dài (tổng số nút) danh sách liên kết int length ( struct node *p ) { int count = ; while ( p != NULL ) { count++; p = p->next; } return ( count ) ; } Hàm thứ tự danh sách struct node *sortlist(struct node *p) { struct node *temp1,*temp2,*min,*prev,*q; q = NULL; while(p != NULL) { prev = NULL; = temp1 = p; temp2 = p -> next; while ( temp2 != NULL ) { if(min -> data > temp2 -> data) { = temp2; prev = temp1; } temp1 = temp2; temp2 = temp2-> next; } if(prev == NULL) p = -> next; else prev -> next = -> next; -> next = NULL; if( q == NULL) q = min; else { temp1 = q; while( temp1 -> next != NULL) temp1 = temp1 -> next; temp1 -> next = min; } } return (q); } Hàm chèn nút có giá trị n vào danh sách có thứ tự trỏ đến p struct node *sinsert(struct node *p, int n) { struct node *curr, *prev; curr =p; prev = NULL; while(curr ->data < n) { prev = curr; curr = curr->next; } if ( prev == NULL) { curr = (struct node *) malloc(sizeof(struct node)); if( curr == NULL) { printf("Loi khong cap duoc bo nho\n"); exit(0); } curr->data = n; curr->next = p; p = curr; } else { curr->data = n; curr->next = prev->next; prev->next = curr; } return(p); }

Ngày đăng: 03/01/2016, 18:43

Từ khóa liên quan

Mục lục

  • 2. Chương trình chèn thêm nút vào danh sách bằng cách dùng đệ quy

Tài liệu cùng người dùng

Tài liệu liên quan