学习时间与内容:
- 15:00-16:00链表学习
- 19:00——23:00链表学习,背包算法,栈
- 总学习5个小时
学习总结:
1 链表的继续学习,尝试指定删除链表中的第几位数,头插,尾插复习
#include <stdio.h>
#include <stdlib.h>
struct node {
int date;//数据域
struct node* next;//指针域
};
struct node* createList() {//创建一个链表
struct node*headnode = (struct node*)malloc(sizeof(struct node));//请求动态内存
// headnode->date = 1;
headnode->next = NULL;//头指针赋初值为空
return headnode;
};
//创建节点
struct node* createnode(int date) {//输入一个数据并存储在newnode的开头,称为创建节点
struct node* newnode = (struct node*)malloc(sizeof(struct node));//headnode为结构体变量,使用前需要初始化
newnode->date = date;//将date存储到当前节点的date域中
newnode->next = NULL;//头指针赋初值为空
return newnode;
};
void printflist(struct node* headnode) {//输出链表
struct node* pmove = headnode->next;//pmove赋值为链表头后的第一个数据
while (pmove) {
printf("%d ", pmove->date);//输出date中的值
pmove = pmove->next;//pmove指向链表下一个数据
}
printf("\n");
}
void insertnodebyhead(struct node* headnode, int date) {//链表--头插
struct node* newnode = createnode(date);
newnode->next = headnode->next;
headnode->next = newnode;
}
void insertnodenexthead(struct node* headnode, int date) { //链表--尾插
struct node* newnode = createnode(date);
node *p = headnode;
while (p->next != NULL) {
p = p->next;
}
p->next = newnode; //p最后一个有值的点的再下一个指向newnode
p = newnode;
}
void deletenodebyappoin(struct node* headnode, int posdate) {//链表的删除
struct node*posnode = headnode->next;//链表的第一个值
struct node* posnodefront = headnode;//暂时存储posnode的上一个值
if (posnode == NULL) {//判断链表是否为空
printf("链表为空");
} else {
while (posnode->date != posdate) {//遍历链表中的数,查找和posdate相同的数
posnodefront = posnode;
posnode = posnodefront->next;
if (posnode == NULL) {
printf("没有找到");
break;
}
}
posnodefront->next = posnode->next;
free(posnode);
}
}
void zhongjianshanchu(struct node *headnode, int txt) {//删除链表中第txt+1个值
struct node *posnode = headnode->next;
int t=0;
struct node *posnodefront = headnode;
if (posnode == NULL) {//判断链表是否为空
printf("链表为空");}
else{
while (t!=txt) {//至链表第txt+1个值
posnodefront = posnode;
posnode = posnodefront->next;
t++;
}
posnodefront->next = posnode->next;
free(posnode);
}
}
int main() {
struct node* list = createList();
insertnodebyhead(list, 1);//头插 ,插入一个数字在链表头部
insertnodebyhead(list, 5);
insertnodebyhead(list, 3);
insertnodenexthead(list, 9); //尾插, 插入一个数字在链表尾部
insertnodenexthead(list, 10);
zhongjianshanchu(list,1);
printflist(list);//输出链表
deletenodebyappoin(list, 5);//删除链表表中一个指定的数
printflist(list);
system("pause");//暂停程序的执行,等待任意健继续执行
return 0;
}
2 背包问题
态规划与分治法类似,都是把大问题拆分成小问题,通过寻找大问题与小问题的递推关系,解决一个个小问题,最终达到解决原问题的效果。但不同的是,分治法在子问题和子子问题等上被重复计算了很多次,而动态规划则具有记忆性,通过填写表把所有已经解决的子问题答案纪录下来,在新问题里需要用到的子问题可以直接提取,避免了重复计算,从而节约了时间,所以在问题满足最优性原理之后,用动态规划解决问题的核心就在于填表,表填写完毕,最优解也就找到。
#include<iostream>
using namespace std;
#include <algorithm>
#include<stdio.h>
int main() {
int w[5] = { 0, 2, 3, 4, 5 };//商品的体积2、3、4、5
int v[5] = { 0, 3, 4, 5, 6 };//商品的价值3、4、5、6
int bagV = 8; //背包大小
int dp[5][9] = { { 0 } };//动态规划表
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= bagV; j++) {//分能否放入背包两种情况考虑
if (j < w[i])
dp[i][j] = dp[i - 1][j];
else
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);//取最优解
}
}
//动态规划表的输出
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
printf("%d ",dp[i][j]);
}
printf("\n");
}
return 0;
}