@chapter 4 DSA Part I
@chapter 4 DSA Part I
CHAPTER FOUR
Data Structures
Mulugeta G.
2
Structures
• Is a collection of data items with different data type.
Declaration of structure
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
Structures
• Structure variables are declared like variables of other types.
• Syntax:
struct Time{
int hour;
int minute;
int second;
};
5
Structures
• Accessing Members of Structure Variables
• cout<<timeObject.hour; or
• cout<<timeptr->hour;
• The parentheses is required since (*) has lower precedence than (.).
6
Structures
• Accessing Members of Structure Variables
cout<<stud.name;
OR
cout<<studptr->name;
7
Structures
• Self-Referential Structures
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
INFO or DATA.
• The second part contains the link field, which contains the
Linked List
A B C
Start
• Start (Head): Special pointer that points to the first node of a
Linked List
• Types of Linked Lists:
Linked List
• Singly Linked List
• Each link part contains the address of the next node in the
list.
• Link part of the last node contains NULL value (special
Linked List
• Singly Linked List
Linked List
• Basic Operations on Linked Lists
• Insertion
• At first (Beginning)
• At last (End)
• Deletion
• First Node
• Last Node
Linked List
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
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;
Linked List
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
start
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
start
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
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:
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
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
Linked List
• Doubly Linked List compared to SLL
Advantages: Disadvantages:
Can be traversed in either Requires more space
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)
• Deletion
• First Node
• Last Node
Linked List
A B C
Start
How do we know when we have finished traversing the list?
(head) pointer.
37
NEXT
Queue