-1

私の問題は、int hで指定された特定の位置にユーザーが入力したことに基づいて、リンクリストに文字を挿入しようとしていることです...しかし、プログラムを実行するたびに、リストの2番目の文字だけが変更されます。ユーザーがプログラムに入れる番号。

Example: 
 ./h
 Name: koala
 a<-l<-a<-o<-k
 Change the position: 2
 To the character: 3
 a<-l<-3<-o<-k
 Insert the Character: F
 To the Postion: 3
 a<-F<-l<-3<-o<-k

のように見せたいです。

 ./h
 Name: koala
 a<-l<-a<-o<-k
 Change the position: 2
 To the character: 3
 a<-l<-3<-o<-k
 Insert the Character: F
 To the Postion: 3
 a<-l<-3<-F<-o<-k

私の問題はlists.cppのinsert_char()関数にあることは知っていますが、何が間違っているのか理解できません...

List.h

#include <iostream>
using namespace std;
struct Node;
Node* new_list();
void insert_front(Node** plist,char x);
void insert_char(Node* plist, char x, int p);
void change_char(Node* plist, char x, int p);
void print_list(Node* list);
void delete_front(Node** plist);
void delete_list(Node** plist);
//void delete_char(Node* plist,int p);
struct Node {
  char x;
  Node *next;
};

main.cpp

struct Node {
  char x;
  Node *next;
};

int main(){
  Node *list;
  list = new_list(); //new empty list
  cout<<"Name: ";
  string name;
  cin>> name;
  for (int i =0; i < name.length(); i++)
  insert_front(&list, name[i]);
  //---------print list-------------------------
  print_list(list);
  cout <<"Change the position: ";
  int z;
  cin>> z;
  cout<< "To the character: " ;
  char x;
  cin>> x;
  change_char(list, x, z);
  print_list(list);
  cout <<"Insert the Character: ";
  char y;
  cin>> y;
  cout<< "To the Postion: ";
  int h;
  cin>> h;
  insert_char(list, y, h);
  print_list(list);
  return 0;
}

lists.cpp

Node* new_list()
{
  Node* list = 0; //in C++ it is better to use 0 than NULL
  return list;
}
void insert_front(Node** plist,char x){
  Node* t;
  t = new Node;
  t->x = x;
  t->next = *plist;
  *plist = t;
  return;
}
void change_char(Node* plist, char x, int p)
{
  Node* s = plist;
  for (int i=1; i<p && 0!=s;i++)
    s = s->next;
  if (0 != s)
    s->x = x;
  return;
}
void insert_char(Node* plist, char x, int p){
  Node* s = plist;
  Node* a = new Node();
  for (int i=1; i<p && s; i++)
    a->next=s->next;
    s->next=a;
  if (0 !=s)
    a->x=x;
  return;
}

//void delete_char(Node* plist,int p)
void print_list(Node* list){
  Node* p;
  p = list;
  if(p == 0)
    cout << "--- empty list ---" << endl;
  while(p !=0){
    cout << p->x<<"<-";
    p = p->next;
  }
  cout << endl;
}
void delete_front(Node** plist){
  Node* t;
  if( *plist != 0){   // list is not empty
    t = (*plist);
    *plist = (*plist)->next;
    delete t;
  }
  }
void delete_list(Node** plist){
  while(*plist != 0) //while list not empty
    delete_front(plist);
}
bool is_empty(Node* list){
  return (list == 0);
}
4

1 に答える 1

1

for ループはinsert_char、文字を何度も「挿入」するだけです。s挿入の開始点 ( によって決定される)に進むつもりだったと思いますh

アップデート:

この部分はいくつかの理由で間違っています:

for (int i=1; i<p && s; i++)
  a->next=s->next;
  s->next=a;

インデントが誤解を招くことに注意してください。中括弧がないため、中央の行のみがループの一部です。効果的に、あなたは書いた:

for (int i=1; i<p && s; i++) {
  a->next=s->next;
}
s->next=a;

個人的には、ブロックが 1 つのステートメントだけで構成されている場合でも、ブロックには常にブレースを使用します。

a->nextそのため、行きたいリストのポイントに進むのではなく、たくさんの時間を設定します。

ループ内の新しい要素が必要な位置に進み、実際の挿入を行う必要があります。

// Advance s to index p.
for (int i = 1; i < p && s->next; i++) {
  s = s->next;
}
// Insert a at s.
a->next = s->next;
s->next = a;
于 2012-09-17T22:59:16.990 に答える