#include<stdio.h> #include<stdlib.h> // cau truc node struct node{ int data; nodenext;…
- User
#include<stdio.h> #include<stdlib.h> // cau truc node struct node{ int data; node*next; }; // cau truc hang doi struct hangdoi{ int size; node *dau; node *cuoi; }; // khoi tao void khoitao(hangdoi *l){ l->size =0 ; l->dau = l->cuoi = NULL ; } // cap phat o nho node *tao_node(int x){ node p = (node) malloc (sizeof (node)); p->data=x; p->next = NULL; return p; } // them phan tu node * them_phan_tu(hangdoi *l , int x){ node *p = tao_node(x); if ( l->dau ==NULL && l->cuoi == NULL){ l->dau = l->cuoi = p ; }else { l->cuoi->next = p ; l->cuoi = p; } l->size ++; } // lay 1 phan tu int lay_phan_tu(hangdoi *l,int x){ if( l->dau ==NULL ){ printf("hang doi rong:"); return - 1; } node *temp = l->dau; x = temp ->data; l->dau = l->dau->next; l->size --; } // nhap void nhap(hangdoi *l){ int n,x; printf("nhap vao so phan tu:"); scanf("%d",&n); for ( int i=0 ; i< n ; i++){ scanf("%d",&x); them_phan_tu(l,x); } } void xuat(hangdoi *l){ node *temp = l->dau; while (temp !=NULL){ printf("%d ",temp->data); temp = temp->next; } printf("\n"); } // them vao vi tri bat kif void them_vao_vt_k(hangdoi *l , int t, int k){ if ( k<0 && k>l->size ){ printf ("vi tri them kkhong hop le:"); } struct node *p= tao_node(t); if (k==0){ p->next = l->dau; l->dau = p ; if(l->cuoi==NULL){ l->cuoi==NULL; } } else { node *temp = l->dau; for( int i=0 ; i<k-1 ; i++){ temp = temp ->next; } p->next = temp ->next; temp->next = p; if( p->next ==NULL){ l->cuoi=p; } } l->size ++; } int main(){ struct hangdoi l; int t,k,x; khoitao(&l); nhap(&l); xuat(&l); lay_phan_tu(&l,x); xuat(&l); printf("nhap vao phan tu them:"); scanf("%d",&t); printf("vi tri them:"); scanf("%d",&k); them_vao_vt_k(&l,t,k); xuat(&l); }
- Assistant
- User
#include<stdio.h> #include<stdlib.h> //struct node struct node { int data; node *next; }; struct nganxep{ node *top; }; void khoitao(nganxep *s){ s->top = 0 ; } // cap phat o nho node tao_node(int x){ node * p = (node) malloc (sizeof (node)); p->data= x ; p->next =NULL; return p; } void them(nganxep *s , int x){ node *p = tao_node(x); if ( s->top == 0){ s->top= p ; }else { p->next = s->top ; s->top = p ; } } void lay ( nganxep *s){ if ( s->top == 0){ printf("ngan xep rong:"); } else{ node *temp = s->top; s->top = temp->next; } } void nhap( nganxep *s , int x){ int n; printf("nhap vao so phan tu cua ngan xep:"); scanf("%d",&n); for ( int i=0 ; i<n ; i++){ scanf("%d",&x); them(s,x); } } void xuat(nganxep *s){ node * temp = s->top; while (temp !=NULL){ printf("%d ",temp->data); temp=temp->next; } printf("\n"); } void them_vao_vt_k(nganxep *s , int t , int k){ int n; if ( k<0 || k > n){ printf("vi tri them khong hop le:"); } node *p = tao_node(t); if ( k== 0){ p->next = s->top; s->top = p; }else { node *temp = s->top ; for ( int i = 0 ; i<k-1 ; i++){ temp = temp->next ; } p->next = temp->next; temp->next = p; } } int main (){ struct nganxep s; khoitao(&s); int x,k,t; nhap(&s,x); xuat(&s); lay(&s); xuat(&s); printf ("nhap vao vi tri them:"); scanf("%d", &k); printf("nhap voao gia tri them:"); scanf("%d",&t); them_vao_vt_k(&s,t,k); xuat(&s); }
- Assistant
- User
dựa những dữ liệu đã cho ở trên làm bài này Cho một danh sách chứa tối đa m số nguyên. Viết chương trình thực hiện các yêu cầu sau: Câu 1 (6 điểm)
- Biểu diễn danh sách bởi con trỏ
- Viết hàm nhập dữ liệu cho danh sách
- Viết hàm hiển thị dữ liệu trong danh sách ra màn hình
- Assistant
- User
Cho một danh sách chứa tối đa m số nguyên. Viết chương trình thực hiện các yêu cầu sau: Câu 1 (6 điểm)
- Biểu diễn danh sách bởi con trỏ
- Viết hàm nhập dữ liệu cho danh sách
- Viết hàm hiển thị dữ liệu trong danh sách ra màn hình #include<stdio.h> #define N 50 struct danhsach{ int data[N]; int size; }; void khoitao( danhsach &l){ l.size=0; } void nhap( danhsach &l ){ printf("nhap vao so luong danh sach:"); scanf("%d",&l.size); for ( int i=0 ; i<l.size ; i++){ printf("nhap phan tu thu %d :",i+1); scanf("%d",&l.data[i]); } } void xuat( danhsach &l){ for( int i=0; i<l.size ; i++){ printf("%d ",l.data[i]); } printf("\n"); } void xoa_dau(danhsach &l){ if(l.size==0){ printf("danh sach rong:"); } for ( int i=0 ; i<l.size-1 ; i++){ l.data[i] =l.data[i+1]; } l.size--; printf("da xoa danh sach:"); } void tach_danh_sach( danhsach &l , danhsach &l1 , danhsach &l2){ khoitao(l1); khoitao(l2); for ( int i=0; i< l.size ; i++){ if( l.data[i] %2 ==0){ l1.data[l1.size++] = l.data[i]; } else{ l2.data[l2.size++] = l.data[i]; } } } int main(){ struct danhsach l,l1,l2; khoitao(l); nhap(l); xuat(l); xoa_dau(l); xuat(l); tach_danh_sach(l,l1,l2); xuat(l1); xuat(l2); }
- Assistant
- User
Cho một danh sách chứa tối đa m số nguyên. Viết chương trình thực hiện các yêu cầu sau: Câu 1 (6 điểm)
- Biểu diễn danh sách bởi con trỏ
- Viết hàm nhập dữ liệu cho danh sách
- Viết hàm hiển thị dữ liệu trong danh sách ra màn hình #include <stdio.h> #define m 100 struct danhsach{ int data[m]; int size; }; void khoitao(danhsach &l){ l.size=0; } void nhap(danhsach &l){ printf("nhap so luong danh sach "); scanf("%d",&l.size); for(int i=0;i<l.size;i++){ scanf("%d",&l.data[i]); } } void xuat(danhsach &l){ for(int i=0;i<l.size;i++){ printf("% d",l.data[i]); } } int main(){ struct danhsach l; khoitao(l); nhap(l); xuat(l); }
- Assistant