C/C++言語記述、データ構造における単一チェーンテーブルの基本操作例
シングルチェーンテーブルの基本操作については、ここに記録しておきますが、中に問題があり、後で修正します.
すべてのコードを完全に実行できます.
コードを入れろ!
実行:
ご指摘を惜しまないでください.ありがとう!
すべてのコードを完全に実行できます.
コードを入れろ!
// case05.cpp
// Created by roohom on 2019/1/8.
// 2019/1/8 13:16
//
// ,
#include
#include
//
typedef struct LNode{
int data;
struct LNode *next;
}LNode, *List;
/* ? ???
//
int dispList(LNode *L) //
{
LNode *i;
i = L;
if (i->next== nullptr) return 0;
do
{
printf("%d", i->data);
i = i->next;
}
while(i == nullptr);
printf("
");
}
? ???*/
int DisplayList(List l)
{
List p = nullptr;
p = l->next;
int k = 0;
while (p)
{
printf("%d ", p->data);
p = p->next;
k++;
}
if (k == 0)
{
printf(" ");
return false;
}
printf("
");
return 1;
}
// C
void tailCreatList(LNode *&C, int a[], int n){
LNode *s,*r; //s ,
int i;
C = (LNode *)malloc(sizeof(LNode)); // C
C->next = nullptr; //
r = C; //r
for(i = 0; idata = a[i];
r->next = s;
r = r->next;
}
r->next = nullptr; // a , NULL
printf(" ( ) !
");
}
// C
void headCreatList(LNode *&C, int a[], int n)
{
LNode *s; //s
int i;
C = (LNode *)malloc(sizeof(LNode));
C->next = nullptr; //
for (i = 0; i < n; ++i) {
s = (LNode *)malloc(sizeof(LNode));
s->data = a[i];
/* */
s->next = C->next; // s C
C->next = s; //C , s C
}
printf(" ( ) !
");
}
// C( ) x , , 1, 0
int findAndDelElem(LNode *C, int x)
{
LNode *p,*q;
p = C;
/* */
while(p->next!= nullptr){
if(p->next->data==x) {
break;
}
p = p->next;
}
/* */
if(p->next== nullptr)
return 0;
else
{
/* */
q = p->next;
p->next = p->next->next;
free(q);
/* */
return 1;
}
}
int main() {
LNode *C;
LNode *L;
printf("
");
int a[7] = {1, 2, 3, 4, 5, 6, 7};
int b[7] = {1, 2, 3, 4, 5, 6, 7};
printf("(1) C:
");
headCreatList(C, a, 7);
printf(" C:
");
DisplayList(C);
printf("(2) L:
");
tailCreatList(L, b, 7);
printf(" L:
");
DisplayList(L);
printf("(3) 3 , .
");
findAndDelElem(C, 3);
printf(" C:
");
DisplayList(C);
findAndDelElem(L, 3);
printf(" L:
");
DisplayList(L);
free(L);
free(C);
}
実行:
"D:\Clion Projects\Cream\Point_learn\cmake-build-debug\case05.exe"
(1) C:
( ) !
C:
7 6 5 4 3 2 1
(2) L:
( ) !
L:
1 2 3 4 5 6 7
(3) 3 , .
C:
7 6 5 4 2 1
L:
1 2 4 5 6 7
Process finished with exit code 0
ご指摘を惜しまないでください.ありがとう!