有人可以幫助創建generic linkedlist
而不需要STL
。我如何聲明主要頭像。它是struct node <> * head?或者struct node * head?我有一個同時使用一個錯誤,這是像一個模板聲明在塊範圍在C++中創建一個通用鏈接列表,不需要stl
#include <iostream>
using namespace std;
template<class T>
struct node
{
T data;
struct node<T>* next;
};
template<class T>
void Push(struct node<T>** H,T dat)
{
struct node<T> * newnode=(struct node<T> *)malloc(sizeof(struct node<T>)) ;
newnode->data=dat;
newnode->next=*H;
*H=newnode;
}
int main() {
struct node<>* head=NULL;
struct node<>* current;
int a=10;
float f=10.1;
Push<int>(&head,a);
Push<float>(&head,f);
current=head;
while(current)
{
cout<<current->data;
current=current->next;
}
//code
return 0;
}
爲什麼不使用'new'和'delete'而不是'malloc'和'free'? – Rakete1111
在'Push()'中使用'node'以及'node'。 –
songyuanyao
我很熟悉c,因此我習慣了它 –