What is the purpose of data structures in C and C++?

In summary, data structures are useful in constructing menu items of any software's menu bar. They can also be used to store other complex data types in a way that is easy to access and use.
  • #1
pairofstrings
411
7
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.
Thanks.
 
Technology news on Phys.org
  • #2
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:

http://en.wikipedia.org/wiki/Struct_(C_programming_language)
 
  • #3
A data structure is just a way to store data, for example array, bin. tree, etc.
 
  • #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:
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.
 
  • #5
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.
 

1. What is a data structure in C and C++?

A data structure in C and C++ is a way of organizing and storing data in a computer's memory. It provides a means of efficiently accessing and manipulating data, allowing for faster and more effective programming.

2. What are some common types of data structures in C and C++?

Some common types of data structures in C and C++ include arrays, linked lists, stacks, queues, trees, and graphs. These data structures can be used to store different types of data and have different methods of accessing and manipulating that data.

3. What is the difference between an array and a linked list?

An array is a linear data structure where elements are stored in contiguous memory locations, while a linked list is a non-linear data structure where elements are linked together via pointers. This means that arrays have a fixed size and elements can be accessed directly, while linked lists can have a variable size and elements must be accessed sequentially.

4. How do data structures in C and C++ affect program performance?

The choice of data structure can greatly impact program performance. For example, using a linked list may be more efficient for inserting and deleting elements, while an array may be better for accessing elements. It is important to choose the right data structure for the task at hand in order to optimize program performance.

5. Are there built-in data structures in C and C++?

Yes, C and C++ both have built-in data structures such as arrays and structures. However, there are also libraries that provide more complex data structures such as linked lists, stacks, and queues. It is also possible to create custom data structures using the built-in data types and functions in C and C++.

Similar threads

  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
4
Replies
107
Views
5K
  • Programming and Computer Science
Replies
11
Views
992
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
7
Views
681
  • Programming and Computer Science
Replies
8
Views
953
  • Programming and Computer Science
Replies
1
Views
925
  • STEM Academic Advising
Replies
3
Views
914
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
35
Views
3K
Back
Top