#include <iostream>
#include <string>
// blank line(s) after includes
using namespace std; // some people will say to avoid this
// but I use it in examples for brevity
// blank line(s) around class def
class nodetype
{ // bracket on its own line
public: // non indented visibility specifier
nodetype(int value, nodetype *p) // constructor first declared in class
{
adata = value; // level of indentation for fn body
ptr = p; // spaces around operators like =
}
// blank line(s) between fns and vars
int adata;
nodetype *ptr;
};
// blank line(s) between class and fn
void LinkedListDelete(nodetype **start, int akey)
{
nodetype *current, **previous; // pointer *s are connected to vars
// blank line between section
previous = start;
current = *start;
// blank line between section
// I use blank lines a lot, they help
// me to organize my thoughts
while((current != NULL) && (akey != current->adata))
{ // indentation inside nested scope
previous = ¤t->ptr; // no space for unary operators like &
current = current->ptr; // assignments justified to same level
}
if (current != NULL)
{
*previous = current->ptr; // no space for unary *, space for =
delete current;
}
// more blank lines between sections
return;
}
void LinkedListPrint(nodetype *list) // no space for unary *
{ // brackets on their own lines
while (list != NULL) // space around !=
{
cout << "(Node: " << list->adata << ") ";
list = list->ptr; // spaces around <<
}
cout << endl;
}
int main()
{
nodetype *node = new nodetype(5, new nodetype(10, // justified stuff
new nodetype(7, new nodetype(14,
new nodetype(23, NULL)))));
// blank lines
cout << "Build linked list: ";
LinkedListPrint(node);
cout << "Removed node 7: ";
LinkedListDelete(&node, 7);
LinkedListPrint(node);
return 0;
}
私はあなたが提供したコードに基づいてこのコードを作成しました。それはまったく同じではありません、私はいくつかのことを変更しました、しかしそれはあなたが望むことをします。nodetypeの構造を推測する必要があり、便宜上コンストラクターを追加しました。私のスタイルの側面を指摘するコメントをいくつか追加しました。
最初に提供したコードよりも読みやすいことに注意してください。スタイルは重要です。XまたはYスタイルを使用する必要があると人々は言うでしょうが、本当に重要なのは、好きなスタイルを選び、それを一貫して守ることです。自分のコードをすばやく読み、理解するのが簡単になります。
信じてください。たくさんのコードを書いたとき、一度にすべてを思い出すことができなくなり、自分が何をしていたかをすばやく理解できることが不可欠です。