链接列表!!!请帮助

问题描述 投票:0回答:0

这个程序是为了制作一个整数链表,但是当我打印它时,它以相反的顺序给出了列表。这里有什么问题?谢谢

这是我写的插入打印的代码

template <class ItemType>
void UnsortedType<ItemType>::InsertItem(ItemType
item)
{
NodeType* location;
location = new NodeType;
location->info = item;
location->next = listData;
listData = location;
length++;
}
template <class ItemType>
void UnsortedType<ItemType>::printList(){
    ItemType x;
    for(int i=0;i<length;i++){
        GetNextItem(x);
        cout << x << " ";
    }
    ResetList();
    cout << endl;
}

这是主文件

int main(){
    UnsortedType<int> l1;
    l1.InsertItem(1);
    l1.InsertItem(5);
    l1.InsertItem(6);
    l1.InsertItem(10);
    l1.InsertItem(14);
    l1.InsertItem(20);
    l1.InsertItem(25);
    l1.InsertItem(31);
    l1.printList();
    return 0;
}
c++
© www.soinside.com 2019 - 2024. All rights reserved.