数据结构复习

结构体数组

#include<stdio.h> 

// 定义:① 
struct student1 { 
	char name[20]; // 姓名
	int age; // 年龄 
	float score; // 分数
	char addr[30]; // 地址 
};

// 定义:②
struct student2 {
	char name[20]; // 姓名
	int age; // 年龄 
	float score; // 分数
	char addr[30]; // 地址 
} stu1, 
  stun[3] = {
		{"赵四", 19, 70.5, "SiChuan"},
		{"马东", 31, 88.5, "ShanXi"},
		{"李八", 71, 98.5, "HeNan"}
	};

// 定义:③
typedef struct {
	char name[20]; // 姓名
	int age; // 年龄
	char course[30]; // 课程
} teacher; 

int main() {
	// 访问方式:① 
	student1 stu = {
		"张三", 11, 95.5, "BeiJing Road"
	};
	printf("名字:%s\n", stu.name);
	printf("年龄:%d\n", stu.age); 
	printf("分数:%f\n", stu.score);
	printf("地址:%s\n", stu.addr); 
	
	// ----------------------------------------
	printf("----------------------------------------\n");
	
	// 访问方式: ②
	student2 *p = stun;
	for (; p < stun+3; p++) {
		printf("名字:%s\n年龄:%d\n分数:%f\n地址:%s\n",
			p->name, p->age, p->score, p->addr);
	}
	
	student2 *p1 = stun;
	for (; p1 < stun+3; p1++) {
		printf("名字:%s\n年龄:%d\n分数:%f\n地址:%s\n",
			(*p1).name, (*p1).age, (*p1).score, (*p1).addr);
	}
	
	for (int i = 0; i < 3; i++) {
		printf("名字:%s\n年龄:%d\n分数:%f\n地址:%s\n",
			stun[i].name, stun[i].age, stun[i].score, stun[i].addr);
	}
	
	// ----------------------------------------
	printf("----------------------------------------\n");
	// 访问方式:③ 
	teacher teach = {
		"张老师", 55, "英语"
	}; 
	printf("名字:%s\n年龄:%d\n课程:%s\n",
			teach.name, teach.age, teach.course);
	
	return 0;
} 

静态分配数组--定义

#define MaxSize 50

typedef struct {
	ElemType data[MaxSize];
	int length;
} SqList;

动态分配数组--定义

#define InitSize 100

typedef struct {
	ElemType *data;
	int MaxSize, length;
} SeqList;

顺序表操作

#include<stdio.h>

#define MaxSize 50

typedef int ElemType;
typedef struct SqList {
	ElemType data[MaxSize];
	int length;
};

bool ListInsert(SqList &L, int i, ElemType e) {
	// 保证代码的健壮性 
	if (i < 1 || i > L.length + 1) {
		return false;
	}
	if (L.length >= MaxSize) {
		return false;
	}
	
	for (int j = L.length; j >= i; j--) {
		L.data[j] = L.data[j-1];
	}
	
	L.data[i - 1] = e;
	
	// 注意:将元素插入后,将长度 +1 
	L.length++;
	
	return true;
}

bool ListDelete(SqList &L, int i, ElemType &e) {
	if (i < 1 || i > L.length) {
		return false;
	}
	
	e = L.data[i - 1];
	
	for (int j = i; j < L.length; j++) {
		L.data[j - 1] = L.data[j];
	}
	L.length--;
	
	return true;	
}

int main() {
	struct SqList list; 
	list.length = 0;
	
	ListInsert(list, 1, 1);
	ListInsert(list, 2, 2);
	ListInsert(list, 3, 3);
	
	for (int i = 0; i < list.length; i++) {
		printf("%d\n", list.data[i]);
	}
	
	int res;
	ListDelete(list, 1, res);
	printf("%d\n", res);
	
	return 0;
}

动态分配动态表操作

#include<stdio.h>
#include<malloc.h>

#define InitSize 10

typedef struct {
	int *data;
	int MaxSize;
	int length;
} SeqList;

// 初始化动态数组 
void InitList(SeqList &L) {
	L.data = (int *) malloc (InitSize * sizeof(int));
	L.length = 0;
	L.MaxSize = InitSize;
}

// 增加动态数组的长度 
void IncreaseSize(SeqList& L, int len) {
	int *p = L.data;
	L.data = (int *) malloc ((L.MaxSize + len) * sizeof (int));
	for (int i = 0; i < len; i++) {
		L.data[i] = p[i];
	} 
	L.MaxSize = L.MaxSize + len;
	free(p);
} 

int main() {
	SeqList L;
	InitList(L);
	
	IncreaseSize(L, 5);
	return 0;
}

头插法尾插法

#include<stdio.h>
#include<malloc.h>

#define maxsize 50

typedef int ElemType;

typedef struct LNode {
	ElemType data;
	struct LNode *next; 
} LNode, *LinkList;

// 头插法(建立单链表)
LinkList List_HeadInsert(LinkList &L) {
	LNode * s;
	int x;
	L = (LinkList) malloc (sizeof(LNode));
	L->next = NULL;
	
	printf("请输入要插入的值:\n");
	scanf("%d", &x);
	while (x != 9999) {
		s = (LNode *) malloc (sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		printf("请继续输入要插入的值:\n");
		scanf("%d", &x); 
	}
	return L; 
} 

// 尾插法(建立单链表)
LinkList List_TailInsert(LinkList &L) {
	int x;
	L = (LinkList) malloc (sizeof(LNode));
	L->next = NULL;
	LNode * s, *r = L;
	
	printf("请输入要插入的值:\n");
	scanf("%d", &x);
	while (x != 9999) {
		s = (LNode *) malloc (sizeof(LNode));
		s->data = x;
		r->next = s;
		
		r = s;
		
		printf("请继续输入要插入的值:\n");
		scanf("%d", &x); 
	}
	
	r->next = NULL;
	return L;
}

int main() {
	struct LNode *list;
	
	List_TailInsert(list);
	
	while (list->next) {
		printf("%d ", list->data);
		list = list->next;
	}
	 
	return 0;
}

单链表的插入删除

#include<stdio.h>
#include<malloc.h>

typedef int ElemType;
typedef struct LNode {
	ElemType data;
	struct LNode *next;
} LNode, *LinkList;

// 在第 i 个位置插入元素e(带头结点)
bool ListInsert(LinkList &L, int i, ElemType e) {
	if (i < 1) { // 健壮性 
		return false;
	}
	
	LNode* p;
	int j = 0;
	p = L;
	while (p != NULL && j < i - 1) {
		p = p->next;
		j++;
	} 
	
	if (p == NULL) {
		return false;
	}
	
	LNode *s = (LNode *) malloc (sizeof(LNode));
	s->data = e;
	
	s->next = p->next;
	p->next = s;
	return true;
}

// 遍历单链表
void printfList(LinkList L) {
	while (L->next != NULL) {
		printf("%d", L->data);
		L = L->next;
	}
} 



int main() {
	LNode* node;
	ListInsert(node, 1, 23);
//	printf("balabala2");
//	ListInsert(node, 2, 34);
//	printf("balabala3");
//	ListInsert(node, 3, 45);
//	printf("balabala4");
	
	printfList(node);
	return 0;
}
posted @ 2024-05-27 19:03  jsqup  阅读(2)  评论(0)    收藏  举报