C++ 编程需要主要帮助链接列表我只是看不到我的错误在哪里,尽管它正在运行

2024-01-18

#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);
}

我执行这个程序,第 3 个程序有效。但其他程序似乎使我的程序崩溃:( 有人可以告诉我正确的编码吗? 我正在制作一个链表,其中的节点填充有姓名、身高和年龄,它应该适用于任何用户。 我应该显示整个列表,对吧?或者链接列表不是这样显示的? 有好心人可以帮忙吗?


我更正了你的程序,并在评论中指出了你应该注意的所有错误。请好好看看它们,看看你做错了什么,这样你就可以从中吸取教训。

#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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++ 编程需要主要帮助链接列表我只是看不到我的错误在哪里,尽管它正在运行 的相关文章

  • 删除文件的最后 10 个字符

    我想删除文件的最后 10 个字符 说一个字符串 hello i am a c learner 是文件内的数据 我只是希望该文件是 hello i am a 文件的最后 10 个字符 即字符串 c learner 应在文件内消除 解决方案 将
  • 结构化绑定中缺少类型信息

    我刚刚了解了 C 中的结构化绑定 但有一件事我不喜欢 auto x y some func is that auto正在隐藏类型x and y 我得抬头看看some func的声明来了解类型x and y 或者 我可以写 T1 x T2 y
  • 类型中的属性名称必须是唯一的

    我正在使用 Entity Framework 5 并且有以下实体 public class User public Int32 Id get set public String Username get set public virtual
  • 如何从 Visual Studio 将视图导航到其控制器?

    问题是解决方案资源管理器上有 29 个项目 而且项目同时具有 ASP NET MVC 和 ASP NET Web 表单结构 在MVC部分中 Controller文件夹中有大约100个子文件夹 每个文件夹至少有3 4个控制器 视图完全位于不同
  • free 和 malloc 在 C 中如何工作?

    我试图弄清楚如果我尝试 从中间 释放指针会发生什么 例如 看下面的代码 char ptr char malloc 10 sizeof char for char i 0 i lt 10 i ptr i i 10 ptr ptr ptr pt
  • 用于 FTP 的文件系统观察器

    我怎样才能实现FileSystemWatcherFTP 位置 在 C 中 这个想法是 每当 FTP 位置添加任何内容时 我都希望将其复制到我的本地计算机 任何想法都会有所帮助 这是我之前问题的后续使用 NET 进行选择性 FTP 下载 ht
  • 在 Unity 中实现 Fur with Shells 技术

    我正在尝试在 Unity 中实现皮毛贝壳技术 http developer download nvidia com SDK 10 5 direct3d Source Fur doc FurShellsAndFins pdf Fins 技术被
  • 为什么这个字符串用AesCryptoServiceProvider第二次解密时不相等?

    我在 C VS2012 NET 4 5 中的文本加密和解密方面遇到问题 具体来说 当我加密并随后解密字符串时 输出与输入不同 然而 奇怪的是 如果我复制加密的输出并将其硬编码为字符串文字 解密就会起作用 以下代码示例说明了该问题 我究竟做错
  • 如何定义一个可结构化绑定的对象的概念?

    我想定义一个concept可以检测类型是否T can be 结构化绑定 or not template
  • C 编程:带有数组的函数

    我正在尝试编写一个函数 该函数查找行为 4 列为 4 的二维数组中的最大值 其中二维数组填充有用户输入 我知道我的主要错误是函数中的数组 但我不确定它是什么 如果有人能够找到我出错的地方而不是编写新代码 我将不胜感激 除非我刚去南方 我的尝
  • 为什么使用小于 32 位的整数?

    我总是喜欢使用最小尺寸的变量 这样效果就很好 但是如果我使用短字节整数而不是整数 并且内存是 32 位字可寻址 这真的会给我带来好处吗 编译器是否会做一些事情来增强内存使用 对于局部变量 它可能没有多大意义 但是在具有数千甚至数百万项的结构
  • 复制目录下所有文件

    如何将一个目录中的所有内容复制到另一个目录而不循环遍历每个文件 你不能 两者都不Directory http msdn microsoft com en us library system io directory aspx nor Dir
  • 如何在 Linq to SQL 中使用distinct 和 group by

    我正在尝试将以下 sql 转换为 Linq 2 SQL select groupId count distinct userId from processroundissueinstance group by groupId 这是我的代码
  • 有没有办法让 doxygen 自动处理未记录的 C 代码?

    通常它会忽略未记录的 C 文件 但我想测试 Callgraph 功能 例如 您知道在不更改 C 文件的情况下解决此问题的方法吗 设置变量EXTRACT ALL YES在你的 Doxyfile 中
  • 对于某些 PDF 文件,LoadIFilter() 返回 -2147467259

    我正在尝试使用 Adob e IFilter 搜索 PDF 文件 我的代码是用 C 编写的 我使用 p invoke 来获取 IFilter 的实例 DllImport query dll SetLastError true CharSet
  • C# 中最小化字符串长度

    我想减少字符串的长度 喜欢 这串 string foo Lorem ipsum dolor sit amet consectetur adipiscing elit Aenean in vehicula nulla Phasellus li
  • DotNetZip:如何提取文件,但忽略zip文件中的路径?

    尝试将文件提取到给定文件夹 忽略 zip 文件中的路径 但似乎没有办法 考虑到其中实现的所有其他好东西 这似乎是一个相当基本的要求 我缺少什么 代码是 using Ionic Zip ZipFile zf Ionic Zip ZipFile
  • 现代编译器是否优化乘以 1 和 -1

    如果我写 template
  • 从 mvc 控制器使用 Web api 控制器操作

    我有两个控制器 一个mvc控制器和一个api控制器 它们都在同一个项目中 HomeController Controller DataController ApiController 如果我想从 HomeController 中使用 Dat
  • 如何确定 CultureInfo 实例是否支持拉丁字符

    是否可以确定是否CultureInfo http msdn microsoft com en us library system globalization cultureinfo aspx我正在使用的实例是否基于拉丁字符集 我相信你可以使

随机推荐