@iwktd981220
2017-09-22T07:17:45.000000Z
字数 1714
阅读 430
code
typedef struct _slist_t {
int data;
struct _slist_t* next;
} slist_t;
/*
第一个参数是要插入结点的表, 第二个是插入的数据
头插入结点, 返回新的链表头.
食用栗子:
slist_t *list = NULL;
list = slist_prepend (list, 1);
list = slist_prepend (list, 2);
*/
slist_t* slist_prepend (slist_t *list, int data);
/*
在指定地方插入结点... 返回新的头
食用栗子:
slist_t *list=NULL;
list=slist_insert(list,0,1);
list=slist_insert(list,0,2);
list=slist_insert(list,-1,4);
list=slist_insert(list,100,5);
产生 2->1->4->5
注意, 当position超过链表长度减1或者为负数时, 默认尾插入...
*/
slist_t* slist_insert(slist_t *list, int position, int data)
/* 逆置一个单链表 */ slist_t *slist_reverse(slist_t *list) //规则一样, 返回新的头 食用栗子略.
参考链接:http://www.cnblogs.com/newwy/archive/2010/10/10/1847456.html
(主程序略,因为不知道输入后干啥...):
typedef struct _slist_t {
int data;
struct _slist_t* next;
} slist_t,*slist_f;
slist_f *slist_prepend (slist_t *list, int data){
slist_t *new_node;
slist_t *l;
l = list ;
new_node = (slist_t*)malloc(sizeof(slist_t));
for (size_t i = 1; i < data; i++) {
l = l ->next;
}
new_node ->next = l ->next;
l ->next = new_node;
return *l;
}
slist_f *slist_insert (slist_t *list,int sequence,int data){
slist_t *lb;
lb = l;
slist_t *new_node;
new_node = (slist_t*)malloc(sizeof(slist_t));
new_node = data;
if (sequence <0 || sequence > sizeof(lb)/sizeof(slist_t)) {
while (lb ->next !=NULL) {
lb = lb ->next;
}
new_node ->next = lb ->next;
lb ->next = data;
}
else for (size_t i = 0; i < sequence; i++) {
lb = lb ->next;
}
list ->next = lb ->next;
lb ->next = list;
return *l;
}
slist_f *slist_reverse(slist_t *list){
int length = sizeof(list)/sizeof(slist_t);
int a[length];
slist_t *l;
slist_t *new_node;
slist_t *new_list;
l = list;
new_list = (slist_t*)malloc(sizeof(slist_t));
new_list -> next = NULL;
for (size_t i = 0; i < length; i++) {
a[0] = l ;
l = l ->next;
}
for (size_t j = 0,k = length-1; j < length; j++) {
new_node = (slist_t*)malloc(sizeof(slist_t));
new_node = a[k];
new_node ->next = new_list ->next;
new_list ->next = new_node;
}
return *new_list;
}