Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

LECTURE 5

Dynamic Memory Management


 Operators new and delete.
 Dynamic Allocation p ?
 int *p = new int;
 int *q = new int (5);
 int *arrayptr = new int [100]; arrayptr
 Memory De-allocation
 delete p;
 delete [ ] arrayptr ;

2
#include <iostream>
using namespace std;
////////////////////////////////////// int main()
class Distance //Distance class {
{ Distance dist; //define a named Distance object
private:
int feet; dist.getdist(); //access object members
float inches;
public: dist.showdist(); // with dot operator
void getdist() //get length from user
{
cout << “\nEnter feet: ”; Distance* distptr; //pointer to Distance
cin >> feet;
cout << “Enter inches: ”; distptr = new Distance; //points to new
Distance object
cin >> inches;
}
distptr->getdist(); //access object members
void showdist() //display distance
distptr->showdist(); // with -> operator cout
{ << endl;
cout << feet << “\’-” << inches << ‘\”’; return 0;
} }
};
LINKED LIST DATA STRUCTURE
 Overcomes the limitations of array based organization

 A linked list is a dynamic data structure that grows and


shrinks without any limitation on size (except memory
space, of course)
(LINEAR) LINKED LIST
info next info next info next

first NULL

node node node


LINKED LIST IMPLEMENTATION
// representing a node
struct node
{
int info;
node * next;
};
LINKED LIST IMPLEMENTATION
class linklist //a list of links
{

private:
node* first; //pointer to first node
bool empty(); //check empty

public:
linklist() //no-argument constructor
{ first = NULL; } //no first link

void traverse();
void InsertAtStart(int val);
void RemoveFromStart();
void InsertAtEnd (int val);
void RemoveFromEnd();
};
LINKED LIST IMPLEMENTATION
//check empty
bool linklist::empty()
{
if (first==NULL)
return true;
else
return false;
}
Void linklist::traverse()
{
if (empty())
return;
node * p=first;
while (p!=NULL){
cout<<p->info;
p=p->next; } //end while
}
Void linklist::InsertAtStart(int val)
{
node * n=new node;
n->info=val;
n->next=first;
first=n;
}
LINKED LIST IMPLEMENTATION
//remove a node from start of linked list
void linklist::RemoveFromStart()
{
if (empty()) {
cout<<“nothing to remove”;
exit(1);
} //end if
node * p = first;
first=first->next;
delete p;
}
LINKED LIST IMPLEMENTATION
//insert a node at end of linked list
void linklist::InsertAtEnd (int val)
{
if (empty())
InsertAtStart(val);
else {
node * p=first;
while (p->next !=NULL)
p=p->next;
node * n=new node;
n->info=val;
n->next=NULL;
p->next=n; } //end else
}
LINKED LIST IMPLEMENTATION
void linklist::RemoveFromEnd() //remove a node from end of list
{
if (empty()) return;
node * Last;
if (first->next==NULL) //list has only one node
{ Last=first;
first=NULL;
delete Last;
}
else //list has more than one nodes
{
node * SecondLast;
SecondLast=first;
while (SecondLast->next->next != NULL)
SecondLast= SecondLast->next;
Last=SecondLast->next;
delete Last;
SecondLast->next=NULL; } //end else
}
LINKED LIST IMPLEMENTATION
void main()
{
linklist l;
l.initialize();
if (l.empty()==true)
cout<< “list is empty”;
l.InsertAtStart(10);
l.InsertAtStart(20);
l.InsertAtStart(30);
l. traverse();
l.RemoveFromStart();
l.traverse();
}
Linked List Variations
 Circular linked list
 Doubly linked list

You might also like