C语言实现链表的插入、删除、查询操作

来源:互联网 发布:算法竞赛宝典 百科 编辑:程序博客网 时间:2024/05/13 18:40

前言

本程序是数据结构上机实验内容,参考《数据结构(C语言版)》(清华大学出版社)中链表部分的伪代码实现。

题目要求

  1. 设计链表结构体
  2. 建立一个链表,并录入n个原始数据
  3. 实现向链表中指定位置插入数据的功能
  4. 实现删除链表中指定位置的数据的功能
  5. 实现获取链表中指定位置的数据的功能
  6. 使用malloc函数动态分配内存地址

实例代码及说明

#include <stdio.h>#include <stdlib.h>#define LIST_INIT_SIZE    5    //链表初始长度  #define ERROR   0#define OK      1//链表结构体struct LNode{    int data;    struct LNode *next;};//获取链表中指定位置的数据int GetElem_L(struct LNode *L, int i, int *e){    //L为带头结点的单链表的头指针。当第i个元素存在时,其赋值给e并返回OK,否则返回ERROR    struct LNode *p;    p= L->next;    int j = 1;    while (p&&j<i)    {        p = p->next;        ++j;    }    if (!p || j>i) return ERROR;    *e = p->data;    return OK;}//向链表中指定位置插入数据int ListInsert_L(struct LNode *L, int i, int *e){    struct LNode *p, *s;    p = L;    int j = 0;    while (p&&j<i - 1)    {        p = p->next;        ++j;    }    if (!p || j>i - 1) return ERROR;    s = (struct LNode *) malloc(sizeof(struct LNode));    s->data = *e;    s->next = p->next;    p->next = s;    return OK;}//删除链表中指定位置的数据int ListDelete_L(struct LNode *L, int i, int *e){    struct LNode *p, *q;    p = L;    int j = 0;    while (p->next && j<i - 1)    {        p = p->next;        ++j;    }    if (!(p->next) || j>i - 1) return ERROR;    q = p->next;    p->next = q->next;    *e = q->data;    free(q);    return OK;}//创建链表,录入n个原始数据void CreateList_L(struct LNode *L, int n){    int i;    int v;    struct LNode *p;    L->next = NULL;    //循环录入数据    for (i = n; i>0; --i)    {        p = (struct LNode *) malloc(sizeof(struct LNode));        scanf("%d", &v);        p->data = v;        p->next = L->next;  //头插法        L->next = p;    }}//输出链表中的数据int PrintList_L(struct LNode *L, char* s){    struct LNode *p;    printf("%s", s);    p = L->next;    if (p == NULL)    {        printf("链表为空\n");        return ERROR;    }    while (p != NULL){        printf("%d   ", p->data);        p = p->next;    }    printf("\n");    return OK;}int main(){    //定义链表    struct LNode L;    //初始化链表    printf("请输入链表中初始的%d个数据:", LIST_INIT_SIZE);    CreateList_L(&L, LIST_INIT_SIZE);    PrintList_L(&L,"初始化的链表:");    //定义插入/删除操作时位置和数值参数      int s, v;    //插入功能演示    printf("请输入数据插入的位置s 和数值v :");    scanf("%d%d", &s, &v);    printf("%s", ListInsert_L(&L, s, &v) ? "插入成功.\n" : "插入失败.\n");    PrintList_L(&L,"插入后的链表:");    //删除功能演示    printf("请输入数据删除的位置s :");    scanf("%d", &s);    if (ListDelete_L(&L, s, &v))        printf("删除成功.删除的数据是:%d\n", v);    else        printf("删除失败.位置有误.");    PrintList_L(&L,"删除后的链表:");    //查询功能演示    printf("请输入要查询的数据位置s :");    scanf("%d", &s);    GetElem_L(&L,s,&v);    printf("第%d个数是:%d\n\n",s,v);    system("pause");    return 0;}

运行结果

这里写图片描述

0 0