C链表错误 [英] C Linked List errors

查看:36
本文介绍了C链表错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在理解C ++中的链接列表时,我需要一些认真的帮助.我想拿一个几周前用数组结构编写的程序,将其转换为链接表并添加几个新函数.我最担心的是,我对链接列表没有信心,并且一直在这里和其他站点上花费时间来获取有关它们的知识.但是我找不到能帮助我解决我现在面临的问题的资源.

I need some serious help with understanding Linked Lists in C++ I am suppose to take a program that I wrote a couple weeks ago using array structures and convert them into linked lists and add a couple new functions. My big concern is I do not feel confident in linked lists and have been spending time on here and other sites gaining knowledge on them. But I cannot find a source that helps me relate to the problem I am facing right now.

这是我的原始代码:

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

这些是我应该做的确切指示:

These are the exact instructions for what I am suppose to do:

将"YouTubeVideo"数组结构(集合)转换为链接列表.程序必须将条目插入链接列表中(通过"video_name"对它们进行排序).[30分](*注:如果未对链表进行排序,您将损失10分.)

现在,我相信我可以按照目前的理解去尝试,但是现在我遇到了问题.

Now I have given it as good a go as I believe I can with my current understanding but I am running into a problem right now.

以下是我尝试解决的代码:

Here is the code with my attempt at a solution in it:

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

我遇到的问题是我的 inserttion ,我得到了错误未声明的标识符头,并且在YouTubeVideo中没有下一步命名的成员.我尝试将它们放在一堆地方并进行声明,但似乎无法修复这些错误.

The problem I am having is with my insertion I am getting error undeclared identifier head and no member named next in YouTubeVideo. I tried placing and declaring them in a bunch of places but cannot seem to fix these errors.

我真的很感谢您的帮助和任何可能给我的知识.我确实为此付出了很大的努力,但此刻只是停留在那儿.

I would really appreciate some help and any possible knowledge you could bestow upon me. I really have given this a big go, but am just stuck fro the moment.

推荐答案

您实际上需要实现一个链表.这看起来像是一项家庭作业,所以我想知道您对C ++有多远.如果您还没有真正在做类和面向对象的编程,那么解决此问题的最简单方法就是在您的youtube视频结构中添加下一个和上一个对象.像这样:

You need to actually implement a linked list. This looks like a homework assignment, so im wondering how far into c++ you are. If you arent really doing classes and Object oriented programming yet, the easiest way to fix this is going to be to add a next and previous object to your youtube video struct. like this:

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

接下来,您将需要添加一个head声明.这将是YouTubeVideo *类型.添加第一个视频时,将头部设置为指向该视频.然后,每当之后添加新视频时,都应将主视频的下一个指针设置为指向新视频,而新视频上的前一个指针应指向主视频.那就是您的链表的开始,但是您的解决方案仍然很混乱.

next, you will need to add a head declaration. this is going to be of type YouTubeVideo*. When you add the first video, set the head to point at thsi video. then, whenever you add a new video after that, set the next pointer of the head video to point to the new video, and the previous pointer on the new video should point to the head video. Thats the start of your linked list, but your solution is still very messy.

如果我是你,我将研究如何实现某些链表"类.这是我编写的第一个链表"类的头文件:

If I was you, I would look at how some Linked List classes are implemented. Here is the header file for the first Linked List class i wrote:

#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 *值的字段.

You can see that this list has a class Node in it, and this class contains a value double value. If you wanted to use this code, you would have to make your node class have a field that was YouTubeVideo* value.

这篇关于C链表错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆