array-based list question
Hello,
I'm trying to do the following. It's a code that corrects a sentence. That's one of the input files:
Code:
delete("is",2)
print 1:This 2:is 3:an 4:an 5:icorrect 6:sntence
delete("an",3)
print 1:This 2:is 3:an 4:icorrect 5:sntence
delete("icorrect",4)
print 1:This 2:is 3:an 4:sntence
insert("incorrect",4)
print 1:This 2:is 3:an 4:incorrect 5:sntence
delete("sntence",5)
insert("sentence",5)
print 1:This 2:is 3:an 4:incorrect 5:sentence
neighbors("is") 2:is previous:This next:an
So, first it's "This is is an an icorrect sntence", and after the code has sorted everything, it's gonna be "This is a correct sentence".
Because I don't know how large the input, I thought I would use this to get the input:
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
Now I don't know how to create the array and populate it. Will this work?
Code:
template <class elemType>
bool arrayListType<elemType>::isempty() const
{
return (length == 0);
}
template <class elemType>
bool arrayListType::isFull() const
{
return (length == maxSize);
}
template <class elemType>
int arrayListType<elemType>::listSize() const
{
return lenght;
}
template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
return maxSize;
}
Any help/input is appreciated! Thank you
Re: array-based list question
Hm, nobody knows? Maybe my question wasn't clear. If so, just let me know :)
Re: array-based list question
Not really clear, no. What is arrayListType and why can't you just use a std::vector or std::list?
Re: array-based list question
That's just the name, and I'm not sure if I can. I think I have to do it this way. I'm just having troubles populating the array.
I guess I would have to make dynamic memory allocation. Any idea how?
Re: array-based list question
Can't edit it anymore. Anyway, it's supposed to be array-based lists/linked lists. Wanted to add that.
Re: array-based list question
"Array-based linked list" is a contradiction in terms. Arrays and linked lists are fundamentally different.
I suppose you could implement a linked list using an array to "allocate" the memory, but that's overly complicated for a school assignment.
Re: array-based list question
I don't see how an array or linked list fits into this at all except that you might have a string which is an array of characters. The STL provides all sorts of containers such as std::list, std::deque, std::string, and so forth. I would be more concerned about your correction algorithm. I don't really understand your basic requirements at all. How does the program know what a correct sentence is?
Re: array-based list question
The commands are in the input file, as you can see in my 1st post.
I guess I could load the string, read each line command by command and perform the actions accordingly?
Re: array-based list question
Quote:
Originally Posted by
TheTrace
The commands are in the input file, as you can see in my 1st post.
I guess I could load the string, read each line command by command and perform the actions accordingly?
Sure, you could. What does that have to do with an array based list? It is a really strange looking input file. The last line doesn't make any sense at all based on what you indicated that the final answer is supposed to be.
Re: array-based list question
It needs to sort it. And I thought I could use an array-based list to solve it. But I now think linked lists are the way to go.
Re: array-based list question
So just load the elements into a std::vector and call std::sort....
Re: array-based list question
I'm just curious to what an array based list is? Is that some sort of variation of the classic deque implementation? Or a fancy way of saying vector?
Re: array-based list question
Quote:
Originally Posted by
monarch_dodra
I'm just curious to what an array based list is? Is that some sort of variation of the classic deque implementation? Or a fancy way of saying vector?
There are benefits for having an array in a linked-list. For starters, it is much easier to manage pointers, implementing reference count such as weak reference that would be difficult without it, and the allocation strategy is more flexible and easier to maintain. I use this approach frequently in a client-policy based design.
Re: array-based list question
Ok, nevermind. Sorry. It's a linked lsit that has to be used. I think fstream would be the best option. I don't know how to load the input into the linked list though. Would be nice if someone could show me how to do this.
Thank you
Re: array-based list question
Now what do you mean, how to put things into std::list or how to write your own implementation of singly/doubly linked list?