Asked by: Susan Harrison218 viewsITNovember 9, 2018
void CreateList(NODE *head); // head is a pointer to the head node,
1 Answers
+5Votes
#include <iostream> #include"malloc.h" using namespace std; struct LinkList { int data; LinkList *next; }; void CreateList_L(LinkList *&L,int n) { int i; LinkList *p; L=(LinkList*)malloc(1); L->next=NULL; //This sentence should be added, otherwise there will be an error. The end is not NULL, there is no way to judge the end for (i=n;i>0;–i) { p=(LinkList*)malloc(1); scanf( "%d",&p->data); p->next=L->next; L->next=p; } } Int main() { LinkList *head; CreateList_L(head,5); LinkList *p=head->next; while(p!=NULL) { cout<<p->data<<" "; p=p->next; } return 0; }
+5Votes
#include <iostream>
#include"malloc.h"
using namespace std;
struct LinkList
{
int data;
LinkList *next;
};
void CreateList_L(LinkList *&L,int n)
{
int i;
LinkList *p;
L=(LinkList*)malloc(1);
L->next=NULL; //This sentence should be added, otherwise there will be an error. The end is not NULL, there is no way to judge the end
for (i=n;i>0;–i)
{
p=(LinkList*)malloc(1);
scanf( "%d",&p->data);
p->next=L->next;
L->next=p;
}
}
Int main()
{
LinkList *head;
CreateList_L(head,5);
LinkList *p=head->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
return 0;
}