[英]Circular Doubly Linked List, Print Function
我需要创建一个带有哨兵节点的圆形双向链接列表,该节点应该从文件中读取数据并将其插入到列表中,而不是对其执行一些操作。 现在,我停留在一个简单的打印功能上,由于某种原因它不会从列表中打印。 文件中的数据采用字符串形式,例如:“热门排序算法,冒泡排序,合并排序,“空行”等
到目前为止,这是我的代码:
头文件包含:
typedef struct NODE {
struct NODE *prev;
char *value;
struct NODE *next;
} NODE;
typedef struct LIST {
int count;
struct NODE *next;
struct NODE *prev;
} LIST;
int InsertEnd(NODE *head, char * value, int *lineCount);
void printLines(int *lineCount);
void Traverse(NODE *head);
主要包含:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "header.h"
int main()
{
int lineCount = 0;
NODE *head;
head = (NODE *)malloc(sizeof(NODE)); /* creates head node dynamically */
head->next = NULL; /* points to first element */
head->prev = NULL; /* points to last element */
head->value = "HEAD"; /* not needed, but it was pretty useful when debugging */
//*********BEGIN OF OPEN FILE FUNCTION
FILE* fp;
char *fname = NULL;
fname = (char *)malloc(200); <<<<<===== I would prefer to set the size dynamically adjusting but I dont know how
printf("Reading file input.txt\n");
//Checks if the file us unable to be opened, then it shows the error message
if ( !(fp = fopen("input.txt", "r")))
{
printf("\nError, Unable to open the file for reading \n");
exit(100);
}
//*********BEGIN OF READ FROM FILE FUNCTION
while (!feof(fp))
{
fgets(fname, 150, fp); //reads the file and stores in buffer
fname[strlen(fname) - 1] = '\0'; // reduces empty strings for input
if (fname != '\0')
{
InsertEnd(head, fname, &lineCount);
//printf("%s\n", head->next->value); <<<<==== If uncomment this print function would work properly but only in this context
}
else
{
printf("Error'\n"); // For debugging
}
}
Traverse(head); // Print Function Should Be Working in Here
printf("Debugging print\n");
printLines(&lineCount); // Shows Line Count
return 0;
}
// Function inserts a new node at the end of the LIST
int InsertEnd(NODE *head, char * value, int* lineCount)
{
int lineCounter = *lineCount;
/* create new node */
NODE *newnode;
newnode = (struct NODE *)malloc(sizeof( struct NODE));
newnode->value = value;
/* placing new node in LIST */
if (head->next == NULL) /* LIST was empty */
{
newnode->next = head;
newnode->prev = head;
head->next = newnode;
head->prev = newnode;
lineCounter++; // Increment line counter
}
else /* LIST wasn't empty */
{
newnode->next = head;
newnode->prev = head->prev;
head->prev->next = newnode; /* adjust node that was previously last */
head->prev = newnode; /* adjust head node */
lineCounter++; // Increment line counter
}
*lineCount = lineCounter;
return lineCount;
}
// This function prints how many lines there are in the LIST, but I need to get rid of the empty spaces
void printLines(int *lineCount)
{
printf("Line counter is %d", *lineCount); // Shows the number of lines, but doesn't skip empty ones.
}
void Traverse(NODE *head)
{
NODE *current = head;
printf("Forward:");
while (current!= head->prev)
{
printf("%s \n", current->value);
current = current->next;
}
printf("\n");
}
因此,到目前为止,我有几个问题:
1)我最有可能需要摆脱列表中的空字符串。 有什么更好的方法,在阅读时摆脱它们或者在打印时不显示它们? 我该怎么做呢?
2)如何修复打印(遍历)功能,哪里出了问题?
3)此外,所有这些都应该通过菜单管理器进行,这将提示您输入命令(我认为这是对的)。 但是有些功能我不知道如何实现。 例如,当使用命中“ I”时,应调用Insert函数并提示用户输入另外两个值和,然后在适当的位置插入。 我该怎么做? 示例“ I 1 8”
4)与上一个类似,应该有List函数,该函数应该在特定值之间打印行。 用户输入格式应为“ L到”(含)列表。 示例“ L 2 5”
5)与前面类似,应该有一个删除功能,其格式为“ D”(包括)。 示例“ D 3 7”
6)最后是“ S”格式的“保存”功能,例如“ S output.txt”
感谢您的帮助!
我至少在您的代码中看到了这些问题,
在main()
if (fname != '\0')
这应该是
if (fname[0] != '\0')
在InsertEnd()
newnode->value = value;
应该
newnode->value = strdup(value);
在您的代码中应该有一些正确性,这非常有帮助,因为首先您需要根据您的请求动态分配缓冲区,但不知道file
长度,因此可以通过此正确性来实现
int sz;
printf("Reading file input.txt\n");
//Checks if the file us unable to be opened, then it shows the error message
if ( !(fp = fopen("sample.txt", "r")))
{
printf("\nError, Unable to open the file for reading \n");
exit(100);
}
fseek(fp, 0L, SEEK_END);
sz = ftell(fp);
printf("size of file %d\n",sz);
fname = (char *)malloc(sz);
rewind(fp);
现在从文件中读取内容时,将fname
检查为\\0
,这是不正确的,我更正了while..loop
。
while (!feof(fp))
{
if(fgets(fname,256, fp) != 0)
{
fname[strlen(fname) - 1] = '\0'; // reduces empty strings for input
InsertEnd(head, fname, &lineCount);
}
else
{
printf("Error'\n"); // For debugging
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.