迭代链表并将每个值打印到屏幕C上
我的输入文件是迭代链表并将每个值打印到屏幕C上,c,linked-list,C,Linked List,我的输入文件是 1 2 3 4 5 我的输出应该是 1 -> NULL 2 -> 1 -> NULL 3 -> 2 -> 1 -> NULL 4 -> 3 -> 2 -> 1 -> NULL 5 -> 2 -> 3 -> 2 -> 1 -> NULL 这是我的职责 void printList(Node* first) { Node *temp; temp=first;
1
2
3
4
5
我的输出应该是
1 -> NULL
2 -> 1 -> NULL
3 -> 2 -> 1 -> NULL
4 -> 3 -> 2 -> 1 -> NULL
5 -> 2 -> 3 -> 2 -> 1 -> NULL
这是我的职责
void printList(Node* first)
{
Node *temp;
temp=first;
printf("elements in linked list are\n");
while(temp!=NULL)
{
printf("%d -> NULL\n",temp->value);
temp=temp->next;
}
}
这是完整的程序
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int value;
struct node* next;
}Node;
Node* createNode(int data);
Node* insertFront(Node* first, Node* newNode);
void printList(Node* first);
void deleteList(Node* first);
int main(int argc, const char **argv)
{
int numItems, ch;
FILE *fp;
numItems = 0;
fp = fopen(argv[1], "r");
if(fp == NULL)
{
fclose(fp);
return -1;
}
while ((ch = getc(fp)) != EOF)
{
if (ch = '\n') numItems++;
}
fclose(fp);
Node *first = NULL;
Node *newNode;
Node *Next;
int i;
for(i = 1; i <= numItems; i++)
{
newNode = createNode(i);
first = insertFront(first, newNode);
}
printList(first);
deleteList(first);
return 1;
}
Node* createNode(int data)
{
Node *newNode;
newNode = malloc(sizeof(Node));
newNode -> value = data;
newNode -> next = NULL;
return newNode;
}
Node* insertFront(Node* first, Node* newNode)
{
if (newNode == NULL) {
/* handle oom */
}
newNode->next=NULL;
if (first == NULL) {
first = newNode;
}
else {
Node *temp=first;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next=newNode;
first = newNode;
}
return first;
}
void printList(Node* first)
{
Node *temp;
temp=first;
printf("elements in linked list are\n");
while(temp!=NULL)
{
printf("%d -> NULL\n",temp->value);
temp=temp->next;
}
}
void deleteList(Node* first)
{
Node *temp;
temp=first;
first=first->next;
temp->next=NULL;
free(temp);
}
#包括
#包括
类型定义结构节点{
int值;
结构节点*下一步;
}节点;
节点*createNode(int数据);
节点*插入前端(节点*第一,节点*新节点);
作废打印列表(节点*第一);
作废删除列表(节点*第一);
int main(int argc,常量字符**argv)
{
int numItems,ch;
文件*fp;
numItems=0;
fp=fopen(argv[1],“r”);
如果(fp==NULL)
{
fclose(fp);
返回-1;
}
而((ch=getc(fp))!=EOF)
{
if(ch='\n')numItems++;
}
fclose(fp);
Node*first=NULL;
节点*新节点;
节点*下一步;
int i;
对于(i=1;i值=数据;
newNode->next=NULL;
返回newNode;
}
节点*insertFront(节点*first,节点*newNode)
{
if(newNode==NULL){
/*把手*/
}
newNode->next=NULL;
if(first==NULL){
第一个=新节点;
}
否则{
节点*温度=第一;
while(临时->下一步!=NULL)
{
温度=温度->下一步;
}
temp->next=newNode;
第一个=新节点;
}
先返回;
}
无效打印列表(节点*第一个)
{
节点*温度;
温度=第一;
printf(“链表中的元素是\n”);
while(temp!=NULL)
{
printf(“%d->NULL\n”,临时->值);
温度=温度->下一步;
}
}
无效删除列表(节点*第一个)
{
节点*温度;
温度=第一;
第一个=第一个->下一个;
temp->next=NULL;
免费(临时);
}
有人能给我指出正确的方向吗?我坐的是链表斗争巴士。提前谢谢。你有几个错误。由于这是一个学习作业,我将描述错误,而不是更正代码:
- 您正在为每个元素打印
;您应该仅在循环完成后打印它。换句话说,各行应该使用->NULL
格式,然后在循环完成后打印%d->“
“NULL\n”
- 您的
不正确:您需要一个循环,类似于deleteList
printList
- 您创建列表的方式看起来很可疑:您不需要读取文件,而是计算行数,然后根据行数创建一个包含
,0
,1
,…的列表。您应该将文件中的数据读取到列表中;您可以在进行计数的相同循环中执行此操作,因为链接列表不需要知道这些数据r长度在前面2
- 您正在为每个元素打印
;您应该仅在循环完成后打印它。换句话说,各行应该使用->NULL
格式,然后在循环完成后打印%d->“
“NULL\n”
- 您的
不正确:您需要一个循环,类似于deleteList
printList
- 您创建列表的方式看起来很可疑:您不需要读取文件,而是计算行数,然后根据行数创建一个包含
,0
,1
,…的列表。您应该将文件中的数据读取到列表中;您可以在进行计数的相同循环中执行此操作,因为链接列表不需要知道这些数据r长度在前面2
void printList(Node*first)
更改为如下内容:
while(temp!=NULL)
{
if( temp->next )
printf("%d ->",temp->value);
else
printf("%d -> NULL\n",temp->value);
temp=temp->next;
}
如果您希望打印看起来像您展示的示例,则应将
void printList(Node*first)
更改为类似以下内容:
while(temp!=NULL)
{
if( temp->next )
printf("%d ->",temp->value);
else
printf("%d -> NULL\n",temp->value);
temp=temp->next;
}
为了正确生成堆栈并确保在需要时正确删除,您需要修复
dasblinkenlight
的回答中提到的问题。完成后,您的具体问题,即如何根据需要打印输出:
1->NULL
2->1->NULL
以此类推,最好通过递归调用进行处理:
void printList (Node* stack) {
Node* pCur = stack; // you could just use stack, you don't have to have pCur
if (pCur) {
printList (pCur->next);
while (pCur->next) {
printf ("%d->", pCur->value)
pCur = pCur->next;
}
printf ("%d->NULL\n", pCur->value);
}
return;
}
为了正确生成堆栈并确保在需要时正确删除,您需要修复
dasblinkenlight
的回答中提到的问题。完成后,您的具体问题,即如何根据需要打印输出:
1->NULL
2->1->NULL
以此类推,最好通过递归调用进行处理:
void printList (Node* stack) {
Node* pCur = stack; // you could just use stack, you don't have to have pCur
if (pCur) {
printList (pCur->next);
while (pCur->next) {
printf ("%d->", pCur->value)
pCur = pCur->next;
}
printf ("%d->NULL\n", pCur->value);
}
return;
}
那么您会遇到什么错误呢?因为您正在经历读取该文件的麻烦(从未检查是否打开成功),也许将其内容存储在您的列表中是合适的。我个人没有错误,只是我在设置打印输出以正确显示时遇到了问题。那么您遇到了什么错误?因为您在读取该文件时遇到了麻烦(从未检查是否打开成功),也许将其内容存储在您的列表中是合适的。我个人没有错误,只是我在设置打印输出以正确显示时遇到了问题