c++创建链表

来源:百度知道 编辑:UC知道 时间:2024/06/18 12:20:36
void CreateList_L(LinkList&L,int n){
L=(LinkList)malloc(sizeof (LNode));
L->next =NULL;
for (i=n;i>0;--i){
p=(LinkList)malloc(sizeof (LNode));
scanf(&p->data);
p->next = L->next;L->next = p;
}
}
这是算法
完整的程序高手补充一下...谢谢
完整程序 的写一下啦
比如#include。。。。。。

#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; //这句话要加,否则最后会出错。结尾不为NULL,就没办法判断结束
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;

}

写好了,还免费赠送了一个链表输出函数哦
#include<stdlib.h>
#include<stdio.h>
struct LNode
{
int data;
struct LNode* next;
};
typedef struct LNode * LinkList;