Tải bản đầy đủ (.pdf) (5 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (113.59 KB, 5 trang )

1. Chương trình xây dưng và in ra màn hình các phần tử của danh sách
# include <stdio.h>
# include <stdlib.h>
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 trong danh sach la:\n");
if(p!= NULL)
{
do
{
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 -- > 0 )
{
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 );
}

2. Chương trình chèn thêm nút vào danh sách bằng cách dùng đệ quy
# include <stdio.h>
# include <stdlib.h>
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 trong 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- > 0 )
{
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 );
}

3. 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 = 1 ;
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);


}

4. Hàm tính chiều dài (tổng số nút) của danh sách liên kết
int length ( struct node *p )
{
int count = 0 ;
while ( p != NULL )
{
count++;
p = p->next;
}
return ( count ) ;
}


5. Hàm sắp 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;
min = temp1 = p;
temp2 = p -> next;
while ( temp2 != NULL )
{
if(min -> data > temp2 -> data)
{
min = temp2;
prev = temp1;
}
temp1 = temp2;
temp2 = temp2-> next;
}
if(prev == NULL)
p = min -> next;
else
prev -> next = min -> next;
min -> next = NULL;
if( q == NULL)
q = min;
else
{

temp1 = q;
while( temp1 -> next != NULL) temp1 = temp1 -> next;
temp1 -> next = min;
}
}
return (q);
}


6. Hàm chèn nút có giá trị n vào danh sách đã có thứ tự và được trỏ đến
bởi 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);
}



×