Why Data Structures???...QUESTIONS INSIDE

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Why Data Structures???...QUESTIONS INSIDE

 
0
  #1
Apr 9th, 2005
I HAVE SPECIFIC QUESTIONS!!!

1. Why do you need Self-Referential Classes? Please provide a "real" life example of when it would be used.



  1. class Node {
  2. public:
  3. Node(int);
  4. void setData(int);
  5. int getData() const;
  6. void setNextPtr( const Node *);
  7. const Node *getNextPtr() const;
  8. private:
  9. int data;
  10. Node *nextPtr;
  11. };

2. ^^^What is a Node, is it just a random name for this Class?

3. what does the (const Node *) mean in:
  1. void setNextPtr(const Node *);

4. why is nextPtr the, "Link?"



I understand Dynamic Memory Allocation, somewhat, but

5. Can somebody break down these two lines of code form me?:

  1. Node *newPtr = new Node(10);
  2.  
  3. delete newPtr;

DEFINITION

A Linked List is a linear collection of self-referential class objects, called nodes, connected by pointer links.

6. What is that definition REALLY saying (plain english)?

According to my text book, "...if Nodes contain base-class pointers or base-class references to base-class and derived-class objects related by inheritance, we can have a linked list of such nodes and use Virtual Function calls to process these objects polymorphically...."

7. Can you please explain or provide an example of what is really being said here ^^^?


I think I understand how Stacks and Queues work, so I'll leave those alone until I think otherwise, but

8. How does a Tree work and what is it's purpose?

Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 63
Reputation: marinme is an unknown quantity at this point 
Solved Threads: 1
marinme marinme is offline Offline
Junior Poster in Training

Re: Why Data Structures???...QUESTIONS INSIDE

 
0
  #2
Apr 9th, 2005
Originally Posted by Fasola
1. Why do you need Self-Referential Classes? Please provide a "real" life example of when it would be used.
Well... self-referential class - a class that refers to itself. This is shown in the Node * nextPtr data member. the main reason you would use this is to actually link to the next Node in the linked list/tree/other data structure. A "real" life example you would use this in? Other than use in data structures I can't link of a use for it.

Originally Posted by Fasola
2. ^^^What is a Node, is it just a random name for this Class?
A Node an object which stores your data/other objects.. works kind of like a cable connection. Think of them as local areas, like neighborhoods.

Originally Posted by Fasola
3. what does the (const Node *) mean in:
  1. void setNextPtr(const Node *);
Well, what does the const qualifier do? Makes data unchangeable? you should be able to figure out the rest by the name of the function.
Originally Posted by Fasola
4. why is nextPtr the, "Link?"
it points to the next Node object.

Originally Posted by Fasola
5. what does can somebody break down these two lines of code form me?:

  1. Node *newPtr = new Node(10);
  2.  
  3. delete newPtr;
The first line creates another node that you can add to the list. The second deletes that node from memory.

Originally Posted by fasola
DEFINITION

A Linked List is a linear collection of self-referential class objects, called nodes, connected by pointer links.

6. What is that definition REALLY saying (plain english)?
Let's look at this logically... pull out key phrases..
linear collection - collection in a linear(a line) order
I defined self-referential class objects above.
pointer links - this is where the Node * nextPtr comes into play, as I said above, points to the next node.

Originally Posted by Fasola
According to my text book, "...if Nodes contain base-class pointers or base-class references to base-class and derived-class objects related by inheritance, we can have a linked list of such nodes and use Virtual Function calls to process these objects polymorphically...."


7. Can you please explain or provide an example of what is really being said here ^^^?
This is kind of a difficult subject to explain... do you know anything about inheritance/polymorphism? your answer lies in research of the previously listed topics.

Originally Posted by Fasola
8. How does a Tree work and what is it's purpose?

what kind of tree do you mean? there are different tree data structures..

And if this post didn't help, here are some links regarding linked lists for you to do some further research:

http://www.inversereality.org/tutori...nkedlists.html
http://www.fredosaurus.com/notes-cpp/index.html go to structs near the bottom.

That should be enough to get ya started
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why Data Structures???...QUESTIONS INSIDE

 
0
  #3
Apr 9th, 2005
>1. Why do you need Self-Referential Classes?
It's the easiest way to implement non-contiguous data structures. You don't need them, but they make life easier.

>2. ^^^What is a Node, is it just a random name for this Class?
Node

>3. what does the (const Node *) mean in
It's a pointer to a Node that shouldn't be modified.

>4. why is nextPtr the, "Link?"
That's basic data structure terminology. A "link" leads from one node to another.

>Node *newPtr = new Node(10);
It creates a Node, calls the constructor with the value 10, and assigns the address of it to newPtr.

>delete newPtr;
It releases the memory pointed to by newPtr. This only works predictably for memory returned by new.

>6. What is that definition REALLY saying (plain english)?
That's an awful explanation of a linked list. Sadly, it's not that simple when first introducing people to the concept. This provides more suitable information.

>7. Can you please explain or provide an example of what is really being said here ^^^?
Basically, you can have a linked list of polymorphic types.

>8. How does a Tree work and what is it's purpose?
Go here and read up on the tutorials that talk about binary trees. When someone talks about a tree, they usually mean a binary search tree.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Why Data Structures???...QUESTIONS INSIDE

 
0
  #4
Apr 11th, 2005
First of all Thank you both Marinme and Narue...that's real helpful

Na, That link you gave me to wikipedia was exactly what I needed. I've been going from one definition to another for the last hour and a half. I have a few interesting questions, but have run out of (session) time in this public library computer, will be back
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Why Data Structures???...QUESTIONS INSIDE

 
0
  #5
Apr 11th, 2005
Lets see if i've learnt anything and if I am getting this:

Okay so a Linked List is just a list of Nodes

Nodes are basicly just Structs/Classes

the Members of these Nodes contain data (such as Values of Objects) and Pointers to the previous or next Node on a Linked List

a Linked List's primary use is to access data (i.e. Nodes or Members of Nodes) that are not contiguously (i.e. adjacent) to eachother. So, it's safe to say a Linked List is primarily used to increase effiecency (i.e. speed up) the process of accessing data at runtime. It's kind of like an "Index" in general or in a database.

NOTE: I am not sure how to utilize this in real world programming!

damn, another session time has ended, I'll be back
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Why Data Structures???...QUESTIONS INSIDE

 
0
  #6
Apr 11th, 2005
I was reading up on Trees, can I get an example of a Binary Tree (top down) and a B-Tree (ground up)?

More importantly, what the two types of Trees are good for, in real word programming?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why Data Structures???...QUESTIONS INSIDE

 
0
  #7
Apr 11th, 2005
>can I get an example of a Binary Tree (top down)
The link I gave you covers these in detail.

>and a B-Tree (ground up)?
These are complicated enough that example code will be hard to find, but you shouldn't have any trouble finding B-tree info on google. I won't give you code or concepts because the search will be good for you.

>what the two types of Trees are good for, in real word programming?
Binary trees (usually of the binary search tree variation) have a wide range of uses. A good simple example is a large phonebook, where the records must be sorted, but still quickly accessible. B-trees are primarily used as external storage structures for databases.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Why Data Structures???...QUESTIONS INSIDE

 
0
  #8
Apr 12th, 2005
Originally Posted by Narue
>can I get an example of a Binary Tree (top down)
The link I gave you covers these in detail.

>and a B-Tree (ground up)?
These are complicated enough that example code will be hard to find, but you shouldn't have any trouble finding B-tree info on google. I won't give you code or concepts because the search will be good for you.

>what the two types of Trees are good for, in real word programming?
Binary trees (usually of the binary search tree variation) have a wide range of uses. A good simple example is a large phonebook, where the records must be sorted, but still quickly accessible. B-trees are primarily used as external storage structures for databases.
gotcha!

was i right about this (below):


http://www.daniweb.com/techtalkforum...22&postcount=5
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why Data Structures???...QUESTIONS INSIDE

 
0
  #9
Apr 12th, 2005
>was i right about this (below):
More or less. Linked lists can be more efficient than arrays, but the inverse can also be true. It depends on what you need. You use an array when you need fast random access, you know roughly how many items ther will be, and items will only be added to the end of the array. This is because arrays can access any item with an index very quickly. With linked lists, you would be forced to follow links until you found the right item. However, insertion into an array is potentially expensive because you need to shift every item that goes after the new item to make room. Resizing an array (if possible) is tedious and error prone, so it's best avoided when possible.

You use a linked list when you don't know how many items there will be, you may insert an item anywhere in the list, and random access is not a primary goal. Because linked lists are non-contiguous, insertion is a trivial matter of simply resetting a few links, so the operation is very efficient no matter where you insert. Linked lists are inherently dynamic, so you don't have to bend over backward to get them to resize themselves.

>I am not sure how to utilize this in real world programming!
File processing. You need to read all of the lines in a file and then sort them. You could use an array, but then you would have to guess at how many lines are in the file, or try to implement a resizable array. Using a linked list is easier and cleaner. Now you have more comfortable options. For example, you can use an unsorted linked list, count the number of lines, then create an array of pointers and sort that, or you could maintain a sorted linked list from the start.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: Why Data Structures???...QUESTIONS INSIDE

 
0
  #10
Apr 12th, 2005
Hello,

Excellent discussion so far. Wondering if you would expand it a bit more, and discuss sorting a linked list. Perhaps also go into double-linked lists (when I write them, I write these too, so I can easily go backwards). You guys are on such a good role..

Christian
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC