0% found this document useful (0 votes)
49 views

@chapter 4 DSA Part I

1) Structures allow users to define custom data types that group together different data types under a single name. They are declared using the struct keyword followed by the structure name and members. 2) Linked lists are linear data structures where each element points to the next. They can be singly, doubly, or circularly linked. Common linked list operations include insertion and deletion at various positions. 3) To insert a node at the beginning of a singly linked list, the next pointer of the new node is made to point to the first node, and the start pointer is made to point to the new node. To delete the first node, the start pointer is made to point to the second node and the first node

Uploaded by

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

@chapter 4 DSA Part I

1) Structures allow users to define custom data types that group together different data types under a single name. They are declared using the struct keyword followed by the structure name and members. 2) Linked lists are linear data structures where each element points to the next. They can be singly, doubly, or circularly linked. Common linked list operations include insertion and deletion at various positions. 3) To insert a node at the beginning of a singly linked list, the next pointer of the new node is made to point to the first node, and the start pointer is made to point to the new node. To delete the first node, the start pointer is made to point to the second node and the first node

Uploaded by

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

1

CHAPTER FOUR

Data Structures

Mulugeta G.
2

Structures
• Is a collection of data items with different data type.

• The data item of structure is called member of the structure.

Declaration of structure

• Structure is defined using the struct keyword.

struct name {
data type1 member 1;
data type2 member 2
.
.
data type n member n;
};
3

Structures
• Structures are aggregate data types built using elements of

primitive data types.


• Example:
struct Time{
int hour;
int minute;
int second;
};
• The struct keyword creates a new user defined data type that is

used to declare variables of an aggregate data type.


4

Structures
• Structure variables are declared like variables of other types.

• Syntax:

• struct<structure tag><variable name>;


E.g.
struct Time timeObject,
struct Time *timeptr;

struct Time{
int hour;
int minute;
int second;
};
5

Structures
• Accessing Members of Structure Variables

• Dot operator (.): to access data members of structure variables.

• Arrow operator (->): to access data members of pointer

variables pointing to the structure.


• E.g. Print member hour of timeObject and timeptr.

• cout<<timeObject.hour; or

• cout<<timeptr->hour;

• TIP: timeptr->hour is the same as (*timeptr).hour.

• The parentheses is required since (*) has lower precedence than (.).
6

Structures
• Accessing Members of Structure Variables

• E.g. struct student {


char name[20];
int age;
char Dept[20];
};
struct student stud;
struct student *studptr;

cout<<stud.name;
OR
cout<<studptr->name;
7

Structures
• Self-Referential Structures

• Structures can hold pointers to instances of themselves.

struct list{
char name[10];

int count;
struct list *next;
};
• However, structures cannot contain instances of themselves.
8

Linked List
• is a linear collection of data elements, called nodes, where the

linear order is given by means of pointers.


• Each node is divided into two parts:

• The first part contains the information of the element i.e.

INFO or DATA.
• The second part contains the link field, which contains the

address of the next node in the list(Pointer).


node
A
data pointer
9

Linked List

A B C 

Start
• Start (Head): Special pointer that points to the first node of a

linked list, so that we can keep track of the linked list.


• The pointer of the last node contains a special value, called the

null pointer, which is any invalid address. This null pointer


signals the end of list.
• The list with no nodes on it is called the empty list or null list.
10

Linked List
• Types of Linked Lists:

• Singly Linked List

• Navigation is forward only

• Doubly Linked Lists

• forward and backward navigation is possible

• Circular Linked List

• Last element is lined to the first element


11

Linked List
• Singly Linked List

• Each node has only one link part.

• Each link part contains the address of the next node in the

list.
• Link part of the last node contains NULL value (special

value) which signifies the end of the node.


12

Linked List
• Singly Linked List

• Each node contains a value (data) and a pointer to the next


node in the list.
• (Start) Head is the header pointer which points at the first
node in the list.
13

Linked List
• Basic Operations on Linked Lists

• Insertion

• At first (Beginning)

• At last (End)

• At a given location (At middle)

• Deletion

• First Node

• Last Node

• Node in given location or having given data item

• Find / Search a item in the list

• Displaying items in the list


14

Linked List

• Insertion at the beginning

There are two steps to be followed:-


a) Make the next pointer of the node point towards the first
node of the list.

b) Make the start pointer point towards this new node.


 If the list is empty simply make the start pointer point
towards the new node;
15

Linked List
• Insertion at the beginning
void insert_beg()
{
student *temp;
if(start==NULL) //if the list is empty
{
start=temp;
Start->next=Null;
cout<<”Node inserted at the beginning”;
}
else
{
temp=start;
start=p;
p->next=temp; //making new node point at
} //the first node of the list
}
16

Linked List
• Insertion at the End

• Here we simply need to make the next pointer of the last node

point to the new node.


• To do this we need to declare a second pointer, q, to step

through the list until it finds the last node


17

Linked List
• Insertion at the End
void insert_end(student *p)
{
student *q=start;
if(start==NULL)
{
start=p;
cout<<”Node inserted at the end…!\n”;
}
else
{// the loop terminate when q points to //the last node
while(q->next!=NULL)
q=q->next;

q->next=p; // the new node is made the last node


}
}
18

Linked List

• Insertion at any given location: 2 Steps


 Make the next pointer of the node to be inserted point to the
next node of the node after which you want to insert the node.
 Make the next pointer of the node after which the node is to
be inserted, point to the node to be inserted.
or
1. Create a Node
2. Set the node data Values
3. Break pointer connection
4. Re-connect the pointers
19

Linked List
• Insertion at any given location:
void insert_after(){
student *q;
q=start;
for(int i=1;i<c; i++)
q=q->next;
if(q==NULL)
cout<<”Less than “<<c<<” nodes…!”;
p->next=q->next;
q->next=p;
cout<<”Node inserted successfully”;
}
20

PART II
Deletion
21

Linked List

• Deletion the First Node

There are two steps to be followed:-


• Making the start pointer point towards the 2nd node.

• Deleting the first node using delete keyword.

start

one two three


22

Linked List
• Deletion the First Node

void del_first(){
if(start==NULL)
cout<<”\n Error……List is empty\n”;
else
{
student * temp=start;
start=temp->next;
delete temp;
cout<<”\n First node deleted!”;
}
}
23

Linked List

• Deletion the Last Node

There are two steps to be followed:-


 Making the second last node’s next pointer point to NULL

 Deleting the last node via delete keyword

start

node1 node2 node3


24

Linked List
• Deletion the Last Node
void del_last()
{
if(start==NULL)
cout<<”\n Error….List is empty”;
else
{
student *q=start;
while(q->next->next!=NULL)
q=q->next;
student *temp=q->next;
q->next=NULL;
delete temp;
cout<<”\n Deleted successfully…”;
}
}
25

Linked List

• Deletion the Node at Particular Location

 Here we make the next pointer of the node previous to the

node being deleted, point to the successor node of the node to


be deleted and then delete the node using delete keyword.

node1 node2 node3

To be deleted
26

Linked List
• Deletion the Node at Particular Location
void del(int c)
{
node *q=start;
for(int i=1;i<c; i++)
{
q=q->next;
if(q==NULL)
cout<<”\n Node not found\n”;
}
if(i==c)
{
node *p=q->next; //node to be deleted
q->next=p->next;//disconnecting the node p
delete p;
cout<<“Deleted Successfully”;
}
}
27

Linked List
• Displaying list of Nodes
• Having added one or more nodes, we need to display the list of
nodes on the screen using the following steps
Steps
1. Set a temporary pointer to point to the same thing as the start
pointer
2. If the pointer points to NULL, display the message “End of list”
and stop
3. Otherwise, display the details of the node pointed by the start
node
4. Make the temporary pointer point to the same thing as the next
pointer of the node it is currently indicating
5. Jump back to step 2
28

Linked List
• Displaying list of Nodes
void display()
{
student *q=start;
do
{ if(q==NULL)
cout<<“End of List\n”;
else
{ // Display details for what q points to
cout<<“Name:”<<q->name<<endl;
cout<<“Age:”<<q->age<<endl;
cout<<“Department:”<<q->Dept<<endl;
// Move to next node (if present)
q=q->next;
}
}while(q!=NULL);
}
29

Linked List
• Navigating/Traversing through the list

To Move Forward:

1. Set a pointer to point to the same thing as the start pointer.

2. If the pointer points to NULL, display the message “list is


empty" and stop.
3. Otherwise, move to the next node by making the pointer
point to the same thing as the next pointer of the node it is
currently indicating(using current pointer).
30

Linked List
• Navigating/Traversing through the list

To Move Backward:
1. Set a pointer to point to the same thing as the start pointer.
2. If the pointer points to NULL, display the message “list is
empty" and stop.
3. Set a new pointer(previous) and assign the same value as
start pointer and move forward until you find the node
before the one we are considering at the moment(using
current pointer).
4. Set current pointer equal to the new pointer (previous
pointer)
31

Linked List
• Doubly Linked List

• Each node points not only to Successor node (Next node), but

also to Predecessor node (Previous node).


• There are two NULL: at the first and last nodes

• Advantage: It is convenient to traverse linked lists Forwards

and Backwards.
 A B C 

Head (Start)
32

Linked List
• Doubly Linked List
• It is not necessary to have start pointer, we can have any
pointer(current) pointing to one of the node in the list

• Here, there is no pointer to the start of the list, there is simply a


pointer to some position in the list that can be moved left or right.
33

Linked List
• Doubly Linked List compared to SLL

Advantages: Disadvantages:
Can be traversed in either Requires more space

direction (may be essential List manipulations are


for some programs). slower (because more links
Some operations, such as must be changed).
deletion and inserting Greater chance of having

before a node, become bugs (because more links


easier. must be manipulated).
34

Linked List
• Doubly Linked List Structure
struct student
{
char name[20];
int age;
node *next;
node *previous;//holds the address of previous node

};
Previous .Data .Next
35

Linked List
• Basic Operations on Linked Lists

• Insertion

• At first (Beginning)

• At last (End)

• At a given location (At middle)

• Deletion

• First Node

• Last Node

• Node in given location or having given data item

• Find / Search a item in the list

• Displaying items in the list


36

Linked List

• Circular Linked List

• The last node points to the first node of the list.

A B C

Start
 How do we know when we have finished traversing the list?

 check if the pointer of the current node is equal to the Start

(head) pointer.
37

Application of Linked List

1. Applications that have an MRU (Most Recently Used) list (a


linked list of file names).
2. The cache in your browser that allows you to hit the BACK
button (a linked list of URLs).
3. Undo functionality in Photoshop or Word (a linked list of
state).
4. A stack, hash table, and binary tree can be implemented using
a doubly linked list.
38

NEXT
Queue

You might also like