Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Data structures in C and C++

  1. Jun 30, 2011 #1
    What is data structures in C and C++ all about?
    I heard that data structures are useful in constructing menu items of any software's menu bar. Is it true? Please tell me more examples of the applications of data structures in real life.
  2. jcsd
  3. Jun 30, 2011 #2


    User Avatar
    Homework Helper

    In the case of Windows, menus are normally defined in text resource files (.rc) which look similar to structures and converted to binary resource files (.res) or a visual resource tool is used that works with the binary resource files (.res) which eventually become part of a programs image file (.exe).

    Getting back to structures, a wiki link for a basic description of them:

  4. Jul 2, 2011 #3
    A data structure is just a way to store data, for example array, bin. tree, etc.
  5. Jul 3, 2011 #4
    To elaborate, a data structure stores information and generally has methods associated with it to search for, insert and delete data. For example, a linked list is a collection of nodes that look like this (in C syntax)

    Code (Text):
    typedef struct list {
        node *head;
        int count;
    } list;

    typedef struct node {
        void *data;
        node *next;
    } node;
    The first struct called "list" contains a pointer to the head of the list (the first node) and an integer variable to hold the number of nodes. The second struct is a node, which contains a pointer to the data (the void * could be any type of data) and another pointer to the next node (which could be NULL if there is only one). Each time you insert a node into the linked list, you point it to the next node. When you want to extract data from the list, you can use a loop and cycle through each node using the the "next" pointer until you find what you want. So you see, depending on your data structure you may need to write methods to perform these tasks (in a lot of cases, the language you use will include a library of data structures and methods that you can use).

    There are many other data structures, and some are better for certain things. Some terms you may run into (some have been mentioned) include arrays, stacks, hash tables, trees, there are many varieties of each.
  6. Jul 6, 2011 #5


    User Avatar
    Science Advisor

    A data structure is, as Max Plank and Adyssa said, just a way to store data in a way that reflects the relationship of the bits of data to one another. "byte", "integer", and "real" are elementary data types. More complicated are array, collections of different bits of data of the samer type, and the "structure" or "struct" in which the various pieces of data can be of different types. A structure can even have arrays or structs as elements.

    But hopefully, you will soon move on to "object oriented programming", the best thing since sliced bread! An "object", called a "class" in C++ or Java, includes both data types and functions that operate on that data. For example, if you were writing a program to handle personnel for a company, you might define an "object" data type that includes, as data, name, address, salary or hourly wage, hours worked and then functions that allow you to set or return each of those, calculate and print wage reports, etc., all encapsulated in the data type itself- so that it is easy to incorporate into many different programs.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook