单向链表的增删改查,逆序,,
注意顺序问题:条件的顺序. 后移时赋值的顺序。
#include
#include
typedef struct node
{
int a;
struct node * next;
} node_t;
void printf_node(node_t* h)
{
node_t* x;
x = h;
printf("print start!\n");
while(x != NULL)
{
printf("%d\n", x->a);
x = x->next;
}
printf("print end!\n");
}
node_t* find_a(node_t *h, int num)
{
node_t *x, *y, *pre;
x = h;
printf("find start!\n");
while(x != NULL && x->a != num) //条件顺序不可改,
{
pre = y;
y = x;
if(x->a == num)
break;
x = x->next;
}
if(x == NULL) {
printf("no number!\n");
return NULL;
}
else {
printf("find it!\n");
return x;
}
}
node_t* modify_node(node_t *head, int num1, int num2)
{
node_t *temp = head;
while(temp = find_a(temp, num1))
temp->a = num2;
#if 0
while(temp != NULL)
{
if(temp->a == num1)
temp->a = num2;
temp = temp->next;// printf_node(temp);
}
#endif
return head;
}
node_t* delete_annyone(node_t *head, node_t *anny)
//void delete_annyone(node_t *head, node_t *anny)
{
node_t *pre = NULL, *cur = NULL;
cur = head;
if(anny == head)
head = head->next;
else {
while(cur != NULL && cur != anny)
{
pre = cur;
cur = cur->next;
}
if(cur->next !=NULL)
pre->next = cur->next;
else
pre->next = NULL;
}
if(cur)
{
free(cur);
cur = NULL;
anny = NULL;
}
return head;
}
node_t* insert_node(node_t *head, int num)
{
node_t *p = head, *q = head;
while(p != NULL)
{
q = p;
p = p->next;
}
p = (node_t *)malloc(sizeof(node_t));
p->a = num;
p->next = NULL;
if(head == NULL)
head = p;
else
q->next = p;
return head;
}
node_t* reverse_node(node_t *head)
{
node_t *pre = NULL, *cur = NULL, *nex = NULL;
if(head->next == NULL)
return head;
pre = head;
cur = pre->next;
nex = cur->next;
pre->next = NULL;
cur->next = pre;
if(pre && cur && nex)
{
while(nex->next != NULL)
{
pre = cur;
cur = nex;
nex = nex->next;
cur->next = pre;
}
head = nex;
nex->next = cur;
}
else
head = cur;
return head;
}
int main(void)
{
node_t* head = NULL, *find = NULL;
head = insert_node(head, 10);
head = insert_node(head, 20);
head = insert_node(head, 30);
head = insert_node(head, 40);
head = insert_node(head, 50);
printf_node(head);
head = reverse_node(head);
// find = find_a(head, 10);
// head = modify_node(head, 20, 9);
// delete_end(head);
// head = delete_head(head);
// head = delete_head(head);
// head = delete_annyone(head, find);
// delete_annyone(head, find);
printf_node(head);
return 0;
}
#if 0
node_t* delete_head(node_t *head)
{
node_t *p;
p = head;
head = head->next;
if(p){
free(p);
p = NULL;
}
return head;
}
void delete_end(node_t *head)
{
node_t *pre = NULL, *cur = NULL, *nex = NULL;
nex = head;
if(head->next != NULL)
{
while(nex != NULL)
{
pre = cur;
cur = nex;
nex = nex->next;
}
pre->next = NULL;
}
else
head = NULL;
if(cur)
{
free(cur);
cur = NULL;
}
// return head;
}
#endif
双向链表 约瑟夫环
#include
#include
typedef struct node
{
int a;
struct node *next, *prev;
} node_t;
void printf_node(node_t* head)
{
node_t* x;
x = head->next;
printf("print start!\n");
do {
printf("%d\n", x->prev->a);
x = x->next;
} while(x != head->next);
printf("print end!\n");
}
node_t* insert_node(node_t *head, int num)
{
node_t *p = head;
p = (node_t *)malloc(sizeof(node_t));
p->a = num;
if(head == NULL)
{
head = p;
p->prev = head;
p->next = head;
}
else {
p->prev = head->prev;
p->next = head;
head->prev->next = p;
head->prev = p;
}
return head;
}
node_t* find_a(node_t *head, int num)
{
node_t *pre, *cur, *nex;
cur = head;
printf("find start!\n");
while(num > 1)
{
cur = cur->next;
num--;
}
printf("find end!\n");
return cur; // 返回的不再是head!!
}
node_t* kill_node(node_t *head, int num)
{
node_t *pre = NULL, *cur = NULL, *nex = NULL;
while(head->next != head)
{
cur = find_a(head, num);
pre = cur->prev;
nex = cur->next;
pre->next = nex;
nex->prev = pre;
head = nex;
if(cur)
{
free(cur);
cur = NULL;
}
}
return head;
}
int main(void)
{
node_t* head = NULL;
head = insert_node(head, 10);
head = insert_node(head, 20);
head = insert_node(head, 30);
head = insert_node(head, 40);
head = insert_node(head, 50);
printf_node(head);
// while(head->next != head)
head = kill_node(head, 2);
printf_node(head);
// head = reverse_node(head);
// find = find_a(head, 10);
// head = modify_node(head, 20, 9);
// delete_end(head);
// head = delete_head(head);
// head = delete_head(head);
// head = delete_annyone(head, find);
// delete_annyone(head, find);
// printf_node(head);
return 0;
}
阅读(1255) | 评论(0) | 转发(0) |