您的位置:首页 > 编程语言 > C语言/C++

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: