新数据覆盖指针中的旧数据
对于我的C类的作业,我们被要求从标准输入读取数据,将这些字连同它们的位置(线和偏移量)一起放入二进制树中,修改二叉树,并根据位置在文件中打印二叉树。我在那里(我希望),但我遇到了问题,我的新数据一直覆盖我的旧数据 - 特别是,当我将它放入Insert数组时,新的Insert结构会覆盖旧的Insert结构,这意味着我的数组中有几个相同的条目。我不太清楚如何阻止它的发生,甚至是为什么发生这种情况,尽管我认为这可能是因为我将数据写入同一个指针而没有清除它。新数据覆盖指针中的旧数据
公平的警告:我们大部分时间都是在这个学期留给自己,我的代码看起来很错,我很惊讶它的工作原理。
#include <stdio.h> #include <stdlib.h>
#include <string.h>
#define s 5000
#define z 10
struct Position {
int line; // line number
int offset; // 1, 2, 3, ...: 1st, 2nd, 3rd, ... word on the line
struct Position *next;
};
struct TreeNode {
char *word;
struct Position *positions; // store positions where the word occurs
struct TreeNode *parent;
struct TreeNode *left;
struct TreeNode *right;
};
struct Insert {
char *word;
struct Position *pos;
};
struct RemoveWord {
char *word;
};
struct RemovePos {
struct Position *pos;
};
struct TreeNode *createNode(char *word, struct Position *pos) {
struct TreeNode *node = (struct TreeNode*) malloc(sizeof(struct TreeNode));
node->word = malloc(s);
strcpy(node->word, word);
node->positions = pos;
node->parent = node->left = node->right = NULL;
return node;
}
struct Position *createPos(int line, int offset, struct Position *next) {
struct Position *node = (struct Position*) malloc(sizeof(struct Position));
node->line = line;
node->offset = offset;
node->next = next;
return node;
}
struct TreeNode *insertNode(struct TreeNode* tree, char *word, struct Position *pos) {
struct Position *tpos = (struct Position*) malloc(sizeof(struct Position));
if (tree == NULL) {
return createNode(word, pos);
}
if (strcmp(word, tree->word) < 0) {
tree->left = insertNode(tree->left, word, pos);
} else if (strcmp(word, tree->word) > 0) {
tree->right = insertNode(tree->right, word, pos);
} else if (strcmp(word, tree->word) == 0) {
tpos = tree->positions;
while(1) {
if (tpos->next == NULL) {
tpos->next = pos;
break;
}
tpos = tpos->next;
}
}
return tree;
}
struct TreeNode *min(struct TreeNode* tree) {
struct TreeNode* c = tree;
while (c->left != NULL) {
c = c->left;
}
return c;
}
struct TreeNode *removeWord(struct TreeNode* tree, char *word) {
if (tree == NULL) {
return tree;
}
if (strcmp(word, tree->word) < 0) {
tree->left = removeWord(tree->left, word);
} else if (strcmp(word, tree->word) > 0) {
tree->right = removeWord(tree->right, word);
} else {
if (tree->left == NULL) {
struct TreeNode *temp = tree->right;
free(tree);
return temp;
} else if (tree->right == NULL) {
struct TreeNode *temp = tree->left;
free(tree);
return temp;
}
struct TreeNode *temp = min(tree->right);
strcpy(tree->word, temp->word);
tree->right = removeWord(tree->right, temp->word);
}
return tree;
}
struct TreeNode *removePosition(struct TreeNode *tree, struct Position *pos) {
if (tree == NULL) {
return tree;
}
removePosition(tree->left, pos);
if (tree->positions->line == pos->line && tree->positions->offset == pos->offset) {
tree = removeWord(tree, tree->word);
return tree;
}
while(1) {
tree->positions = tree->positions->next;
if (tree->positions == NULL) {
break;
}
if (tree->positions->line == pos->line && tree->positions->offset == pos->offset) {
tree = removeWord(tree, tree->word);
return tree;
}
}
removePosition(tree->right, pos);
return tree;
}
struct TreeNode *removeLine(struct TreeNode *tree, int line) {
if (tree == NULL) {
return tree;
}
removeLine(tree->left, line);
if (tree->positions->line == line) {
tree = removeWord(tree, tree->word);
return tree;
}
while(1) {
tree->positions = tree->positions->next;
if (tree->positions == NULL) {
break;
}
if (tree->positions->line == line) {
tree = removeWord(tree, tree->word);
return tree;
}
}
removeLine(tree->right, line);
return tree;
}
//TESTING INORDER -- via sreekar2307 on github
void inorder(struct TreeNode* root) {
if(root==NULL) return ;
inorder(root->left);
printf("word is %s, line is %i, offset is %i\n",root->word, root->positions->line, root->positions->offset);
while(1) {
root->positions = root->positions->next;
if (root->positions == NULL) {
break;
}
printf("---> addition: line is %i, offset is %i\n", root->positions->line, root->positions->offset);
}
inorder(root->right);
}
int main() {
FILE *out;
char line[s];
int w = 1;
int l = 1;
int b = 1; //switch for before/after END
int I = 0; //switch for insert
int R = 0; //switch for remove word
int RL = 0; //switch for remove line
int INSERT = 0; // number of insert commands
int REMOVEWORD = 0; // number of removeword commands
int REMOVEPOS = 0; // number of removepos commands
int REMOVELINE = 0; // number of removeline commands
char *iw = malloc(s); //holds insert word
int il, io; //holds insert line, offset
int rwl, rwo; //holds remove word line, offset
struct Insert* ic[z];
struct RemoveWord* rwc[z];
struct RemovePos* rpc[z];
struct Insert *ic1 = (struct Insert *) malloc(sizeof(struct Insert));
struct RemoveWord *rwc1 = (struct RemoveWord *) malloc(sizeof(struct RemoveWord));
struct RemovePos *rpc1 = (struct RemovePos *) malloc(sizeof(struct RemovePos));
int rlc[z];
ic1->word = malloc(s);
rwc1->word = malloc(s);
rpc1->pos = (struct Position *) malloc(sizeof(struct Position));
struct Position *pos = (struct Position *) malloc(sizeof(struct Position));
struct Position *tpos = (struct Position *) malloc(sizeof(struct Position));
out = fopen("output.txt", "w+");
pos->next = NULL;
tpos->next = NULL;
struct TreeNode *ortree = NULL;
while (fgets(line, s, stdin)) {
char *word = malloc(s);
line[strcspn(line, "\n")] = 0;
for (word = strtok(line, " "); word; word = strtok(NULL, " ")) {
pos = createPos(l, w, NULL);
ortree = insertNode(ortree, word, pos);
w++;
if (strcmp(word, "END") == 0) {
b = 0;
}
if (l > 1 && b == 1 && I == 0 && R == 0 && RL == 0) {
printf("word is %s\n", word);
if (strcmp(word, "I") == 0) {
I = 1;
continue;
} else if (strcmp(word, "R") == 0) {
R = 1;
continue;
} else if (strcmp(word, "RL") == 0) {
RL = 1;
continue;
}
}
if (I == 1) {
iw = word;
I++;
continue;
}
if (I == 2) {
il = atoi(word);
I++;
continue;
}
if (I == 3) {
io = atoi(word);
tpos = createPos(il, io, NULL);
ic1->pos = tpos;
strcpy(ic1->word, iw);
ic[INSERT] = (struct Insert *) malloc(sizeof(struct Insert));
ic[INSERT] = ic1;
I = 0;
INSERT++;
continue;
}
if (R == 1) {
int r = atoi(word);
if (r == 0) {
strcpy(rwc1->word, word);
rwc[REMOVEWORD] = rwc1;
REMOVEWORD++;
R = 0;
continue;
} else {
rwl = r;
R++;
continue;
}
}
if (R == 2) {
rwo = atoi(word);
tpos = createPos(rwl, rwo, NULL);
rpc1->pos = tpos;
rpc[REMOVEPOS] = rpc1;
R = 0;
REMOVEPOS++;
continue;
}
if (RL == 1) {
rlc[REMOVELINE] = atoi(word);
RL = 0;
REMOVELINE++;
continue;
}
}
l++;
w = 1;
*line = '\0';
}
for (int n = 0; n < INSERT; n++) {
printf("ic[n]->word is %s\n", ic[n]->word);
ortree = insertNode(ortree, ic[n]->word, ic[n]->pos);
}
for (int n = 0; n < REMOVEWORD; n++) {
printf("rwc[n]->word is %s\n", rwc[n]->word);
ortree = removeWord(ortree, rwc[n]->word);
}
for (int n = 0; n < REMOVEPOS; n++) {
ortree = removePosition(ortree, rpc[n]->pos);
}
for (int n = 0; n < REMOVELINE; n++) {
ortree = removeLine(ortree, rlc[n]);
}
inorder(ortree);
fclose(out);
return 0;
}
输入被要求,所以这里是我们给出的十个文件之一。
input.1 output.out
RL 1
RL 7
R they
R Center
END
That's a slight uptick from a year ago, when a CNN poll found that 53 percent said they were dreading having to carry on such a conversation, with 43 percent saying they looked forward to such a dialogue.
"There's a sense of dread. It suggests some indigestion may be part of Thanksgiving dinner if politics come up," said Lee Miringoff, director of the Marist Institute for Public Opinion. "People you work with and go out with socially tend to share political views, but when you get to family, if politics is in the recipe, it may not taste very well."
That trepidation about broaching politics isn't coming from just one political party, but almost two-thirds of Democrats said they are, while only about half of Republicans were. (Fifty-six percent of independents also said so.)
President Trump is, unsurprisingly, a polarizing topic of potential dinner conversation. Forty-seven percent of people in the poll said they find it "stressful and frustrating" when talking with people who have a different opinion about the president than they do. (A Pew Research Center poll from June found 59 percent saying the same thing.)
回答:
我能够通过移动我的ic1
,rwc1
,rpc1
,pos
和tpos
初始化到它们被使用的循环,以解决上述问题。
以上是 新数据覆盖指针中的旧数据 的全部内容, 来源链接: utcz.com/qa/261158.html