数据结构(三) -- 单向链表

来源:互联网 发布:什么软件有钱人多 编辑:程序博客网 时间:2024/04/27 23:59

简介

单向链表是链表的一种,它是单一方向的,访问时只能从头节点开始。


单向链表实现

在实现单向链表时,最好加入头节点的概念。头节点(head)不存储数据,它的下一个节点才是真正存储数据的节点,这样的做的好处是可以使头节点保持不变,令对链表第一个元素(头节点的下一个元素)的操作与对链表其他元素的操作保持一致,简化代码逻辑。

<list.h>

#ifndef __LIST_H__#define __LIST_H__typedef struct node {    int data;    struct node *next;}Node;typedef struct list {    Node *head;    int length;}List;/* 创建并返回链表 */List *createList(void);/* 销毁链表 */void destroyList(List *list);/* 链表是否为空 */int listIsEmpty(List *list);/* 获取链表长度*/int getListLength(List *list);/* 获取链表positon位置节点的数据*/int listGet(List *list, int position);/* 在链表positon位置节点后插入一个节点*/int listInsert(List *list, int data, int position);/* 删除链表positon位置的节点*/int listDelete(List *list, int position);/* 遍历当前链表,打印数据*/void listTraverse(List *list);#endif


<list.c>

#include <stdio.h>#include <stdlib.h>#include "list.h"List *createList(void){    List *list;    list = (List *)malloc(sizeof(List));    if(NULL == list) {        return NULL;    }    list->head = (Node *)malloc(sizeof(Node));    if(NULL == list->head) {        free(list);        return NULL;    }    list->head->data = 0;    list->head->next = NULL;    list->length = 0;    return list;}void destroyList(List *list){    Node *cur, *tmp;    if(list == NULL) {        return;    }    cur = list->head;    while(cur != NULL) {        tmp = cur->next;        free(cur);        cur = tmp;    }    free(list);    list = NULL;}int listIsEmpty(List *list){    if(list->length == 0) {        return 1;    }    return 0;}/** * 插入数据到postion位置的后边 * param list 链表指针 * param data 插入的数据 * param postion 插入数据的位置, 0表示在在头节点后插入 */int listInsert(List *list, int data, int position){    Node *cur;    Node *newNode;    int i = 0;    if(position < 0 || position > list->length) {        printf("insert node position:%d is invalid\n", position);        return -1;    }    newNode = (Node *)malloc(sizeof(Node));    if(newNode == NULL) {        return -1;    }    newNode->data = data;    cur = list->head;    for(i = 0; i < position; i++) { //找到position位置的节点        cur = cur->next;    }    newNode->next = cur->next;    cur->next = newNode;    list->length++;    return 0;}int listDelete(List *list, int position){    int i;    Node *cur, *del;    if(list->length == 0) {        printf("list is Empty\n");        return -1;    }    if(position < 1 || position > list->length) { //position不能为0,因为不能删除头节点        printf("delete node position:%d is invalid\n", position);        return -1;    }    cur = list->head;    for(i = 0; i < position - 1; i++) { //找到要删除节点的前一个节点        cur = cur->next;    }    del = cur->next;    cur->next = del->next;    free(del);    del = NULL;    list->length--;    return 0;}int listGet(List *list, int position){    int i;    Node *cur;    if(list->length == 0) {        printf("list is Empty\n");        return -1;    }    if(position < 1 || position > list->length) { //position不能为0,头节点没有数据        printf("insert node position:%d is invalid\n", position);        return -1;    }    cur = list->head;    for(i = 0; i < position; i++) { //找到position位置的节点        cur = cur->next;    }    return cur->data;}void listTraverse(List *list){    Node *cur;    for(cur = list->head->next; cur != NULL; cur = cur->next) {        printf("[%s] %d\n", __FUNCTION__, cur->data);    }}


github地址(含Makefile和测试代码)
https://github.com/zsirkg/myWorks/tree/master/algorithms_and_data_structures/list/single
0 0
原创粉丝点击