- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要一些认真的帮助来理解 C++ 中的链表,我打算采用几周前使用数组结构编写的一个程序,并将它们转换为链表并添加几个新函数。我最担心的是我对链接列表没有信心,并且一直花时间在这里和其他网站上获取有关它们的知识。但我找不到可以帮助我解决我现在面临的问题的来源。
这是我的原始代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 100
struct YouTubeVideo {
char video_name[1024]; // YouTube video name
int video_ranking; // Number of viewer hits
char video_url[1024]; // YouTube URL
};
struct YouTubeVideo Collection[MAX];
int tail = 0;
//-- Forward Declaration --//
void printall();
void insertion();
void sort();
void branching(char option);
void menu();
void load_file();
void save_file();
int main()
{
char ch;
load_file();
printf("\n\nWelcome to CSE240: YouTube Classic Hits\n");
do {
menu();
fflush(stdin); // Flush the standard input buffer
ch = tolower(getchar()); // read a char, convert to lower case
branching(ch);
} while (ch != 'q');
return 0;
}
void menu()
{
printf("\nMenu Options\n");
printf("------------------------------------------------------\n");
printf("i: Insert a new favorite\n");
printf("p: Review your list\n");
printf("q: Save and quit\n");
printf("\n\nPlease enter a choice (i, p, or q) ---> ");
}
void branching(char option)
{
switch(option)
{
case 'i':
insertion();
sort();
break;
case 'p':
printall();
break;
case 'q':
save_file();
break;
default:
printf("\nError: Invalid Input. Please try again...");
break;
}
}
void insertion()
{
if(tail < MAX)
{
printf("\nWhat is the name of the video? (No spaces characters allowed)\n");
scanf("%s", Collection[tail].video_name);
printf("\nHow many viewer hits does this video have?\n");
scanf("%d", &Collection[tail].video_ranking);
printf("\nPlease enter the URL: ");
scanf("%s", Collection[tail].video_url);
tail++;
}
else
{
printf("\nERROR: Your collection is full. Cannot add new entries.\n");
}
}
void sort()
{
int i = 0, j = 0;
struct YouTubeVideo temp;
for(i = 0; i < tail; i++)
{
for(j = i+1; j < tail; j++)
{
if(Collection[i].video_ranking < Collection[j].video_ranking)
{
temp = Collection[i];
Collection[i] = Collection[j];
Collection[j] = temp;
}
}
}
//RA: I think it's easier (and faster) to assume your current list is already
// sorted and then insert your new element into the correct position. (You
// can show this maintains a sorted list by induction.)
printf("\nSorting Complete...\n");
}
void printall()
{
int i;
printf("\nCollections: \n");
for(i = 0; i < tail; i++)
{
printf("\nVideo Name: %s", Collection[i].video_name);
printf("\nRanking (Hits): %d", Collection[i].video_ranking);
printf("\nURL: %s", Collection[i].video_url);
printf("\n");
}
}
void save_file() {
FILE *fileName; // declare a pointer to File type
char ch;
int index = 0;
fileName = fopen("ranking.dbm", "wb"); // "b" for binary mode
// ìwî for write
if(fileName != NULL)
{
fwrite(&tail, sizeof(int), 1, fileName); // Write tail to the file for later retrieval.
for(index = 0; index < tail; index++)
{
fwrite(&Collection[index].video_name, 1024, 1, fileName);
fwrite(&Collection[index].video_ranking, sizeof(int), 1, fileName);
fwrite(&Collection[index].video_url, 1024, 1, fileName);
}
fclose(fileName);
}
else
printf ("ERROR: Could not open file for saving data !\n");
}
void load_file() {
FILE *fileName; // declare a pointer to File type
int index = 0;
fileName = fopen("ranking.dbm", "rb"); // "b" for binary mode
// ìrî for read
if(fileName != NULL) {
fread(&tail, sizeof(int), 1, fileName);
for(index = 0; index < tail; index++)
{
fread(Collection[index].video_name, 1024, 1, fileName);
fread(&Collection[index].video_ranking, sizeof(int), 1, fileName);
fread(Collection[index].video_url, 1024, 1, fileName);
}
fclose(fileName);
}
else
printf ("ERROR: Could not open file for loading data !\n");
}
这些是我应该做什么的确切说明:
将“YouTubeVideo”数组结构(集合)转换为链表。当条目插入链表时,程序必须对条目进行排序(按“video_name”)。 [30分](*注:如果链表未排序,您将失去10分。)
现在我已经尽我所能地尝试了,但我现在遇到了问题。
这是我尝试解决方案的代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
using namespace std;
#define MAX 100
struct YouTubeVideo
{
char name[1024]; // YouTube video name
int ranking; // Number of viewer hits
char url[1024]; // YouTube URL
};
struct YouTubeVideo Collection[MAX];
int tail = 0;
//-- Forward Declaration --//
void printall();
void insertion();
void branching(char option);
void menu();
int main()
{
char ch;
// TODO: Add code to load save data from file
cout << "\n\nWelcome to CSE240: YouTube Classic Hits\n";
do {
menu();
cin >> ch; // read a char, convert to lower case
cin.ignore();
ch = tolower(ch);
branching(ch);
} while (ch != 'q');
return 0;
}
void menu()
{
cout << "\nMenu Options\n";
cout << "------------------------------------------------------\n";
cout << "i: Insert a new favorite\n";
cout << "p: Review your list\n";
cout << "s: Search\n";
cout << "d: Delete an entry\n";
cout << "q: Save and quit\n";
cout << "\n\nPlease enter a choice (i, p, s, d, or q) ---> ";
}
void branching(char option)
{
switch(option)
{
case 'i':
insertion();
break;
case 'p':
printall();
break;
case 's':
// TODO: Add code to search for a particular node by name
break;
case 'd':
// TODO: Add code to remove a node
break;
case 'q':
// TODO: Add code to save data into a file
break;
default:
cout << "\nError: Invalid Input. Please try again...";
break;
}
}
void insertion() { // insert a new entry
struct YouTubeVideo *p, *temp;
p = (struct YouTubeVideo *) malloc(sizeof(struct YouTubeVideo)); if (p == 0) {
printf("out of memory\n"); return; }
printf("Enter Video name, Views, URL: \n"); scanf("%s", p->name); // p->name is array scanf("%d", &p->phone);
scanf("%s", p->ranking);
temp = head;
if ((head == NULL)||(strcmp(p->name, temp->name) <=0)) {
p->next = head;
head = p;
}
else {
while (temp->next != NULL) {
if (stricmp(p->name, temp->next->name) <=0) { p->next = temp->next;
temp->next = p;
return;
} else
temp = temp->next; }
p->next = NULL;
temp->next = p;
} }
void printall()
{
int i;
cout << "\nCollections: \n";
for(i = 0; i < tail; i++)
{
cout << "\nVideo Name: " << Collection[i].name << "\n";
cout << "\nRanking (Hits): " << Collection[i].ranking << "\n";
cout << "\nURL: " << Collection[i].url << "\n";
cout << "\n";
}
}
我遇到的问题是我的插入
,我收到错误未声明的标识符头
,并且YouTubeVideo 中没有名为 next 的成员
。我尝试在很多地方放置和声明它们,但似乎无法修复这些错误。
我真的很感谢您能给我一些帮助和任何可能的知识。我确实花了很大力气,但现在只是卡住了。
最佳答案
您需要实际实现一个链表。这看起来像是一项家庭作业,所以我想知道你对 C++ 的了解有多深。如果您还没有真正进行类和面向对象编程,那么解决此问题的最简单方法是将下一个和上一个对象添加到您的 YouTube 视频结构中。像这样:
struct YouTubeVideo {
char video_name[1024]; // YouTube video name
int video_ranking; // Number of viewer hits
char video_url[1024]; // YouTube URL
YouTubeVideo* next;
YouTubeVideo* previous;
};
接下来,您需要添加头声明。这将是 YouTubeVideo* 类型。添加第一个视频时,将头部设置为指向该视频。然后,每当您添加新视频时,请将头部视频的下一个指针设置为指向新视频,并且新视频上的前一个指针应指向头部视频。这是链接列表的开始,但您的解决方案仍然非常困惑。
如果我是你,我会看看一些链表类是如何实现的。这是我编写的第一个链表类的头文件:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "List.h"
class LinkedList : public List {
public:
LinkedList();
virtual double get(int index) const;
virtual void add(double item);
virtual void insert(int index, double item);
virtual double delete_item(int index);
virtual int size() const;
private:
class Node;
Node* first;
Node* last;
int listsize;
};
class LinkedList::Node{
public:
double value;
Node* next;
};
#endif
你可以看到这个列表中有一个Node类,这个类包含一个值double value
。如果您想使用此代码,则必须使您的节点类具有一个 YouTubeVideo* 值字段。
关于C 链表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12848633/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!