关于链表的问题求助
本人刚刚接触数据结构,由于C语言还没有学玩,所以对一些基础问题非常混沌,向高手求教。1、对输入的一批数据用尾插法建立一带表头结点的单向线性链表, 输出该链表的数据,仅对单向线性链表进行处理,在不建立新链表的前提下,使原链表中的数据结点逆序,输出处理后的链表数据。
2、实现仅带尾结点指针单向循环链表的建立、插入、删除、查找操作算法。
3、对两个带表头结点的单向线性链表按结点数据域分别进行递增排序,然后归并成一个链表。
2008-03-27 10:43
2008-03-27 10:44
2008-03-27 23:44
2008-03-27 23:46
2008-03-28 12:48
2008-03-28 12:55
程序代码:#include <stdio.h>
#include <malloc.h>
#include <conio.h>
//define a node
typedef struct node{
int val;
struct node *next;
} node;
//create a link list by tail inserting
void tail_insert(node **h,int i){
if (*h == 0){
*h = (node*)malloc(sizeof(node));
(*h)->val = i;
(*h)->next = 0;
return;
}
node *p = (node*)malloc(sizeof(node)),*pl = *h;
p->val = i;
p->next = 0;
while (pl->next)
pl = pl->next;
pl->next = p;
return;
}
//create a link list by head inserting
void head_insert(node **h,int i){
if (*h == 0){
*h = (node*)malloc(sizeof(node));
(*h)->next = 0;
(*h)->val = i;
return;
}
node *p = (node*)malloc(sizeof(node));
p->val = i;
p->next = *h;
*h = p;
return;
}
//reverse a link list by iteration
node* reverse_iteration(node *h){
node *nh = h,*p = 0;
h = h->next;
nh->next = 0;
while (h){
p = h;
h = h->next;
p->next = nh;
nh = p;
}
return nh;
}
//reverse a link list by recursion
node * reverse_recursion(node *h){
if (!h || !h->next)
return h;
node *p = h->next,*r = 0;
r = reverse_recursion(p);
p->next = h;
h->next = 0;
return r;
}
2008-03-28 18:33
2008-03-28 23:29
2008-03-29 09:28

2008-03-29 11:41