C语言实现单链表之具体实现
2010-11-01 18:41
387 查看
/*
* link_list.c
*
* Created on: Nov 1, 2010
* Author: jenson
*/
#include "link_list.h"
#include <stdlib.h>
#include <string.h>
sq_list sl_create() {
sq_list list = (sq_list) malloc(sizeof(struct _sq_list_node_));
}
int sl_init(sq_list list) {
if (list != NULL) {
list->next = NULL;
return 1;
}
perror("init sq_list.\n");
return -1;
}
int sl_insert_first(sq_list list, elem_type e) {
if (list != NULL) {
sq_list node = (sq_list) malloc(sizeof(struct _sq_list_node_));
if (node != NULL) {
node->data = e;
node->next = list->next;
list->next = node;
return 1;
} else {
perror("malloc node.\n");
return -1;
}
}
perror("insert sq_list.\n");
return -1;
}
int sl_length(sq_list list) {
if (list != NULL) {
int len = 0;
sq_list p = list->next;
while (p != NULL) {
len++;
p = p->next;
}
return len;
}
perror("sl_length list is null.\n");
return -1;
}
int sl_insert_locale(sq_list list, int pos, elem_type e) {
if (list != NULL) {
int len = sl_length(list);
if (pos >= 0 && pos <= len) {
if (len == 0) {
return sl_insert_first(list, e);
} else {
sq_list p = list->next;
int i = 0;
for (i = 0; i < pos; i++) {
p = p->next;
}
sq_list node = (sq_list) malloc(sizeof(struct _sq_list_node_));
if (node != NULL) {
node->data = e;
node->next = p->next;
p->next = node;
return 1;
} else {
perror("malloc node.\n");
return -1;
}
}
} else {
perror("invalid arg pos");
return -1;
}
}
perror("sl_insert_locale list is null.\n");
return -1;
}
int sl_delete(sq_list list, int pos, elem_type *val) {
if (list != NULL) {
int len = sl_length(list);
if (pos > 0 && pos <= len - 1) {
sq_list p, q;
p = list;
int j = 0;
while (p->next != NULL && j < pos - 1) {
p = p->next;
++j;
}
if (p->next == NULL || j > pos - 1) {
return -1;
}
q = p->next;
p->next = q->next;
*val = q->data;
free(q);
return 1;
} else {
perror("invaild position.\n");
return -1;
}
}
perror("list is null.\n");
return -1;
}
int sl_get(sq_list list,int pos,elem_type * val){
if(list!=NULL){
int len = sl_length(list);
if(pos>0 && pos <=len){
sq_list p = list;
int j = 0;
while(p!=NULL && j<pos){
p = p->next;
++j;
}
*val = p->data;
return 1;
}else{
perror("sl_get invalid arg pos.\n");
return -1;
}
}
perror("sl_get null list.\n");
return -1;
}
void sl_destroy(sq_list list) {
if (list != NULL) {
free(list);
list = NULL;
return;
}
}
void sl_display(sq_list list) {
if (list != NULL) {
sq_list p = list->next;
while (p != NULL) {
printf("%d\t", p->data);
p = p->next;
}
}
}
//测试代码
/*
* main.c
*
* Created on: Nov 1, 2010
* Author: jenson
*/
#include "link_list.h"
#include <stdio.h>
int main() {
sq_list list = sl_create();
sl_init(list);
sl_insert_first(list, 1);
sl_insert_first(list, 2);
sl_insert_first(list, 3);
sl_insert_first(list, 4);
sl_insert_first(list, 5);
sl_insert_first(list, 6);
sl_insert_first(list, 7);
sl_insert_first(list, 8);
sl_insert_first(list, 9);
sl_insert_first(list, 10);
sl_display(list);
// int len = sl_length(list);
// printf("\nlen=%d\n", len);
// sl_insert_locale(list, 5, 11);
// sl_insert_locale(list, 5, 12);
// sl_display(list);
int value;
// printf("\n");
// sl_delete(list, 2, &value);
// sl_display(list);
// printf("\nvalue=%d\n", value);
// len = sl_length(list);
// printf("\nlen=%d\n", len);
sl_get(list,101,&value);
printf("\nvalue=%d\n", value);
free(list);
return 0;
}
本文出自 “有思想的代码” 博客,请务必保留此出处http://wujuxiang.blog.51cto.com/2250829/413653
* link_list.c
*
* Created on: Nov 1, 2010
* Author: jenson
*/
#include "link_list.h"
#include <stdlib.h>
#include <string.h>
sq_list sl_create() {
sq_list list = (sq_list) malloc(sizeof(struct _sq_list_node_));
}
int sl_init(sq_list list) {
if (list != NULL) {
list->next = NULL;
return 1;
}
perror("init sq_list.\n");
return -1;
}
int sl_insert_first(sq_list list, elem_type e) {
if (list != NULL) {
sq_list node = (sq_list) malloc(sizeof(struct _sq_list_node_));
if (node != NULL) {
node->data = e;
node->next = list->next;
list->next = node;
return 1;
} else {
perror("malloc node.\n");
return -1;
}
}
perror("insert sq_list.\n");
return -1;
}
int sl_length(sq_list list) {
if (list != NULL) {
int len = 0;
sq_list p = list->next;
while (p != NULL) {
len++;
p = p->next;
}
return len;
}
perror("sl_length list is null.\n");
return -1;
}
int sl_insert_locale(sq_list list, int pos, elem_type e) {
if (list != NULL) {
int len = sl_length(list);
if (pos >= 0 && pos <= len) {
if (len == 0) {
return sl_insert_first(list, e);
} else {
sq_list p = list->next;
int i = 0;
for (i = 0; i < pos; i++) {
p = p->next;
}
sq_list node = (sq_list) malloc(sizeof(struct _sq_list_node_));
if (node != NULL) {
node->data = e;
node->next = p->next;
p->next = node;
return 1;
} else {
perror("malloc node.\n");
return -1;
}
}
} else {
perror("invalid arg pos");
return -1;
}
}
perror("sl_insert_locale list is null.\n");
return -1;
}
int sl_delete(sq_list list, int pos, elem_type *val) {
if (list != NULL) {
int len = sl_length(list);
if (pos > 0 && pos <= len - 1) {
sq_list p, q;
p = list;
int j = 0;
while (p->next != NULL && j < pos - 1) {
p = p->next;
++j;
}
if (p->next == NULL || j > pos - 1) {
return -1;
}
q = p->next;
p->next = q->next;
*val = q->data;
free(q);
return 1;
} else {
perror("invaild position.\n");
return -1;
}
}
perror("list is null.\n");
return -1;
}
int sl_get(sq_list list,int pos,elem_type * val){
if(list!=NULL){
int len = sl_length(list);
if(pos>0 && pos <=len){
sq_list p = list;
int j = 0;
while(p!=NULL && j<pos){
p = p->next;
++j;
}
*val = p->data;
return 1;
}else{
perror("sl_get invalid arg pos.\n");
return -1;
}
}
perror("sl_get null list.\n");
return -1;
}
void sl_destroy(sq_list list) {
if (list != NULL) {
free(list);
list = NULL;
return;
}
}
void sl_display(sq_list list) {
if (list != NULL) {
sq_list p = list->next;
while (p != NULL) {
printf("%d\t", p->data);
p = p->next;
}
}
}
//测试代码
/*
* main.c
*
* Created on: Nov 1, 2010
* Author: jenson
*/
#include "link_list.h"
#include <stdio.h>
int main() {
sq_list list = sl_create();
sl_init(list);
sl_insert_first(list, 1);
sl_insert_first(list, 2);
sl_insert_first(list, 3);
sl_insert_first(list, 4);
sl_insert_first(list, 5);
sl_insert_first(list, 6);
sl_insert_first(list, 7);
sl_insert_first(list, 8);
sl_insert_first(list, 9);
sl_insert_first(list, 10);
sl_display(list);
// int len = sl_length(list);
// printf("\nlen=%d\n", len);
// sl_insert_locale(list, 5, 11);
// sl_insert_locale(list, 5, 12);
// sl_display(list);
int value;
// printf("\n");
// sl_delete(list, 2, &value);
// sl_display(list);
// printf("\nvalue=%d\n", value);
// len = sl_length(list);
// printf("\nlen=%d\n", len);
sl_get(list,101,&value);
printf("\nvalue=%d\n", value);
free(list);
return 0;
}
本文出自 “有思想的代码” 博客,请务必保留此出处http://wujuxiang.blog.51cto.com/2250829/413653
相关文章推荐
- C语言数据结构中链表的具体实现
- C语言实现双向链表之具体实现
- C语言实现数据结构中的循环链表
- 用数组实现的单链表[C语言]
- 链表源代码(C语言实现)
- 链表结构_C语言实现
- c语言用尾递归实现单向链表的逆向排列
- C语言实现链表及其操作
- 操作系统模拟内存管理实验,C语言链表实现,附源码
- C语言单向链表的实现
- C语言实现链表的创建、计算链表长度及两个链表的合并
- C语言实现链表之单向链表(十三)获取数据对应的结点
- c语言实现单链表
- C语言实现链表之双向链表(九)在任意位置插入结点
- 自己实现C语言单向链表
- 数据结构——单循环链表一部分功能的C语言实现
- C语言实现面向对象风格带迭代器的双向链表
- C语言实现链表(三)
- C语言单链表实现方法详解
- C语言单向链表的实现与操作