0% found this document useful (0 votes)
5 views27 pages

Lec 4

The document provides an overview of pointers and data structures in programming, explaining how pointers hold memory addresses and facilitate data manipulation. It categorizes data structures into primitive and non-primitive types, further dividing them into linear and non-linear structures, with examples like arrays, linked lists, stacks, and queues. Additionally, it outlines common operations that can be performed on data structures, such as searching, inserting, deleting, sorting, updating, traversing, and merging.

Uploaded by

ramiwael476
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

Lec 4

The document provides an overview of pointers and data structures in programming, explaining how pointers hold memory addresses and facilitate data manipulation. It categorizes data structures into primitive and non-primitive types, further dividing them into linear and non-linear structures, with examples like arrays, linked lists, stacks, and queues. Additionally, it outlines common operations that can be performed on data structures, such as searching, inserting, deleting, sorting, updating, traversing, and merging.

Uploaded by

ramiwael476
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Data Structure

Lecture (4)
Pointers
Pointers
• A pointer is a variable that contains the memory location of another variable.
• Pointers are used to pass information back and forth between functions.
• Pointers enable the programmers to return multiple data items from a function
via function arguments.
• Pointers are used in data structures such as arrays.
• The general syntax of declaring pointer variables is:
Pointers
Pointers
• data_type is the data type of the value that the pointer will point to.

• & is called the address operator, and * is called the dereference operator.
Example
Example

sum = 5
mul = 10
*ptr2 = 4
div = -21
• The expression *ptr++ is equivalent to
*(ptr++) and will increase the value of ptr so
that it now points to the next memory
location.
• To increment the value of the variable whose

Note
address is stored in ptr, write (*ptr)++
Example

1 3

2
Example

1 3

2 4
Example

• Write a program to add two


integers using pointers and
functions ?
Example
• Write a program to swap the
value of two integers using
pointers and functions.
Data
Structures
Data Structures
• A data structure is basically a group of data elements that are put together
under one name, and which defines a particular way of storing and organizing
data in a computer so that it can be used efficiently.
• For any given problem, the application of an appropriate data structure
provides the most efficient solution.
• A solution is said to be efficient if it solves the problem within the required
resource constraints like the total space available to store the data and the time
allowed to perform each subtask.
Data Types

Primitive data types Non-Primitive Data Structures

Linear data structures Non-linear data structures

Static Dynamic Trees Graphs

Array Linked lists Stacks Queues


Data Structure
• Primitive data types:
➢ Primitive data types are certain data types already defined in the system,
such as int, float, double, and char.
➢ Primitive data types can hold one value.
• Non-Primitive data structures:
➢ Non-Primitive data structures are user-defined, such as arrays, linked lists,
stacks, queues, trees, and graphs.
➢ Non-Primitive data structures are divided to linear and non-linear data
structures.
Data Structure
• Linear data structures:
➢ The elements of a data structure are arranged in a linear or sequential order.
➢ Linear data structures can have a linear relationship between elements
either by means of sequential memory locations (arrays), or by means of
links (linked lists).
➢ In these data structures, one element is connected to only one another
element in a linear form.
➢ Divided to static and dynamic data structures.
Data Structure
• Static data structures:
➢ The size of a data structure is allocated at the compile time.
➢ Therefore, the maximum size is fixed.
➢ Example: arrays.
• Dynamic data structures:
➢ The size of a data structure is allocated at the run time.
➢ Therefore, the maximum size is flexible.
➢ Examples: linked lists, stacks, queues
Data Structure
• Non-linear data structures:
➢ The elements of a data structure are not arranged in a sequential order.
➢ Non-linear data structures are used for representing data that contains a
hierarchical relationship among various elements.
➢ In these data structures, one element is connected to many other elements.
➢ Examples: trees and graphs.
Array Data Structure
• An array is a collection of similar data elements.
• These data elements have the same data type.
• The elements of the array are stored in consecutive memory locations.
• Arrays are of fixed size.
Linked List Data Structure
• A linked list is a very flexible, dynamic data structure in which elements
(called nodes) form a sequential list.
• Elements can be inserted or deleted easily.
• Every node contains the value of the node and a pointer (address) to the next
node in the list.
Stack Data Structure
• A stack is a linear data structure in which insertion and deletion of elements
are done at only one end, which is known as the top of the stack.
• Stack is called a last-in, first-out (LIFO) structure because the last element
which is added to the stack is the first element which is deleted from the stack.
Queue Data Structure
• A queue is a first-in, first-out (FIFO) data structure in which the element that is
inserted first is the first one to be taken out.
• The elements in a queue are added at one end called the rear and removed from
the other end called the front.
Tree Data Structure
• A tree is a non-linear data structure which consists of a collection of nodes
arranged in a hierarchical order.
• One of the nodes is designated as the root node.
Graph Data Structure
• A graph is a non-linear data structure which is a collection of vertices (nodes)
and edges that connect these vertices.
• A graph is often viewed as a generalization of the tree structure.
• Graphs do not have any root node. Rather, every node in the graph can be
connected with every another node in the graph.
Operations On Data Structures
• The common operations that can be performed on the data structures are:
➢ Searching: Find the locations of data items that satisfy the given constraint.
➢ Inserting: Add new data items to the given data structure.
➢ Deleting: Remove a data item from the given data structure.
➢ Sorting: Arrange data items in ascending or descending order.
➢ Updating: Replace the data item with another data item.
➢ Traversing: Access each data item exactly once so that it can be processed.
➢ Merging: Combine two data structures to single data structure.

You might also like