• 締切済み

Cellの流れを教えてください

このプログラムの一部なのですが、 どこがどう行くという流れがいまいち想像できません。 kにheadを入れてくとkやheadの中身はどうなるのですか? よろしくお願いします。 Cell head = null; Cell k; k= new Cell(); k.data = 10; k.next = head; head = k; k= new Cell(); k.data = 100; k.next = head; head = k;

noname#85912
noname#85912
  • Java
  • 回答数1
  • ありがとう数0

みんなの回答

  • salsberry
  • ベストアンサー率69% (495/711)
回答No.1

上記の操作が全て終わった状態で、 - kは2番目にnewされたCellオブジェクトを指しています。 - headはkと同じオブジェクトを指しています。 -- head.dataの値は100です。 -- head.nextは1番目にnewされたCellオブジェクトを指しています。 --- head.next.dataの値は10です。 --- head.next.nextの値はnullです。 こういうのは絵を描くと分かりやすいですよ。 > kにheadを入れてくと kにheadを入れている部分はありません。念のため。

関連するQ&A

  • C++ 連結リストの結合について

    現在、C++で連結リストの結合を実現しようとしているのですが、自分のやり方ではどうしてもエラーになってしまい、他にうまい実現方法が思いつかないので質問させていただきます。 以下が私が作ったプログラムなのですが、どこをどう変えたらうまく行くのか、具体的に教えて頂けたら嬉しいです。結合関数はaddAll(List &l)です。よろしくお願いします。 #include <iostream> using namespace std; class Cell{ private: int data; Cell *next; public: Cell(int d, Cell *n = NULL){ data = d; next = n; } friend class List; }; class List{ private: Cell *head; public: List(){ head = NULL; } ~List(){ while (head != NULL)removeFirst(); } void addLast(int data); void removeFirst(); void print(); void addAll(List &l); }; void List::addLast(int data){ if (head == NULL){ head = new Cell(data); } else{ Cell *p = head; while (p->next != NULL){ p = p->next; } p->next = new Cell(data); } } void List::removeFirst(){ if (head == NULL){ return; } else if (head->next == NULL){ delete head; return; } Cell *removed = head; head = head->next; delete removed; } void List::print(){ if (head == NULL){ return; } for (Cell *p = head; p != NULL; p = p->next){ cout << p->data << ' '; } cout << endl; } void List::addAll(List &l){ if (l.head == NULL){ return; } else if (head == NULL){ if (l.head == NULL){ return; } else{ head = l.head; return; } } else{ Cell *p = head; while (p->next != NULL){ p = p->next; } p->next = l.head; } return; } int main(){ List l1; l1.addLast(1); l1.addLast(2); l1.addLast(3); l1.print(); List l2; l2.addLast(4); l2.addLast(5); l2.addLast(6); l1.addAll(l2); l1.print(); return 0; } 実行結果は 1 2 3 1 2 3 4 5 6 と表示されればOKです

  • 連結リストによるデータ管理プログラムの解説

    ★から★までのプログラムが、各行ごとにどのような動きをしているのか簡潔な言葉で説明を書いていただきたいのです。必要がないと判断した行はとばして下さって構いません。 (例) printf("hello!"); …hello!と表示 if(a==0){ …aが0なら #include <stdio.h> #include <stdlib.h> struct CELL{ struct CELL *next; char data; }; /* Head CELL CELL CELL +-------+ +-------+ +-------+ +-------+ | ? | *----> | 5 | *----> | 6 | *----> | 8 | / | +-------+ +-------+ +-------+ +-------+ */ main(void){★ struct CELL head; struct CELL *p, *wp; char a; head.next=NULL; printf("?\n"); scanf("%c %*c",&a); while(a!='0'){ printf("mode?\n"); scanf("%c",&mode); if(mode=="a"){ p=&head; while(p->next!=NULL){ p=p->next; } wp=(struct CELL *)malloc(sizeof(struct CELL)); if(wp==NULL){ printf("cannot allocate enough memory.\n"); return 0; } p->next=wp; p->next->data=a; p->next->next=NULL; printf("?\n"); scanf("%c %*c",&a); } if(mode=="p"){ printf("\n\nNow...\n"); p=&head; while(p->next!=NULL){ printf("%c --> \n",p->next->data); p=p->next; } printf("NULL\n"); if(mode=="d"){ p=&head; while(p->next->data==a){ p=p->next; } if(p==NULL) break; wp=p->next->next; while(p->next->data==a) p=wp;★ } } return 0; }

  • C++ リストの末尾要素の削除

    連結リストの末尾の要素を削除するプログラムを教えてください。 自分で何度も書き直してみたのですが、どうしてもうまくうごきません。 自分で書いた現状が以下のものですが、止まってしまいます。 void List::removeLast(){ if (head == NULL)return; Cell *p = head; Cell *prev = NULL; while (p!= NULL){ prev = p; p = p->next; } delete prev; } 以下のプログラムが動かすにはremoveLast()をどう変えたらよいのでしょうか? よろしくお願いします。 #include <iostream> using namespace std; // クラスの宣言 class Cell { friend class List; // Listクラスから,このクラスに自由にアクセスできるようにする private: int data; // データ Cell *next; // 次のセルのアドレスを指すポインタ public: Cell(int _data, Cell *n = NULL){ data = _data; next = n; } }; class List { private: Cell *head; // 連結リストの先頭要素のアドレスを指すポインタ public: List(){ head = NULL; }; ~List(){ while (head != NULL)removeFirst(); } void addFirst(int data){ head = new Cell(data, head); } void removeFirst(){ if (head == NULL)return; Cell *removed = head; head = head->next; delete removed; } void removeLast(); void print(){ for (Cell *p = head; p != NULL; p = p->next){ cout << p->data << ' '; } cout << endl; } }; // 連結リストの末尾要素を削除する関数 void List::removeLast() { //この部分です } int main() { List l1; for (int i = 0; i < 10; i++) { l1.addFirst(i); } l1.print(); l1.removeLast(); l1.print(); l1.removeLast(); l1.print(); l1.removeLast(); l1.print(); return 0; } 要求される実行結果は以下の通りです リストの内容: 9 8 7 6 5 4 3 2 1 0 リストの内容: 9 8 7 6 5 4 3 2 1 リストの内容: 9 8 7 6 5 4 3 2 リストの内容: 9 8 7 6 5 4 3

  • 双方向リストのバブルソートについて

    双方向リストをバブルソートを用いてソートしたいです。 下記がプログラム(一部)ですが、ソートした後にリスト表示すると 無限ループに陥ります。 どこがいけないのでしょうか。 #include <stdio.h> #include <stdlib.h> struct cell{ int data; struct cell *next, *prev; }; void insert_head(struct cell **head, int num){ struct cell *p, *p1; p = *head; p1 = make_cell(); *head = p1; p1->data = num; p1->next = p; if(p1->next != (struct cell *)NULL){ p1->next = p; p->prev = p1; } } void print_list(struct cell *head){ struct cell *p; p = head; printf("data = \n"); while(p != (struct cell *)NULL){ printf("%d\n", p->data); p = p->next; } } void sort_list(struct cell **head){ struct cell *p, *p2; int i, n; n = 0; p = *head; while(p->next != (struct cell *)NULL){ p = p->next; n++; } for(i = 0, p = *head; i < n-2; i++){ if(p->data > p->next->data){ if(p == *head){ *head = p->next; }else{ p->prev->next = p->next; } p2 = p->next; p->next = p->next->next; p->next->next = p; p->next->next->prev = p; p->next->prev = p->prev; p->prev = p2; }else p = p->next; } } int main(void){ struct cell *head = (struct cell *)NULL; int n; while(1){ printf("1:Insert head 2:Insert tail 3:Delete 4:List 5:Sort 6:Exit\n"); scanf("%d", &n); switch(n){ case 1: printf("num = "); scanf("%d", &n); insert_head(&head, n); break; case 2: printf("num = "); scanf("%d", &n); insert_tail(&head, n); break; case 3: printf("num = "); scanf("%d", &n); delete_cell(&head, n); break; case 4: print_list(head); break; case 5: sort_list(&head); break; case 6: return 0; break; } } }

  • パスを含むファイルリストを表示する際、 ASCIIコード順になるように

    パスを含むファイルリストを表示する際、 ASCIIコード順になるようにソートして表示するようにするプログラムを作っているのですが、リストの仕組がいまいちよくわからず、うまくソートできません。 /* insert_dirent_cell(): セルをリストの先頭に追加する * 引数: head -- リストの先頭のセルへのポインタ * cell -- リストに加えたいセルへのポインタ * 戻り値: リストの先頭のセルへのポインタ */ struct dirent_cell *insert_dirent_cell(struct dirent_cell *head,struct dirent_cell *cell) { struct dirent_cell *ins_buf; //最初に挿入 if(head == NULL) { head = cell; return cell; } //先頭に挿入 if(strcmp(head->name, cell->name) > 0) { cell->next = head; head->next = cell->next->next; return cell; } //途中に挿入 ins_buf = head; while(ins_buf->next != NULL) { if(strcmp(ins_buf->next->name, cell->name) > 0) { cell->next = ins_buf->next; ins_buf->next->next = cell->next->next; return cell; } ins_buf = ins_buf->next; } //最後に挿入 ins_buf->next = NULL; cell->next = ins_buf; return cell; }

  • C++ 連結リストの要素の削除について

    リストで任意の場所の要素を削除するプログラムを考えているのですが、どうしてもその部分のプログラムにmain関数通りの動きをさせる方法が分かりませんでした。いろいろ試してみたのですが、どこかしらでおかしくなってしまうのです。 以下に私がつくった途中までのプログラムを示すので、removeAt(int index)の中身の例を教えて頂きたいのです。よろしくお願い致します。 #include <iostream> using namespace std; // クラスの宣言 class Cell { friend class List; // Listクラスから,このクラスに自由にアクセスできるようにする private: int data; // データ Cell *next; // 次のセルのアドレスを指すポインタ public: Cell(int _data, Cell *n = NULL){ data = _data; next = n; } }; class List { private: Cell *head; // 連結リストの先頭要素のアドレスを指すポインタ public: List(){ head = NULL; }; ~List(){ while (head != NULL)removeFirst(); } void addFirst(int data){ head = new Cell(data, head); } void removeFirst(){ if (head == NULL)return; Cell *removed = head; head = head->next; delete removed; } void removeAt(int index); void print(){ for (Cell *p = head; p != NULL; p = p->next){ cout << p->data << ' '; } cout << endl; } }; // 連結リストの index 番目の要素を削除する関数 void List::removeAt(int index) { //この部分です } int main() { List l1; for (int i = 0; i < 10; i++) { l1.addFirst(i); } l1.print(); l1.removeAt(0); // リストの先頭要素で動作確認 l1.print(); l1.removeAt(2); l1.print(); l1.removeAt(4); l1.print(); l1.removeAt(17); // リストの要素数より大きな引数で動作確認 l1.print(); l1.removeAt(0); // リストの先頭要素で動作確認 l1.print(); l1.removeAt(6); // リストの要素数より大きな引数で動作確認 l1.print(); l1.removeAt(5); l1.print(); return 0; } 要求される実行結果は以下の通りです リストの内容: 9 8 7 6 5 4 3 2 1 0 リストの内容: 8 7 6 5 4 3 2 1 0 リストの内容: 8 7 5 4 3 2 1 0 リストの内容: 8 7 5 4 2 1 0 リストの内容: 8 7 5 4 2 1 0 リストの内容: 7 5 4 2 1 0 リストの内容: 7 5 4 2 1 0 リストの内容: 7 5 4 2 1

  • switchとwhile(1)、scanf()を組み合わせてプログラミングしたのですが

    C言語のリストの学習用に、次のようなプログラムを作成しました。 コマンドを入力することでリストを操作するプログラムです。 #include <stdio.h> #include <stdlib.h> typedef struct cell{ int data; struct cell *next; } CELL; CELL head; /* ダミーのセル */ void InsertCell(int x) /* 新しいセルの挿入用関数:iで出る */ { CELL *p, *new; new = (CELL *)malloc(sizeof(CELL)); new->data = x; for(p = &head ; p->next !=NULL ; p = p->next){ if(new->data < (p->next)->data) { new->next = p->next; p->next = new; return; } // xがすでにリストにあるとき、何もせず終了 else if(new->data == (p->next)->data) return; } new->next = NULL; p->next = new; } void Help() /* ヘルプ:hで出る */ { printf("コマンド説明\n"); printf("i : データを挿入\n"); printf("d : データを削除\n"); printf("p : リストを表示\n"); printf("x : プログラム終了\n"); } void PrintList() /* 画面表示用関数:pで出る */ { CELL *p; for(p = &head ; p->next !=NULL ; p = p->next){ printf("%d", (p->next)->data); printf("\n"); } } void DeleteCell(int x) /* 消去用関数:dで出る */ { CELL *p, *tmp; for(p = &head ; p->next !=NULL ; p = p->next){ if((p->next)->data == x) { tmp->next = p->next; p->next = (p->next)->next; free(tmp->next); return; } } printf("そのような数字はありません\n"); } int main(void) { int x; char s; printf("コマンドのヘルプはhを入力してください\n"); while(1){ printf("コマンドを入力してください(i,d,p,x):"); scanf("%c",&s); switch(s) { case 'i': printf("整数を入力してください:"); scanf("%d", &x); InsertCell(x); break; case 'd': printf("整数を入力してください:"); scanf("%d", &x); DeleteCell(x); break; case 'p': PrintList(); break; case 'h': Help(); break; case 'x': printf("終了します"); exit(1); break; default : printf("正しいコマンドを入力してください\n"); break; } } return 0; } これをBorland C++コンパイラでコンパイルしたのち実行すると、 コマンドのヘルプはhを入力してください コマンドを入力してください(i,d,p,x):i 整数を入力してください:10 コマンドを入力してください(i,d,p,x):正しいコマンドを入力してください コマンドを入力してください(i,d,p,x):i 整数を入力してください:100 コマンドを入力してください(i,d,p,x):正しいコマンドを入力してください コマンドを入力してください(i,d,p,x):d 整数を入力してください:10 コマンドを入力してください(i,d,p,x):正しいコマンドを入力してください コマンドを入力してください(i,d,p,x):p 100 コマンドを入力してください(i,d,p,x):正しいコマンドを入力してください コマンドを入力してください(i,d,p,x):x 終了します となり、何故かコマンドも特に入力していないのに 『コマンドを入力してください(i,d,p,x):正しいコマンドを入力してください』 の一文が挟まれます。 これはどういう事でしょうか。解決するにはswitch文をifにするしか手はないのでしょうか? どうかよろしくお願いします。

  • 線形リストに挿入するプログラム

    連結リストに要素を挿入する関数 insert( ) を関数を定義して,以下の条件の下で整数型の要素を連結リストに挿入するプログラムを作成しなければならないのですが、関数insert部分が見当がつきません. 回答よろしくおねがいします。 条件 1. 関数名を insert( ) とする. 2. 連結リストの先頭ノードを指すポインタ(*head)と,リストに挿入する要素(data)を引数とする. 3. 連結リストの先頭ノードを指すポインタ(*head)からたどって,要素がリスト内で降順(大きいものから小さいものへの順)となる位置に要素を挿入する. 4. 引数で示された要素が既にリストに存在する場合には,要素の挿入は行なわない. #include <stdio.h> #include <stdlib.h> typedef struct __cell { int data; struct __cell *next; } CELL; CELL *insert(CELL *head, int data); void showList(CELL *head); int main(void) { CELL *head; head = NULL; head = insert(head, 5); showList(head); head = insert(head, 2); showList(head); head = insert(head, 6); showList(head); head = insert(head, 4); showList(head); head = insert(head, 6); showList(head); head = insert(head, 4); showList(head); head = insert(head, 1); showList(head); return 0; } CELL *insert(CELL *head, int data) { /*ここで関数 insert( ) を定義します*/ } void showList(CELL *head) { CELL *p; for(p=head ; p!=NULL ; p=(*p).next) { printf("%d -> ", (*p).data); } printf("fin\n"); }

  • C言語 二分木探索

    今、int型の二分木にデータを追加する関数(引数は二分木へのポインタと追加する値、追加されたノードへのポインタを返す)をつくろうとしてます。 以前リストにデータを追加する関数をつくったのでそれを変更してつくろうとしているのですが途中までいってつまってしまいました。 つくりかたを教えてください。 よろしくお願いします! struct BinaryTreeNode{ int data; struct BinaryTreeNode *l_next; struct BinaryTreeNode *r_next; }; struct BinaryTree{ int node_num; struct BinaryTreeNode *root; }; BinaryTreeNode *BinaryTreeNodeAlloc(void) { BinaryTreeNode *node; node = (BinaryTreeNode *)malloc(sizeof(BinaryTreeNode)); if (node == NULL) { return (NULL); } node->l_next = NULL; node->r_next = NULL; return (node); } BinaryTreeNode *BinaryTreeDataAdd(BinaryTree *list, int x) { BinaryTreeNode *ptr; BinaryTreeNode *prev; BinaryTreeNode *new_node; ptr = list->root; prev = NULL; while (ptr) {       ←?? if (ptr->data < x) { prev = ptr; ptr = ptr->next; } else if (ptr->data == x) { return (NULL); } else { new_node = BinaryTreeNodeAlloc(); if (new_node == NULL) { exit (0);                ←?? } new_node->data = x; new_node->next = ptr; if (prev != NULL) { prev->next = new_node; } else { list->head = new_node; } list->node_num++; return (new_node); } } new_node = BinaryTreeNodeAlloc(); if (new_node == NULL) { exit (0); } new_node->data = x; new_node->r_next = NULL; new_node->l_next = NULL; if (prev != NULL) { prev->next = new_node; } else { list->head = new_node; } list->node_num++; return (new_node); }

  • 関数テンプレート(構造体付き)

    初歩的なことだと思うのですが、、 構造体の中の型に汎用性をもたせたいのですが、 template <typename DATA> struct cell { DATA data; DATA *next; }; cell<DATA> *insert(DATA x, cell<DATA> *init ) { cell<DATA> *p; r =new cell<DATA>; このような形かなと思ったのですが、エラーがかなりでます。 リストのプログラムで、挿入されるデータに汎用性をもたせたいのです。 お願いします。

専門家に質問してみよう