码迷,mamicode.com
首页 > 其他好文 > 详细

【数据结构-链表】单链表

时间:2015-05-12 17:09:37      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:单链表

链表

特点

链表是通过任意的存储单元来存储线性表中的数据元素

链表组成

链表是由很对节点组成
带有头结点的单链表
技术分享

typedef struct{
    struct node *head;//链表的头结点
}LinkList;

节点结构

技术分享

typedef struct node{
    int data;
    struct node *next;
}Node;

链表的插入操作

  1. 首先得到插入位置的节点n
  2. 得到插入位置的前一个节点pre
  3. 讲新插入的节点的next指针指向n
  4. pre节点的next指向n
    技术分享
    技术分享
/**
 *  在p位置插入数值为i的节点
 *
 */
void insert(LinkList *list, int p, int x) {
    Node *node = (Node *)malloc(sizeof(Node));//创建一个新节点
    node->data = x;//给新节点的data赋值
    Node *n = list->head;//定义一个位置节点
    Node *pre = list->head;//定义位置前面的那个节点

    if(p == 0) {
        node->next = list->head->next;//头节点的后面那个节点赋给这个新节点的next
        list->head->next = node;//头结点的next指向这个节点
    } else {
        for(int i=1; i<=p; i++) {
            pre = n;//得到插入位置的前一个节点
            n = n->next;//得到插入位置的节点
            if(n == NULL) {
                printf("没有找到对应的位置\n;");
                return;
            }
        }
        pre->next = node;//前节点的next指向这个节点
        node->next = n;//这个节点的next指向以前的老节点
    }
}

删除操作

  1. 查找要删除的元素所在那个节点node
  2. 讲node的前一个节点的next指针指向node后面的节点
  3. free(node)
    技术分享

C语言实现单链表

#include <stdio.h>
#include <stdlib.h>
/**
 *  定义一个节点结构
 */
typedef struct node{
    int data;
    struct node *next;
}Node;

typedef struct{
    struct node *head;//链表的头结点
}LinkList;

/**
 *  创建链表
 *
 *  @param list 要创建的这个链表指针
 *  @param n    表示创建一个含有n个元素的链表
 */
void createList(LinkList *list, int n) {
    Node *tail;//该指针始终只想末尾的那个节点
    list->head = (Node *)malloc(sizeof(Node));
    tail = list->head;
    for (int i=0; i<n; i++) {
        Node *n = (Node *)malloc(sizeof(Node));
        printf("请输入新节点的数据:\n");
        scanf("%d", &n->data);
        n->next = NULL;
        tail->next = n;
        tail = n;
    }
}

/**
 *  删除链表中值为x的节点
 */
void delete(LinkList *list, int x) {
    Node *pre;
    Node *node = list->head->next;
    pre = list->head;
    if(list == NULL) {
        printf("链表为空!");
    }

    while(node->data != x && node != NULL) {
        pre = node;
        node = node->next;
    }

    if(node == NULL) {
        printf("没有找到这个元素!\n");
    } else {
        pre->next = node->next;
        free(node);
    }
}

/**
 *  在p位置插入数值为i的节点
 *
 */
void insert(LinkList *list, int p, int x) {
    Node *node = (Node *)malloc(sizeof(Node));//创建一个新节点
    node->data = x;//给新节点的data赋值
    Node *n = list->head;//定义一个位置节点
    Node *pre = list->head;//定义位置前面的那个节点

    if(p == 0) {
        node->next = list->head->next;//头节点的后面那个节点赋给这个新节点的next
        list->head->next = node;//头结点的next指向这个节点
    } else {
        for(int i=1; i<=p; i++) {
            pre = n;//得到插入位置的前一个节点
            n = n->next;//得到插入位置的节点
            if(n == NULL) {
                printf("没有找到对应的位置\n;");
                return;
            }
        }
        pre->next = node;//前节点的next指向这个节点
        node->next = n;//这个节点的next指向以前的老节点
    }
}

/**
 *  打印链表
 *
 *  @param list 要打印的链表
 */
void display(LinkList *list) {
    Node *node = list->head->next;
    while(node != NULL) {
        printf("链表元素%d\n", node->data);
        node = node->next;
    }
}

int main() {
    LinkList *list;
    createList(list, 4);
    display(list);
    delete(list, 3);
    printf("删除后的元素\n");
    display(list);
    insert(list, 2, 3);
    printf("插入后的元素\n");
    display(list);
    return 0;
}

【数据结构-链表】单链表

标签:单链表

原文地址:http://blog.csdn.net/ttf1993/article/details/45671673

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!