1.顺序表与链表
左侧为顺序表,右侧为链表,区别在于有无在内存中顺序存放数据。
2.顺序表初始化
顺序表结构:
typedef struct Table{
int* head;
int length;
int size;
}table;
顺序表初始化:
table initTable(){
Table t;
t.head = (int*)malloc(5*sizeof(int));
if(!t.head){
printf("init talbe error!");
exit(0);
}
t.length = 0;
t.size = 5;
return t;
}
顺序表遍历:
void displayTable(Table t){
for(int i=0;i<t.length;i++){
printf("%d ",t.head[i]);
}
printf("\n");
}
例子:
#include <stdio.h>
#pragma warning(disable:4996)
#define Size 5
typedef struct Table{
int* head;
int length;
int size;
}table;
Table initTable(){
Table t;
t.head = (int*)malloc(Size*sizeof(int));
if(!t.head){
printf("init talbe error!");
}
t.length = 0;
t.size = Size;
return t;
}
void displayTable(Table t){
for(int i=0;i<t.length;i++){
printf("%d ",t.head[i]);
}
printf("\n");
}
int main(){
Table t = initTable();
for(int i=0;i<Size;i++){
t.head[i] = i;
t.length++;
}
displayTable(t);
return 0;
}
3.顺序表操作-添加
Table addTable(Table t,int elem,int add){//elem插入数据,add插入位置
if(add<1||add>t.length+1){
printf("add error!\n");
exit(0);
}
if(t.length == t.size){
t.head = (int*)realloc(t.head,(t.size+1)*sizeof(int));
if(!t.head){
printf("realloc error!");
return t;
}
t.size++;
}
for(int i=t.length-1;i>=add-1;i--){
t.head[i+1] = t.head[i];
}
t.head[add-1] = elem;
t.length++;
return t;
}
4.顺序表操作-删除
Table delTable(Table t,int del){
if(del<1||del>t.length){
printf("delete error!");
exit(0);
}
for(int i=del;i<t.length;i++){
t.head[i-1] = t.head[i];
}
t.length--;
return t;
}
5.顺序表操作-顺序查找
int findTable(Table t,int elem){
for(int i=0;i<t.length;i++){
if(t.head[i]==elem){
return 1;
}
}
return 0;
}
6.顺序表操作-更改元素
Table changeTable(Table t, int elem, int newElem) {
int oldElem = findTable(t,elem);
if (!oldElem) {
printf("can not find the elemt!");
return t;
}
t.head[oldElem - 1] = newElem;
return t;
}
7.总结
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable:4996)
#define Size 5
typedef struct Table {
int* head;
int length;
int size;
}table;
Table initTable() {
Table t;
t.head = (int*)malloc((Size) * sizeof(int));
if (!t.head) {
printf("init talbe error!");
}
t.length = 0;
t.size = Size;
return t;
}
void displayTable(Table t) {
for (int i = 0; i < t.length; i++) {
printf("%d ", t.head[i]);
}
printf("\n");
}
table addTable1(Table t, int elem, int add) {
if (add<1 || add>t.length + 1) {
printf("add error");
exit(0);
}
if (t.length == t.size) {
t.head = (int*)realloc(t.head, (t.size+1)*sizeof(int));
if (!t.head) {
printf("realloc fail!");
return t;
}
t.size += 1;
}
for (int i = t.length - 1; i >= add - 1; i--) {
t.head[i + 1] = t.head[i];
}
t.head[add - 1] = elem;
t.length++;
return t;
}
Table addTable(Table t, int elem, int add) {//elem插入数据,add插入位置
if (add<1 || add>t.length + 1) {
printf("add error!\n");
exit(0);
}
if (t.length == t.size) {
t.head = (int*)realloc(t.head, (t.size + 1) * sizeof(int));
if (!t.head) {
printf("realloc error!");
return t;
}
t.size++;
}
for (int i = t.length - 1; i >= add - 1; i--) {
t.head[i + 1] = t.head[i];
}
t.head[add - 1] = elem;
t.length++;
return t;
}
Table delTable(Table t, int del) {
if (del<1 || del>t.length) {
printf("delete error!");
exit(0);
}
for (int i = del; i < t.length; i++) {
t.head[i - 1] = t.head[i];
}
t.length--;
return t;
}
int findTable(Table t, int elem) {
for (int i = 0; i < t.length; i++) {
if (t.head[i] == elem) {
return i+1;
}
}
return 0;
}
Table changeTable(Table t, int elem, int newElem) {
int oldElem = findTable(t,elem);
if (!oldElem) {
printf("can not find the elemt!");
return t;
}
t.head[oldElem - 1] = newElem;
return t;
}
int main() {
Table t = initTable();
for (int i = 0; i < Size; i++) {
t.head[i] = i;
t.length++;
}
displayTable(t);
printf("add 6 in 3\n");
t = addTable(t, 6, 3);
displayTable(t);
t = delTable(t, 3);
displayTable(t);
if (findTable(t, 3)) {
printf("find 3 success!\n");
}
else {
printf("find 3 fail!\n");
}
t = changeTable(t, 3, 6);
displayTable(t);
return 0;
}