【发布时间】:2010-07-26 16:09:45
【问题描述】:
我一直在编写一个程序来提高我对链表及其功能的了解。我想知道你们中的一些人是否可以查看我的代码并注意到您可能熟悉的任何错误,或者一般的错误。这些函数适用于我的测试函数,但显然,我并没有测试所有可能的场景。
// LinkedList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
struct node
{
int value;
node* next;
};
node* push(node*, int);
node* pop(node*);
node* pop(node*, int);
node* insert(node*, int, int);
void printList(const node*);
int _tmain(int argc, _TCHAR* argv[])
{
node* head = NULL;
for(int i = 1; i <= 15; ++i)
{
head = push(head, i);
}
for(int j = 14; j >= 0; j = j - 2)
{
head = pop(head, j);
printList(head);
}
head = pop(head);
head = insert(head, 4, 27);
printList(head);
cin.ignore();
}
node* push(node* read, int val)
{
node* write = read;
if(read == NULL)
{
read = new node;
read->next = NULL;
read->value = val;
cout << "Node Head: " << read->value << endl;
return read;
}
else
{
while(read->next != NULL)
{
read = read->next;
}
read->next = new node;
read->next->next = NULL;
read->next->value = val;
read = read->next;
cout << "Node Link: " << read->value << endl;
return write;
}
}
node* pop(node* read)
{
node* write = read;
if(read->next == NULL)
{
delete read;
read = NULL;
return write;
}
else
{
while(read->next != NULL)
{
if(read->next->next == NULL)
{
cout << "Pop: " << read->next->value << endl;
delete read->next;
read->next = NULL;
}
else
{
read = read->next;
}
}
return write;
}
}
node* pop(node* read, int pos)
{
node* write = read;
if(read->next == NULL)
{
delete read;
return write;
}
else
{
if(pos == 0)
{
node* old = read;
cout << "Pop: " << read->value << endl;
read = read->next;
delete old;
return read;
}
else
{
for(int i = 0; i < (pos - 1); i++)
{
read = read->next;
}
node* old = read->next;
cout << "Pop: " << old->value << endl;
read->next = read->next->next;
delete old;
return write;
}
}
}
node* insert(node* read, int pos, int val)
{
node* write = read;
for(int i = 0; i < (pos - 1); i++)
{
read = read->next;
}
node* ins = new node;
ins->value = val;
cout << "Insert: " << ins->value << endl;
ins->next = read->next;
read->next = ins;
return write;
}
void printList(const node* read)
{
if(read != NULL)
{
cout << "List Item: " << read->value << endl;
printList(read->next);
}
}
/****************OUTPUT*********************
Node Head: 1
Node Link: 2
Node Link: 3
Node Link: 4
Node Link: 5
Node Link: 6
Node Link: 7
Node Link: 8
Node Link: 9
Node Link: 10
Node Link: 11
Node Link: 12
Node Link: 13
Node Link: 14
Node Link: 15
Pop: 15
List Item: 1
List Item: 2
List Item: 3
List Item: 4
List Item: 5
List Item: 6
List Item: 7
List Item: 8
List Item: 9
List Item: 10
List Item: 11
List Item: 12
List Item: 13
List Item: 14
Pop: 13
List Item: 1
List Item: 2
List Item: 3
List Item: 4
List Item: 5
List Item: 6
List Item: 7
List Item: 8
List Item: 9
List Item: 10
List Item: 11
List Item: 12
List Item: 14
Pop: 11
List Item: 1
List Item: 2
List Item: 3
List Item: 4
List Item: 5
List Item: 6
List Item: 7
List Item: 8
List Item: 9
List Item: 10
List Item: 12
List Item: 14
Pop: 9
List Item: 1
List Item: 2
List Item: 3
List Item: 4
List Item: 5
List Item: 6
List Item: 7
List Item: 8
List Item: 10
List Item: 12
List Item: 14
Pop: 7
List Item: 1
List Item: 2
List Item: 3
List Item: 4
List Item: 5
List Item: 6
List Item: 8
List Item: 10
List Item: 12
List Item: 14
Pop: 5
List Item: 1
List Item: 2
List Item: 3
List Item: 4
List Item: 6
List Item: 8
List Item: 10
List Item: 12
List Item: 14
Pop: 3
List Item: 1
List Item: 2
List Item: 4
List Item: 6
List Item: 8
List Item: 10
List Item: 12
List Item: 14
Pop: 1
List Item: 2
List Item: 4
List Item: 6
List Item: 8
List Item: 10
List Item: 12
List Item: 14
Pop: 14
Insert: 27
List Item: 2
List Item: 4
List Item: 6
List Item: 8
List Item: 27
List Item: 10
List Item: 12
*******************************************/
【问题讨论】:
-
我不是想看看什么是最好的工具,我只是想改进我自己的工具,以便更好地理解这门语言。否则我只会使用向量。
-
这不是真正的 C++。在 C++ 中,会有一个 List 类,其中的函数充当方法。这是 C。
-
@Steven:C++ 并不要求您以面向对象的方式进行编程。这是一个有效的 C++ 问题。
-
让我用更实际的术语来说明这一点。只会 C 的人可以完全理解这段代码并提供帮助。不需要 C++ 知识,因为没有显着使用区分 C 和 C++ 的特性。这不是 C++ 代码,而是移植的 C 代码。
-
请停止将C 编译器无法编译的代码标记为C 代码。只懂 C 的人可以理解用 C++、Java、Python 和任何数量的其他语言编写的大部分程序。这也不会神奇地使这些程序成为 C 程序。
标签: c++ data-structures linked-list