• 締切済み

線形リストについての質問

線形リストの第n番目の要素を削除した線形リストを結果として返す再帰的メソッド ListNode delete(ListNOde head,int n)を作成したのですが。線形リストがまったくわかりません。どなたか回答お願いいたします。 ちなみにListNodeのクラス定義は public class ListNode{ ListNode next; String word; } です。

みんなの回答

  • koko_u_
  • ベストアンサー率18% (459/2509)
回答No.1

>ListNode delete(ListNOde head,int n)を作成したのですが。 ではそのソースを補足に。 そして開発言語も補足して下さい。

doanobu
質問者

補足

開発言語はjavaです。 したではなくしたいでした。 すいません。

関連するQ&A

  • 線形リストについて。

    このたび線形リストを学習し、自分でサンプルコードを書くことにしました。 しかし、動かすことが出来ず、理解することが出来なかったため、ご相談致します。 コード: #include <stdio.h> #include <stdlib.h> #define N 10 typedef struct node *link; struct node { int item; link next; }; void main() { int i; link head ,t; head = NULL; //リストに要素を入れる。 for(i = 0,t = head; i < N; i++, t = t->next) { t = malloc(sizeof *t); t ->next = NULL; t ->item = i; } //リストを表示する for(t = head; t != NULL; t = t->next) { printf("%d\n", t->item); } } 実行結果: リストを表示するのfor文内、t->itemでエラー。 原因はheadを上手くポインタで指せていないことにあると思います。 お聞きしたいことは2点です。 1.なぜ意図したように上手く動かないのか。 2.このように動かしたい場合、何処を変更すれば良いのか。 以上です。 宜しくお願いします。

  • 線形リスト

    こんにちわ。今、大学で線形リストの勉強してるのですがよくわかりません。 ↓のプログラムで (1)10個のIDを受け取って受け取った順にリストを作るプログラム(各要素の値のアドレスとそのnextの値を表示してリストになっていることを確認する。) (2) (1)のプログラムを拡張して、リストを作ったあと、1から10までの間の整数nを入力として受け取り、リストのn番目の要素のIDを表示プログラムを書け。 の2つのことをしたいんですけど、どのようにすればいいのですか?どなたか回答おねがいします。 #include<stdio.h> #include<stdlib.h> struct list{ int ID; struct list *next; }; int main(int argc, char* argv[]) { struct list *top; top=(struct list *)malloc(sizeof(struct list)); //struct list 型の大きさの領域を1つ確保 //topがそこを指している状態にする //printf("IDを10個入力してください"); if(top == NULL){ //メモリが足りないと値がNULLになってします printf("メモリは確保できません\n"); exit(1); //強制終了。プログラムはココで終わってしまいます } top->next=NULL; top->ID=1; //ポインタtopが指している構造体のIDの値を変えている top->next=(struct list*)malloc(sizeof(struct list)); //もう1つ作ります //top->next==NULL (メモリ確保に失敗)の処理は省略 (top->next)->next=NULL; (top->next)->ID=5; //これで下図のような状態になります; return 0; }

  • C#の連結リストがわかりません。

    最近、C#をちゃんとやろうと思い、勉強のためのコードを見ていたのですが以下の連結リストについてわかりません。わからないところにコメントをつけました。呼び出しメソッドはList.Add(2);です。よろしくお願いいたします。 using System; using System.IO; class Node { public int elem; public Node next; public Node() : this(0, null){}//このメソッドがある意味がわかりません。 public Node(int val, Node next) { this.elem = val; this.next = next;//なぜか最初にnextにデバッグするとnullになりますが2回目デバッグからはNodeになります。そもそもなぜクラスにクラスを代入しているかもわかりません。 } } /// <summary> /// 連結リストクラス /// </summary> class List { public Node head; public List() { head = null; } public void Add(int val) { Node node = new Node(val, this.head); this.head = node; } }

  • 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

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

    連結リストに要素を挿入する関数 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++ 連結リストの要素の削除について

    リストで任意の場所の要素を削除するプログラムを考えているのですが、どうしてもその部分のプログラムに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

  • 線形リスト(C言語)

    線形リストでn番目のIDを表示するプログラムを作っていますが、なぜか実行すると強制終了してしまいます。 コンパイルエラーは起きていないので、原因がさっぱりわかりません。 どなたかご教授お願いします。 #include <stdio.h> #include <stdlib.h> struct list{ int ID; struct list *next; }; int main(void) { struct list *top, *a; int i, n, x; a = NULL; for(i = 1; i < 11; i++) { printf("%d番目のIDを入力: " ,i); scanf("%d" ,&x); top = (struct list *)malloc(sizeof(struct list)); top->ID = x; top->next = a; a = top; } printf("何番目のIDを表示しますか: "); scanf("%d" ,&n); for(i = 1; i < 11; i--) { if(i == n) printf("%d" ,top->ID); top = top->next; } free((top->next->next->next->next->next->next->next->next)->next); free((top->next->next->next->next->next->next->next)->next); free((top->next->next->next->next->next->next)->next); free((top->next->next->next->next->next)->next); free((top->next->next->next->next)->next); free((top->next->next->next)->next); free((top->next->next)->next); free((top->next)->next); free(top->next); free(top); return 0; }

  • c言語 片方向連結リスト

    c言語の片方向連結リストのプログラムについて質問があります. 下記のプログラムの関数int get_index(ListPtr l, int value)に以下のようなコードを書く.リストl において値value を持つセルの位置を返す.返り値は,最初のセルが値value を持っていれば0,次のセルが値value を持っていれば1,...,値value を持っているセルが存在しなければ–1とする. また,関数void add(ListPtr l, int index, int value)に以下のようなコードを書く.リストl の位置index に値value を持つセルを挿入.挿入前のリストに対して:index が0 のときは先頭に挿入,index が1 のときは(0から数えて)1番目のセルの前に挿入,index が2 のときは(0から数えて)2番目のセルの前に挿入,...,index がリストのサイズと等しいときはリストの末尾に挿入,それ以外の場合は何もしなくてよい. これらのコードはどのように書けばよいのでしょうか? #include <stdio.h> #include <stdlib.h> #include <assert.h> /* 連結リスト中のノードの構造体 */ struct node { int val; /* 値 */ struct node *next; /* 次ノード */ }; /* セルとそのポインタの型 */ typedef struct node Node; typedef Node *NodePtr; /* セルを一つ生成 */ NodePtr create_node(int v) { NodePtr n = NULL; n = malloc(sizeof(Node)); n->val = v; n->next = NULL; return n; } /* セルを表示 */ void print_node(NodePtr n) { if (n != NULL) { printf("<%d>", n->val); } else { printf("(null)"); } } /* 連結リストの構造体 */ struct list { /* 先頭セルへのポインタ */ NodePtr head; }; /* 連結リストとそのポインタの型 */ typedef struct list List; typedef List *ListPtr; /* 空の連結リストを生成 */ ListPtr create_list(void) { ListPtr l = NULL; l = malloc(sizeof(List)); l->head = NULL; return l; } /* 連結リスト l が空かどうか判定 */ int is_empty(ListPtr l) { return (l->head == NULL); } /* リスト l の内容を表示 */ void print_list(ListPtr l) { NodePtr n = NULL; if (is_empty(l)) { printf("(empty)\n"); return; } n = l->head; while (n != NULL) { print_node(n); n = n->next; } printf("\n"); } /* リスト l の先頭にセルを挿入 */ void add_first(ListPtr l, int val) { NodePtr n = NULL; n = create_node(val); n->next = l->head; l->head = n; } /* リスト l の先頭セルを削除 */ int delete_first(ListPtr l) { NodePtr n = NULL; int v; /* リストが空なら -1 を返す(負の値はリストに含まれないと仮定)*/ if (is_empty(l)) return -1; v = l->head->val; n = l->head; l->head = l->head->next; free(n); n = NULL; return v; } /* 連結リスト l のサイズを取得 */ int get_list_size(ListPtr l) { NodePtr n = NULL; int size; size = 0; n = l->head; while (n != NULL) { size++; n = n->next; } return size; } /* * 連結リスト l における index 番目のセルの値を取得 * (そのようなセルが存在しなければ -1 を返す) */ int get_value(ListPtr l, int index) { NodePtr n = NULL; if (index < 0) return -1; n = l->head; while (index > 0 && n != NULL) { n = n->next; index--; } return (n == NULL) ? -1 : n->val; } /* リスト l 中の全セルを削除(ループ版)*/ void delete_all(ListPtr l) { NodePtr n = NULL, m = NULL; n = l->head; while (n != NULL) { m = n; n = n->next; free(m); } l->head = NULL; } /* セル n 以降を全て削除(内部処理用の再帰関数)*/ void delete_rest(NodePtr n) { if (n->next != NULL) delete_rest(n->next); free(n); } /* リスト l 中の全セルを削除(再帰版)*/ void delete_all_recursively(ListPtr l) { if (l->head == NULL) return; delete_rest(l->head); l->head = NULL; } /* リスト l 全体を削除 */ #define delete_list(l) (delete_list0(l),l=NULL) void delete_list0(ListPtr l) { delete_all(l); free(l); } /* リスト l において値 val を持つセルの位置を返す */ int get_index(ListPtr l, int value) { return -1; } /* リスト l の位置 index に値 val を持つセルを挿入 */ void add(ListPtr l, int index, int value) { } /* 連結リストの使用例 */ int main(void) { FILE *fp = NULL; char buf[10]; int age; ListPtr l = NULL; l = create_list(); add_first(l, 28); add_first(l, 40); add_first(l, 33); add_first(l, 15); print_list(l); delete_first(l); print_list(l); delete_first(l); print_list(l); delete_first(l); print_list(l); delete_first(l); print_list(l); return 0; }

  • 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です

  • LISPのプログラミングについて

    リストと数nを引数として、リストの(n-1)番目の要素を返す関数を 再帰を使って定義するにはどうすればいいでしょうか?