数据结构——顺序表操作集
线性表线性表就是像线一样的一个接着一个的数据集组成的表。第一个数据集没有前驱,最后数据集一个没有后继。关系上是一对一的。顺序表用数组来实现存诸数据,这里我用了char型元素的数组,当然也可以用结构体元素的数组。这样数组中的每一个元素就都是结构了。数据最大数不超过MASIZE。表中元素开始为0,用listLength来纪录表中元素。#ifndef _HEAD_H#define _HEAD_H#def
·
线性表
线性表就是像线一样的一个接着一个的数据集组成的表。第一个数据集没有前驱,最后数据集一个没有后继。关系上是一对一的。

顺序表
用数组来实现存储数据,这里我用了char型元素的数组,当然也可以用结构体元素的数组。这样数组中的每一个元素就都是结构了。
数据最大数不超过MAXSIZE。表中元素开始为0,用listLength来记录表中元素个数。头文件:head.h
#ifndef _HEAD_H
#define _HEAD_H
#define MAXSIZE 20
typedef struct node{
char element[MAXSIZE];
int listLength;
}Node;
/*清除缓冲区*/
void clean();
/*开始添加数据*/
void createNum(Node* list);
/*删除指定位置上的数据*/
void deleteNum(Node* list, int n);
/*找到指定位置上的数据,并返回 */
int findNum(const Node* list, int n);
/*修改指定位置上的数据*/
void modify(Node* list, int n);
/*显示表(遍历)*/
void display(const Node * list);
/*输入函数,其中有判断越界*/
int input(int length);
/*在相应的位置插入数据*/
void insert(Node* list, int n);
#endif
操作集:operation.c
#include<stdio.h>
#include "head.h"
void display(const Node * list)
{
int length = list->listLength;
for (int i = 0; i < length; i++)
{
printf("\'%c\' ", list->element[i]);
}
printf("\n");
}
int input(int length)
{
printf("Please input a number:(don't above %d and above 0)\n", length);
int n;
while(scanf("%d", &n))
{
clean();
if (n <= length && n > 0)
break;
else
printf("Please input a number:(don't above %d and above 0)\n", length);
}
return n;
}
void createNum(Node* list)
{
if (list->listLength >= MAXSIZE)
{
printf("the list is full\n");
return ;
}
printf("Please input a character:");
char ch;
scanf("%c", &ch);
clean();
list->element[list->listLength] = ch;
list->listLength++;
}
void clean()
{
while(getchar() != '\n')
continue;
}
void deleteNum(Node* list, int n)
{
for (int i = n; i < list->listLength - 1; i++)
{
list->element[i] = list->element[i+1];
}
list->listLength--;
}
int findNum(const Node* list,int n)
{
return list->element[n];
}
void modify(Node* list, int n)
{
puts("Please input your number to modify it:");
char ch;
scanf("%c", &ch);
clean();
list->element[n] = ch;
}
void insert(Node* list, int n)
{
char ch;
printf("Please input the data what you want to insert:\n");
scanf("%c", &ch);
clean();
int length = list->listLength;
for (int i = length; i > n; i-- )
{
list->element[i] = list->element[i-1];
}
list->element[n] = ch;
list->listLength++;
}
主函数:main.c
#include<stdio.h>
#include<string.h>
#include"operation.c"
#include"head.h"
int main(void)
{
Node list;
list.listLength = 0;
printf("你想添加几个数据?");
int n;
scanf("%d", &n);
clean();
while(n--)
createNum(&list);
display(&list);
printf("找到你相应位置上的数据\n");
int location = input(list.listLength);
printf("the %d number is \'%c\'\n",location, findNum(&list, location - 1));
printf("删除指定位置上的数据\n");
location = input(list.listLength);
deleteNum(&list, location - 1);
display(&list);
printf("修改指定位置上的数据\n");
location = input(list.listLength);
modify(&list, location - 1);
display(&list);
printf("在指定位置上插入数据\n");
location = input(list.listLength);
insert(&list, location - 1);
display(&list);
return 0;
}

主要理解增删改查。在插入时要将插入位置后面的数据全部向后移动一个位置,以空出一个位置。而在删除后也要将删除位置后面的所有数据全部向前移动一个位置,以把那个位置填补。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)