南京邮电大学数据结构与算法实验1(教材p216 1~5题)
1.顺序表的初始化、查找、插入、删除、输出、撤销等操作
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct vector {
int size, count;
int* data;
} vector;
vector* getNewVector(int n) {
vector* p = (vector*)malloc(sizeof(vector));
p->size = n;
p->count = 0;
p->data = (int*)malloc(sizeof(int) * n);
return p;
}
int expand(vector* v) {
if (v == NULL) return 0;
printf("expand v from %d to %d\n", v->size, 2 * v->size);
int* p = (int*)realloc(v->data, sizeof(int) * 2 * v->size);
if (p == NULL) return 0;
v->data = p;
v->size *= 2;
return 1;
}
int insert(vector* v, int pos, int val) {
if (pos < 0 || pos > v->count) return 0;
if (v->size == v->count && !expand(v)) return 0;
for (int i = v->count - 1; i >= pos; i--) {
v->data[i + 1] = v->data[i];
}
v->data[pos] = val;
v->count += 1;
return 1;
}
int erase(vector* v, int pos) {
if (pos < 0 || pos >= v->count) return 0;
for (int i = pos + 1; i < v->count; i++) {
v->data[i - 1] = v->data[i];
}
v->count -= 1;
return 1;
}
void output_vector(vector* v) {
int len = 0;
for (int i = 0; i < v->size; i++) {
len += printf("%3d", i);
}
printf("\n");
for (int i = 0; i < len; i++) printf("-");
printf("\n");
for (int i = 0; i < v->count; i++) {
printf("%3d", v->data[i]);
}
printf("\n");
printf("\n\n");
return;
}
void clear(vector* v) {
if (v == NULL) return;
free(v->data);
free(v);
return;
}
int main() {
srand(time(0));
#define MAX_OP 20
vector* v = getNewVector(2);
for (int i = 0; i < MAX_OP; i++) {
int op = rand() % 4, pos, val, ret;
switch (op) {
case 0:
case 1:
case 2:
pos = rand() % (v->count + 2);
val = rand() % 100;
ret = insert(v, pos, val);
printf("insert %d at %d to vector = %d\n",
val, pos, ret);
break;
case 3:
pos = rand() % (v->count + 2);
ret = erase(v, pos);
printf("erase item at %d in vector = %d\n",
pos, ret);
break;
}
output_vector(v);
}
clear(v);
return 0;
}
2. 编写程序,完成带表头结点单链表的初始化、查找、插入、删除、输出、撤销等操作
3. 编写程序实现单链表的逆置操作
4. 编写程序实现将单链表排序成为有序单链表的操作
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
};
typedef struct HeaderList {
Node* head;
int n;
};
HeaderList* init_HeaderList() {
HeaderList* l = (HeaderList*)malloc(sizeof(HeaderList));
l->head = (Node*)malloc(sizeof(Node));
l->head->next = NULL;
l->n = 0;
return l;
}
int find(HeaderList* l, int val) {
if (l == NULL) return -1;
Node* p;
for (p = l->head->next; p != NULL; p = p->next) {
if (p->data == val) return 1;
}
return 0;
}
void insert(HeaderList* l, int val) {
if (l == NULL) return;
Node* p = (Node*)malloc(sizeof(Node));
p->data = val;
p->next = l->head->next;
l->head->next = p;
return;
}
void erase(HeaderList* l, int pos) {
if (l == NULL) return;
Node* p = l->head->next;
Node* q = l->head;
for (int i = 0; i < pos;i++) {
p = p->next;
q = q->next;
}
q->next = p->next;
free(p);
return;
}
void clear(HeaderList* l) {
if (l == NULL) return;
free(l->head);
free(l);
return;
}
void output(HeaderList* l) {
if (l == NULL) return;
Node* p;
printf("此链表为:\n");
for (p = l->head->next; p != NULL; p = p->next) {
printf("%3d ", p->data);
}
return;
}
void sort(HeaderList* l) {
if (l == NULL) return;
Node* p, * q;
for (p = l->head->next; p != NULL; p = p->next) {
for (q = p->next; q != NULL; q = q->next) {
if (p->data > q->data) {
int temp = p->data;
p->data = q->data;
q->data = temp;
}
}
}
return;
}
Node* reverse(Node* head) {
if (head == NULL || head->next == NULL) return head;
Node* new_head,*tail;
tail = head->next;
new_head = reverse(head->next);
head->next = tail->next;
tail->next = head;
return new_head;
}
int main(void) {
HeaderList* myList = init_HeaderList();
insert(myList, 10);
insert(myList, 20);
insert(myList, 30);
insert(myList, 40);
output(myList);
int valToFind = 20;
int found = find(myList, valToFind);
if (found) {
printf("\n%d 在链表中找到了。\n", valToFind);
}
else {
printf("\n%d 在链表中未找到。\n", valToFind);
}
int posToRemove = 2;
erase(myList, posToRemove);
printf("\n删除第%d个元素后的链表:\n", posToRemove+1);
output(myList);
printf("\n进行冒泡排序后的链表:\n");
sort(myList);
output(myList);
printf("\n逆置链表后的结果:\n");
myList->head->next = reverse(myList->head->next);
output(myList);
clear(myList);
return 0;
}
5.编写程序实现一元多项式的创建、输出、撤销以及两个一元多项式相加和相乘的操作
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct poly {
int coef;
int exp;
struct poly* next;
}poly;
typedef struct Polynominal {
poly* head;
}Polynominal;
poly* getNewNode(int coef, int exp) {
poly* p = (poly*)malloc(sizeof(poly));
p->coef = coef;
p->exp = exp;
p->next = NULL;
return p;
}
void sort(Polynominal* m) {
poly* p, * q, * temp = (poly*)malloc(sizeof(poly));
for (p = m->head->next; p != NULL; p = p->next) {
for (q = p->next; q != NULL; q = q->next) {
if (p->exp < q->exp) {
temp->exp = p->exp;
p->exp = q->exp;
q->exp = temp->exp;
temp->coef = p->coef;
p->coef = q->coef;
q->coef = temp->coef;
}
}
}
}
Polynominal* create_polynominal() {
Polynominal* p = (Polynominal*)malloc(sizeof(Polynominal));
p->head = getNewNode(-1, -1);
poly* head, * tail;
head = p->head;
tail = p->head;
printf("请输入一个多项式(通过指定指数为-1来结束输入)\n");
do {
int coef = 0;
int exp = 0;
printf("请输入系数:\n");
scanf("%d", &coef);
printf("请输入指数:\n");
scanf("%d", &exp);
if (exp < 0) break;
poly* p1 = getNewNode(coef, exp);
tail->next = p1;
tail = p1;
} while (1);
sort(p);
return p;
}
void print(Polynominal* p) {
poly* q;
for (q = p->head->next; q != NULL; q = q->next) {
if (q->next == NULL) {
printf("%d*x^%d", q->coef, q->exp);
}
else {
printf("%d*x^%d+", q->coef, q->exp);
}
}
printf("\n\n");
}
Polynominal* add(Polynominal* p1, Polynominal* p2) {
Polynominal* result = (Polynominal*)malloc(sizeof(Polynominal));
result->head = getNewNode(-1, -1);
poly* current1 = p1->head->next;
poly* current2 = p2->head->next;
poly* currentResult = result->head;
while (current1 != NULL && current2 != NULL) {
if (current1->exp > current2->exp) {
currentResult->next = getNewNode(current1->coef, current1->exp);
current1 = current1->next;
}
else if (current1->exp < current2->exp) {
currentResult->next = getNewNode(current2->coef, current2->exp);
current2 = current2->next;
}
else {
int sumCoefficients = current1->coef + current2->coef;
if (sumCoefficients != 0) {
currentResult->next = getNewNode(sumCoefficients, current1->exp);
}
current1 = current1->next;
current2 = current2->next;
}
currentResult = currentResult->next;
}
while (current1 != NULL) {
currentResult->next = getNewNode(current1->coef, current1->exp);
current1 = current1->next;
currentResult = currentResult->next;
}
while (current2 != NULL) {
currentResult->next = getNewNode(current2->coef, current2->exp);
current2 = current2->next;
currentResult = currentResult->next;
}
return result;
}
Polynominal* multiply(Polynominal* p1, Polynominal* p2) {
Polynominal* result = (Polynominal*)malloc(sizeof(Polynominal));
result->head = getNewNode(-1, -1);
poly* current1 = p1->head->next;
while (current1 != NULL) {
poly* current2 = p2->head->next;
poly* tempResult = result->head;
while (current2 != NULL) {
int productCoefficient = current1->coef * current2->coef;
int productExponent = current1->exp + current2->exp;
while (tempResult->next != NULL && tempResult->next->exp > productExponent) {
tempResult = tempResult->next;
}
if (tempResult->next != NULL && tempResult->next->exp == productExponent) {
tempResult->next->coef += productCoefficient;
}
else {
poly* newTerm = getNewNode(productCoefficient, productExponent);
newTerm->next = tempResult->next;
tempResult->next = newTerm;
}
current2 = current2->next;
}
current1 = current1->next;
}
return result;
}
int main(void) {
Polynominal* p1 = create_polynominal();
printf("请输入第2个多项式:\n");
Polynominal* p2 = create_polynominal();
Polynominal* p3 = add(p1, p2);
printf("相加结果为:\n");
print(p3);
Polynominal* p4 = multiply(p1, p2);
printf("相乘结果为:\n");
print(p4);
return 0;
}