C ++编程主要帮助需要LINKED LISTS我只是看不到我的错误,虽然它正在运行 [英] C++ Programming major help needed LINKED LISTS I just can't see where my error is although it is running

查看:188
本文介绍了C ++编程主要帮助需要LINKED LISTS我只是看不到我的错误,虽然它正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include< iostream> 

using namespace std;

struct list
{
char name [20];
int age
double height;
list * next;
};

list * first = NULL,* current;
int optn = 0;

void currentfor()
{
if(current-> next == NULL)
cout< 列表已经结束! << endl
else
current = current-> next;
}

void currentbac()
{
if(current == first)
cout< 这是列表的开始。 << endl
else
{
list * previous;
previous = first;
while(previous-> next!= current)
{
previous = previous-> next;
}
current = previous;
}
}

void addbeginning()
{
list * newlist;

newlist = new list;
cout<< 输入你的名字: ;
cin>> newlist-> name;
cout<< 输入您的年龄:;
cin>> newlist-> age;
cout<< 输入您的身高:;
cin>> newlist-> height;
newlist-> next = first;
first = newlist;
}

void addending()
{
list * newlist,* newlist2;

newlist = new list;
cout<< 输入你的名字: ;
cin>> newlist-> name;
cout<< 输入你的年龄:;
cin>> newlist-> age;
cout<< 输入你的身高:;
cin>> newlist-> height;
newlist-> next = NULL;
if(first == NULL)
{
first = newlist;
current = first;
}
else
{
newlist2 = first;
while(newlist2-> next!= NULL)
{
newlist2 = newlist2-> next;
}
newlist2-> next = newlist;
}
}

void addmiddle()
{
if(current-> next = NULL)
addending
else
{
list * newlist;
newlist = new list;
cout<< 输入你的名字: ;
cin>> newlist-> name;
cout<< 输入您的年龄:;
cin>> newlist-> age;
cout<< 输入你的身高:;
cin>> newlist-> height;
newlist-> next = current-> next;
current-> next = newlist;
}
}

void deletebegin()
{
list * newlist;
newlist = first;
first = first-> next;
delete newlist;
}

void deletemiddle()
{
if(current-> next = NULL)
cout<这个。;
else
{
list * newlist;
newlist = current;
current = current-> next;
delete newlist;
}
}

void deleteend()
{
list * newlist,* newlist2;

if(first == NULL)
cout<< 列表结束< endl
else
{
newlist = first;
if(newlist-> next == NULL)
{
delete newlist;
first = NULL;
}
else
{
while(newlist-> next!= NULL)
{
newlist2 = newlist;
newlist = newlist-> next;
}
delete newlist;
newlist2-> next = NULL;
}
}
}

void display()
{
list * newlist;

newlist = first;
cout<< endl
do
{
if(newlist == NULL)
cout<< 列表结束<< endl
else
{
cout<< 名称是:< newlist-> name<< ;
cout<< 年龄是:< newlist-> age<< ;
cout<< 高度是:< newlist-> height;
cout<<< - 当前位置;
cout<< endl
newlist = newlist-> next;
}
}
while(newlist!= NULL);
cout<< 列表结束< endl
}


int main()
{
first = NULL;
do
{
display();
cout<< endl
cout<< 选择一个选项:< endl
cout<< 1.将当前位置向前移动一次。 << endl
cout<< 2.向后移动当前位置一次。 << endl
cout<< 3.在列表的开头添加成员。 << endl
cout<< 4.在列表的当前位置添加成员。 << endl
cout<< 5.在列表的结尾处添加成员。 << endl
cout<< 6.从列表中删除第一个成员。 << endl
cout<< 7.从列表中删除当前位置的成员。 << endl
cout<< 8.从列表中删除最后一个成员。 << endl
cout<< 9.结束程序。 << endl
cout<< endl<< >>;
cin>> optn;
switch(optn)
{
case 1:currentfor();
break;
case 2:currentbac();
break;
case 3:addbeginning();
break;
case 4:addmiddle();
break;
case 5:addending();
break;
case 6:deletebegin();
break;
case 7:deletemiddle();
break;
case 8:deleteend();
break;
}
}
while(optn!= 9);
}

我执行这个程序和3号工程。程序:(
有人可以给我显示正确的编码吗?
我做一个链接列表与节点填充名称高度和年龄,它应该为任何用户工作
和I m

解决方案

p>我更正了你的程序,并指出你应该注意在评论中的所有错误,请仔细看看他们,并试图看看你做错了,所以你可以从这个学习。

  #include< iostream> 

使用命名空间std;

struct list {
char名称[20];
int age;
double height;
list * next;
};

list * first = NULL,* current = NULL; // Init current为NULL,这样你可以测试当前是否设置为

int optn = 0;

void currentfor(){
if(current == NULL){
cout< 你还没有任何成员! << endl
} else {
if(current-> next == NULL)
cout<< 这是名单的结尾。 << endl
else
current = current-> next;
}
}

void currentbac(){
if(current == first)
cout< 这是列表的开始。 << endl
else {
list * previous;
previous = first;
while(previous-> next!= current){
previous = previous-> next;
}
current = previous;
}
}

void addbeginning(){
list * newlist;

newlist = new list;
cout<< 输入你的名字: ;
cin>> newlist-> name;
cout<< 输入您的年龄:;
cin>> newlist-> age;
cout<< 输入你的身高:;
cin>> newlist-> height;
newlist-> next = first;
first = newlist;

if(current == NULL)//将当前指针设置为first,因为这是你添加的第一个元素
current = first;
}

void addending(){
list * newlist,* newlist2;

newlist = new list;
cout<< 输入你的名字: ;
cin>> newlist-> name;
cout<< 输入您的年龄:;
cin>> newlist-> age;
cout<< 输入你的身高:;
cin>> newlist-> height;
newlist-> next = NULL;

if(first == NULL){
first = newlist;
current = first;
} else {
newlist2 = first;
while(newlist2-> next!= NULL){
newlist2 = newlist2-> next;
}
newlist2-> next = newlist;
}
}
void addmiddle(){
if(current-> next == NULL)//你在这里分配。使用==而不是=或者你将赋值给
// current-> next!这是不正确的。
addending();
else {
list * newlist;
newlist = new list;
cout<< 输入你的名字: ;
cin>> newlist-> name;
cout<< 输入您的年龄:;
cin>> newlist-> age;
cout<< 输入你的身高:;
cin>> newlist-> height;

newlist-> next = current-> next;
current-> next = newlist;
}
}

void deletebegin(){
list * newlist;
newlist = first;
first = first-> next;

//你需要先更新当前指针
if(newlist == current){
current = current-> next;
}

delete newlist;
}

void deletemiddle(){
list * newlist;
newlist = first;

//如果我们删除第一个元素
if(current == first){
list * deleteMe = first;
first = first-> next;

delete deleteMe;
current = current-> next;
} else {//否则
//搜索直到newlist-> next == current
// Als test for newlist!= NULL或者你将尝试从NULL获取下一个值 - >崩溃!
while(newlist!= NULL&& newlist-> next!= current)
newlist = newlist-> next;

if(newlist!= NULL){
delete newlist-> next;
newlist-> next = current-> next; //也从列表中的上一个节点更新下一个!或者它不会消失时显示

if(current-> next == NULL)//你在这里再次。使用==比较值,而不是=
current = first; //这并不意味着你不必删除如果
//你没有current-> next。如果你没有current-> next,
//只是将它设置为first。该元素确实需要删除。
else
current = current-> next;
}
}
}

void deleteend(){
list * newlist,* newlist2;

if(first == NULL)
cout<< 列表结束< endl
else {
newlist = first;

if(newlist-> next == NULL){
delete newlist;
first = NULL;
current = NULL; // current也应该为null
} else {
while(newlist-> next!= NULL){
newlist2 = newlist;
newlist = newlist-> next;
}

delete newlist;
newlist2-> next = NULL;
current = newlist2; //您忘记更新当前指针。
}
}
}

void display(){
list * newlist;

newlist = first;
cout<< endl

do {
if(newlist == NULL)
cout<< 列表结束<< endl
else
{
cout<< 名称是:< newlist-> name<< ;
cout<< 年龄是:< newlist-> age<< ;
cout<< 高度是:< newlist-> height;

if(current == newlist)//你需要检查你是否真的在当前位置
cout<<< - 当前位置;

cout<< endl
newlist = newlist-> next;
}
}
while(newlist!= NULL);

if(newlist!= NULL)//如果newList最初是NULL呢?您将打印两次。
cout<< 列表结束< endl
}


int main(void){
first = NULL;

do {
display();
cout<< endl
cout<< 选择一个选项:< endl
cout<< 1.将当前位置向前移动一次。 << endl
cout<< 2.向后移动当前位置一次。 << endl
cout<< 3.在列表的开头添加成员。 << endl
cout<< 4.在列表的当前位置添加成员。 << endl
cout<< 5.在列表的结尾处添加成员。 << endl
cout<< 6.从列表中删除第一个成员。 << endl
cout<< 7.从列表中删除当前位置的成员。 << endl
cout<< 8.从列表中删除最后一个成员。 << endl
cout<< 9.结束程序。 << endl
cout<< endl<< >>;
cin>> optn;
switch(optn){
case 1:currentfor();
break;
case 2:currentbac();
break;
case 3:addbeginning();
break;
case 4:addmiddle();
break;
case 5:addending();
break;
case 6:deletebegin();
break;
case 7:deletemiddle();
break;
case 8:deleteend();
break;
}
}
while(optn!= 9);
}


#include <iostream>

using namespace std;

struct list
{
  char name[20];
  int age;
  double height;
  list *next;
};

list *first = NULL, *current;
int optn = 0;

void currentfor()
{
  if (current->next == NULL)
    cout << "List has ended!" << endl;
  else
    current = current->next;
}

void currentbac()
{
  if (current == first)
    cout << "This is the beginning of the list." << endl;
  else
    {
      list *previous;
      previous = first;
      while (previous->next != current)
        {
          previous = previous->next;
        }
      current = previous;
    }
}

void addbeginning()
{
  list *newlist;

  newlist = new list;
  cout << "Enter your name:" ;
  cin >> newlist->name;
  cout << "Enter your age:" ;
  cin >> newlist->age;
  cout << "Enter your height:" ;
  cin >> newlist->height;
  newlist->next=first;
  first=newlist;
}

void addending()
{
  list *newlist, *newlist2;

  newlist = new list;
  cout << "Enter your name: ";
  cin >> newlist->name;
  cout << "Enter your age : ";
  cin >> newlist->age;
  cout << "Enter your height : ";
  cin >> newlist->height;
  newlist->next = NULL;
  if (first == NULL)
    {
      first = newlist;
      current=first;
    }
  else
    {
      newlist2 = first;
      while (newlist2->next != NULL)
        {
          newlist2 = newlist2->next;
        }
      newlist2->next = newlist;
    }
}

void addmiddle()
{
  if ( current->next=NULL)
    addending();
  else
    {
      list *newlist;
      newlist=new list;
      cout << "Enter your name:" ;
      cin >> newlist->name;
      cout << "Enter your age:" ;
      cin >> newlist->age;
      cout << "Enter your height:" ;
      cin >> newlist->height;
      newlist->next=current->next;
      current->next=newlist;
    }
}

void deletebegin()
{
  list *newlist;
  newlist = first;
  first = first->next;
  delete newlist;
}

void deletemiddle()
{
  if ( current->next=NULL)
    cout<<"There is no one after this.";
  else
    {
      list *newlist;
      newlist=current;
      current=current->next;
      delete newlist;
    }
}

void deleteend()
{
  list *newlist, *newlist2;

  if (first == NULL)
    cout << "End of list" << endl;
  else
    {
      newlist = first;
      if (newlist->next == NULL)
        {
          delete newlist;
          first = NULL;
        }
      else
        {
          while (newlist->next != NULL)
            {
              newlist2 = newlist;
              newlist = newlist->next;
            }
          delete newlist;
          newlist2->next = NULL;
        }
    }
}

void display()
{
  list *newlist;

  newlist = first;
  cout << endl;
  do
    {
      if (newlist == NULL)
        cout << "End of List" << endl;
      else
        {
          cout << "Name is: " << newlist->name << " ";
          cout << "Age is: " << newlist->age << " ";
          cout << "Height is: " << newlist->height;
          cout<<" <-- Current position ";
          cout<< endl;
          newlist = newlist->next;
        }
    }
  while (newlist!=NULL);
  cout << "End of list" << endl;
}


int main()
{
  first = NULL;
  do
    {
      display();
      cout << endl;
      cout << "Choose an option: " << endl;
      cout << "1. Move the current position forward once." << endl;
      cout << "2. Move the current position backwards once." << endl;
      cout << "3. Add a member at the beginning of the list." << endl;
      cout << "4. Add a member at the current position of the list." << endl;
      cout << "5. Add a member at the ending of the list." << endl;
      cout << "6. Delete the first member from the list." << endl;
      cout << "7. Delete the member at current position from the list." << endl;
      cout << "8. Delete the last member from the list." << endl;
      cout << "9. End program." << endl;
      cout << endl << " >> " ;
      cin >> optn;
      switch (optn)
        {
        case 1 : currentfor();
          break;
        case 2 : currentbac();
          break;
        case 3 : addbeginning();
          break;
        case 4 : addmiddle();
          break;
        case 5 : addending();
          break;
        case 6 : deletebegin();
          break;
        case 7 : deletemiddle();
          break;
        case 8 : deleteend();
          break;
        }
    }
  while (optn!= 9);
}

I execute this program and number 3 works.But the others seem to crash my program :( can someone please show me the correct coding to this? Im making a linked list with nodes filled with name height and age and it should work for any user. And I m supposed to display the entire list right?Or dont linked lists appear that way ? can some good soul please help?

解决方案

I corrected your program and indicated all errors you should pay attention to in the comments. Please take a good look at them and try to see what you did wrong, so you can learn from this.

#include <iostream>

using namespace std;

struct list {
   char name[20];
   int age;
   double height;
   list *next;
};

list *first = NULL, *current = NULL; // Init current to NULL so you can test whether current is set at all

int optn = 0;

void currentfor() {
   if(current == NULL) {
      cout << "You don't have any members yet!" << endl;
   } else {
      if (current->next == NULL)
         cout << "This is the end of the list." << endl;
      else
         current = current->next;
   }
}

void currentbac() {
   if (current == first)
      cout << "This is the beginning of the list." << endl;
   else {
      list *previous;
      previous = first;
      while (previous->next != current) {
         previous = previous->next;
      }
      current = previous;
   }
}

void addbeginning() {
   list *newlist;

   newlist = new list;
   cout << "Enter your name:" ;
   cin >> newlist->name;
   cout << "Enter your age:" ;
   cin >> newlist->age;
   cout << "Enter your height:" ;
   cin >> newlist->height;
   newlist->next = first;
   first = newlist;

   if(current == NULL) // Set the current pointer to first, because this is the first element you add
      current = first;
}

void addending() {  
   list *newlist, *newlist2;

   newlist = new list;
   cout << "Enter your name: ";
   cin >> newlist->name;
   cout << "Enter your age : ";
   cin >> newlist->age;
   cout << "Enter your height : ";
   cin >> newlist->height;
   newlist->next = NULL;

   if (first == NULL) {
      first = newlist;
      current=first;
   } else {
      newlist2 = first;
      while (newlist2->next != NULL) {
         newlist2 = newlist2->next;
      }
      newlist2->next = newlist;
   }
}
void addmiddle() {
   if (current->next == NULL) // You were assigning here. Use == instead of = or you will assign NULL to
                              // current->next! Which is incorrect.
      addending();
   else {
      list *newlist;
      newlist = new list;
      cout << "Enter your name:" ;
      cin >> newlist->name;
      cout << "Enter your age:" ;
      cin >> newlist->age;
      cout << "Enter your height:" ;
      cin >> newlist->height;

      newlist->next = current->next;
      current->next = newlist;
   }
}

void deletebegin() {
   list *newlist;
   newlist = first;
   first = first->next;

   // You need to update the current pointer first
   if(newlist == current) {
      current = current->next;
   }

   delete newlist;
}

void deletemiddle() {
   list *newlist;
   newlist = first;

   // If we delete the first element
   if(current == first) {
      list *deleteMe = first;
      first = first->next;

      delete deleteMe;
      current = current->next;
   } else { // Otherwise
      // Search until newlist->next == current
      // Als test for newlist != NULL or you will try to get a next value from NULL -> crash!
      while(newlist != NULL && newlist->next != current)
         newlist = newlist->next;

      if(newlist != NULL) {
         delete newlist->next;
         newlist->next = current->next; // Also update the next from the previous node in the list! Or it will not disappear when displaying

         if (current->next == NULL) // You did it again here. Use == for comparing values instead of =
            current = first; // It doesn't mean that you don't have to delete if
         // you don't have a current->next. If you don't have a current->next,
         // just set it to first. The element does need to be deleted.
         else
            current = current->next;
      }
   }
}

void deleteend() {
   list *newlist, *newlist2;

   if (first == NULL)
      cout << "End of list" << endl;
   else {
      newlist = first;

      if (newlist->next == NULL) {
         delete newlist;
         first = NULL;
         current = NULL; // Current should also be null
      } else {
         while (newlist->next != NULL) {
            newlist2 = newlist;
            newlist = newlist->next;
         }

         delete newlist;
         newlist2->next = NULL;
         current = newlist2; // You forgot to update the current pointer.
      }
   }
}

void display() { 
   list *newlist;

   newlist = first;
   cout << endl;

   do {
      if (newlist == NULL)
         cout << "End of List" << endl;
      else
         {
            cout << "Name is: " << newlist->name << " ";
            cout << "Age is: " << newlist->age << " ";
            cout << "Height is: " << newlist->height;

            if(current == newlist) // You need to check whether you really are at the current position
               cout<<" <-- Current position ";

            cout<< endl;
            newlist = newlist->next;
         }
   }
   while(newlist!=NULL);

   if(newlist != NULL) // What if the newList was initially NULL? You will print twice.
      cout << "End of list" << endl;
}


int main(void) {
   first=NULL;

   do {
         display();
         cout << endl;
         cout << "Choose an option: " << endl;
         cout << "1. Move the current position forward once." << endl;
         cout << "2. Move the current position backwards once." << endl;
         cout << "3. Add a member at the beginning of the list." << endl;
         cout << "4. Add a member at the current position of the list." << endl;
         cout << "5. Add a member at the ending of the list." << endl;
         cout << "6. Delete the first member from the list." << endl;
         cout << "7. Delete the member at current position from the list." << endl;
         cout << "8. Delete the last member from the list." << endl;
         cout << "9. End program." << endl;
         cout << endl << " >> " ;
         cin >> optn;
         switch (optn) {
            case 1 : currentfor();
               break;
            case 2 : currentbac();
               break;
            case 3 : addbeginning();
               break;
            case 4 : addmiddle();
               break;
            case 5 : addending();
               break;
            case 6 : deletebegin();
               break;
            case 7 : deletemiddle();
               break;
            case 8 : deleteend();
               break;
            }
      }
   while (optn!= 9);
}

这篇关于C ++编程主要帮助需要LINKED LISTS我只是看不到我的错误,虽然它正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C/C++开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆