Mã:
#include <stdio.h>
#include <stdlib.h>
typedef int element;
typedef struct basicnode {
element inf;
basicnode *next, *prev;
} *node;
typedef struct basiclist {
node head;
node tail;
} *list;
list Initialize() {
list ds = (list)malloc(sizeof(list));
if (!ds) {
printf ("MEMORY FULL!!! No memory to access!");
return NULL;
}
ds->head = NULL;
ds->tail = NULL;
return (ds);
}
node Create_Node() {
node temp = (node)malloc(sizeof(node));
if (!temp) {
printf ("MEMORY FULL!!! No memory to access!");
return NULL;
}
temp->next = NULL;
temp->prev = NULL;
return (temp);
}
void Add_head(list ds, element x) {
node NewNode = Create_Node();
if (!NewNode) return;
if (!ds->head) ds->head = ds->tail = NewNode;
else {
NewNode->inf = x;
NewNode->next = ds->head;
ds->head->prev = NewNode;
ds->head = NewNode;
}
}
void Add_Last(list ds, element x) {
node NewNode = Create_Node();
if (!NewNode) return;
NewNode->inf = x;
if (!ds->tail) ds->head = ds->tail = NewNode;
else {
ds->tail->next = NewNode;
NewNode->prev = ds->tail;
ds->tail = NewNode;
}
}
node Add_Mid(list ds, element x, node InsNode) {
node NewNode = Create_Node();
if (!NewNode) return (NULL);
if (!InsNode->next) {
InsNode->next = NewNode;
NewNode->prev = InsNode;
ds->tail = NewNode;
}
else {
NewNode->next = InsNode->next;
InsNode->next->prev = NewNode;
InsNode->next = NewNode;
NewNode->prev = InsNode;
}
return (NewNode);
}
node Searching(list ds, element SearchData) {
node CurNode = ds->head;
while (CurNode) {
if (CurNode->inf == SearchData) return CurNode;
CurNode = CurNode->next;
}
return CurNode;
}
void Delete_Node (list ds, element DelData) {
node DelNode = Searching(ds, DelData);
if (!DelNode) return;
if (!DelNode->next && !DelNode->prev) ds->head = ds->tail = NULL;
else
if (!DelNode->prev) {
ds->head = ds->head->next;
ds->head->prev = NULL;
}
else
if (!DelNode->next) {
ds->tail = ds->tail->prev;
ds->tail->next = NULL;
}
else {
DelNode->prev->next = DelNode->next;
DelNode->next->prev = DelNode->prev;
}
DelNode->next = DelNode->prev = NULL;
free (DelNode);
}
void Delete (list ds) {
node TempNode = ds->head;
while (TempNode) {
TempNode = TempNode->next;
free(ds->head);
ds->head = TempNode;
}
return;
}
list Create(list ds, int N) {
ds = Initialize();
element NewData;
for (int i = 0; i < N; i++) {
printf ("Nhap du lieu thu %d:", i+1); scanf ("%d", &NewData);
Add_Last(ds, NewData);
}
return (ds);
}
void Bubble_Sort (list ds) {
node Inode = ds->head;
element temp;
if (!Inode) return;
while (Inode != ds->tail) {
node Jnode = ds->tail;
while (Jnode != Inode) {
if (Jnode->inf < Jnode->prev->inf) {
temp = Jnode->inf;
Jnode->inf = Jnode->prev->inf;
Jnode->prev->inf = temp;
}
Jnode = Jnode->prev;
}
Inode = Inode->next;
}
}
int main() {
list ds;
node dp;
int i;
ds = Create(ds, 5);
for (dp = ds->head; dp; dp= dp->next) printf ("%d ", dp->inf);printf ("
");
Bubble_Sort(ds);
printf ("Danh sach tang dan:
");
for (dp = ds->head; dp; dp= dp->next) printf ("%d ", dp->inf);printf ("
");
dp = Searching(ds, 5);
if (dp) printf ("%d
", dp->inf); else printf ("Khong co phan tu 15 trong danh sach.
");
if (dp) {
Delete_Node(ds, 5);
printf ("Danh sach sau khi xoa phan tu 5:
");
for (dp = ds->head; dp; dp= dp->next) printf ("%d ", dp->inf);
}
else printf ("Khong co phan tu 5 trong danh sach.");
Delete(ds);
for (dp = ds->head; dp; dp= dp->next) printf ("%d ", dp->inf); printf ("
");
return 0;
}
=================================
Hai hàm Delete bị lỗi mà mình k biết cách khắc phục. Mong cao nhân chỉ giáo giùm.
- - - Nội dung đã được cập nhật ngày 07-10-2014 lúc 11:09 PM - - -
K ai giúp à??????????????
View more random threads:
khi đề cập đến đội hình MU, không thể ko nói tới một trong các câu lạc bộ bóng đá huyền thoại và với tầm ảnh hưởng nhất trong lịch sử của môn thể thao vua. Với hơn 1 thế kỷ còn đó và phát triển,...
Đội hình MU - Manchester United:...