#include "stdafx.h"
#include "stdlib.h"
#include "string.h"
#include"stdio.h"
struct Score {
char* no;
char* name;
int score;
};
template <typename T>
struct Node { //双向链表
struct Node* prev;
struct Node* next;
T data;//T *data;
};
template <typename T>
struct Tree {
struct Tree* left;
struct Tree* right;
struct Tree* parent;
T data;//T *data;
};
template <typename T>
void print_node(Node<T>* pnode) {
printf("pnode->prev = %X\t", pnode->prev);
printf("pnode->next = %X\t", pnode->next);
printf("pnode->data = %d\n", pnode->data);
}
template <typename T>
Node<T>* init_node(T data, Node<T>* prev, Node<T>* next) {
Node<T>* p = new Node<T>;
p->data = data;
p->prev = prev;
p->next = next;
return p;
}
Node<int> node0;
void strcopy(char** dest, char* src) {
char* p;
if (*dest != NULL) {
delete* dest;
}
p = (char*)malloc(strlen(src) + 1);
if (p != NULL) {
*dest = p;
strcpy_s(*dest, strlen(src) + 1, src);
}
return;
}
typedef struct Score* PScore;
typedef struct Node<struct Score*>* PNode;
/*
功能:链表节点插入
参数:
p1:插入位置(p1之前)
p2:待插节点
*/
void insert(struct Node<struct Score*>* p1, struct Node<struct Score*>* p2) {
if (p1 == NULL) {
printf("插入位置不能为NULL.\n");
return;
}
if (p2 == NULL) {
printf("待插节点不能为NULL.\n");
return;
}
//边界情况
//最左边
/*
if (p1->prev == NULL) {
p2->prev = p1->prev;
p2->next = p1;
p1->prev = p2;
p1->next =
}
*/
}
void displayScore(struct Score* p_score) {
printf("%s\t%s\t%d\n", p_score->no, p_score->name, p_score->score);
}
/*
功能:构建成绩节点
*/
struct Score* newScore(char* no, char* name, int score) {
struct Score* p_Score;
p_Score = (struct Score*)malloc(sizeof(struct Score));
if (p_Score == NULL)return NULL;
p_Score->no = p_Score->name = NULL;
strcopy(&p_Score->no, no);
strcopy(&p_Score->name, name);
p_Score->score = score;
return p_Score;
}
/*
构建链表节点
*/
struct Node<struct Score*>* new_Node(char* no, char* name, int score) {
struct Node<struct Score*>* p_DL_Node;
struct Score* p_Score;
//申请struct Score,初始化成绩数据
p_Score = newScore(no, name, score);
//申请struct DL_Node<struct Score*>
p_DL_Node = (struct Node<struct Score*>*) malloc(sizeof(struct Node<struct Score*>));
if (p_Score != NULL && p_DL_Node != NULL) {
//初始化链表节点
p_DL_Node->prev = p_DL_Node->next = NULL;
p_DL_Node->data = p_Score;
}
return p_DL_Node;
}
int main()
{
struct Score score = { NULL,NULL,0 };
//score = scores[0]; //结构体可以整体赋值
char m[9] = "zhangsan";
strcopy(&score.name, m);
strcopy(&score.no, m);
score.score = 80;
printf("no=%s\tname=%s\tscore=%d\n", score.no, score.name, score.score);
char n[5] = "张三";
strcopy(&score.name, n);
printf("no=%s\tname=%s\tscore=%d\n", score.no, score.name, score.score);
char s[7] = "李四光";
strcopy(&score.name, s);
printf("no=%s\tname=%s\tscore=%d\n", score.no, score.name, score.score);
/*
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) {
printf("文件写打开失败!");
return 1;
}
//fwrite()//二进制写
fprintf(fp, "%s", "大家好!");
fclose(fp);
*/
FILE* fp2 = fopen("data.txt", "r");
char strNo[100], strName[100];
int score;
if (fp2 == NULL) {
printf("文件读打开失败!");
return 1;
}
struct Score _score = { NULL,NULL,0 };//局部指针变量,其初值一般不为NULL,
struct Node<struct Score*>* p_head = NULL, * p_end = NULL;
struct Node<struct Score*>* p_new, * p_DL_Node;
while (!feof(fp2)) {
fscanf(fp2, "%s\t%s\t%d\n", strNo, strName, &score);
p_new = new_Node(strNo, strName, score);
//第一个节点
if (p_head == NULL) {
p_head = p_end = p_new;
}
else { //非第一个节点
p_end->next = p_new;
p_new->prev = p_end;
p_end = p_end->next;
}
/*
printf("%s\t%s\t%d\n", strNo, strName, score);
strcopy(&_score.no, strNo);
strcopy(&_score.name, strName);
_score.score = score;
//printf("%s\t%s\t%d\n", _score.no, _score.name, _score.score);
displayScore(&_score);
*/
}
p_DL_Node = p_head;
while (p_DL_Node != NULL) {
displayScore(p_DL_Node->data);
p_DL_Node = p_DL_Node->next;
}
p_DL_Node = p_end;
while (p_DL_Node != NULL) {
displayScore(p_DL_Node->data);
p_DL_Node = p_DL_Node->prev;
}
fclose(fp2);
getchar();
return 0;
}
该如何解决里面的报错呀?