Вы находитесь на странице: 1из 8

Data structure and algorithms EED BUETK

EXPERIMENT # 07
Objective #1: Implementation of linked list algorithm

LINKED LIST:

Linked list is one of the most important data structures. We often face situations,
where the data is dynamic in nature and number of data can’t be predicted or the
number of data keeps changing during program execution. Linked lists are very useful
in this type of situations.

The implementation of a linked list in C++ is done using pointers. You can go through
the pointers chapter if you don’t have a strong grip over it. You can also practice a
good number of questions from practice section.

A linked list is made up of many nodes which are connected in nature. Every node is
mainly divided into two parts, one part holds the data and the other part is connected
to a different node. It is similar to the picture given below.

Input:
#include <iostream>

using namespace std;

struct node
{
int data;
node *next;
};

class linked_list
{
private:
node *head,*tail;
public:
linked_list()

SHERYAR 16EL47
Data structure and algorithms EED BUETK

{
head = NULL;
tail = NULL;
}

void add_node(int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;

if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}

node* gethead()
{
return head;
}

void display(node *head)


{
if(head == NULL)
{
cout << "NULL" << endl;
}
else
{
cout << head->data << endl;
display(head->next);
}
}
};

int main()
{
linked_list a;
a.add_node(1);
a.add_node(2);
a.display(a.gethead());
return 0;

SHERYAR 16EL47
Data structure and algorithms EED BUETK

OUTPUT

Objective#2: Implementation of insertion into a Linked list.

Input

#include <iostream>

using namespace std;

struct node
{
int data;
node *next;
};

class linked_list
{
private:
node *head,*tail;
public:
linked_list()
{
head = NULL;
tail = NULL;

SHERYAR 16EL47
Data structure and algorithms EED BUETK

void add_node(int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;

if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}

node* gethead()
{
return head;
}

static void display(node *head)


{
if(head == NULL)
{
cout << "NULL" << endl;
}
else
{
cout << head->data << endl;
display(head->next);
}
}

static void concatenate(node *a,node *b)


{
if( a != NULL && b!= NULL )
{
if (a->next == NULL)
a->next = b;
else
concatenate(a->next,b);
}
else
{
cout << "Either a or b is NULL\n";

SHERYAR 16EL47
Data structure and algorithms EED BUETK

}
}

void front(int n)
{
node *tmp = new node;
tmp -> data = n;
tmp -> next = head;
head = tmp;
}

void after(node *a, int value)


{
node* p = new node;
p->data = value;
p->next = a->next;
a->next = p;
}
};

int main()
{
linked_list a;
a.add_node(1);
a.add_node(2);
a.front(3);
linked_list::display(a.gethead());
return 0;
}

OUTPUT

SHERYAR 16EL47
Data structure and algorithms EED BUETK

Objective #3: Implementation of deletion from a Linked list.

Input
#include <iostream>

using namespace std;

struct node
{
int data;
node *next;
};

class linked_list
{
private:
node *head,*tail;
public:
linked_list()
{
head = NULL;
tail = NULL;
}

void add_node(int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;

if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}

node* gethead()
{
return head;
}

static void display(node *head)

SHERYAR 16EL47
Data structure and algorithms EED BUETK

{
if(head == NULL)
{
cout << "NULL" << endl;
}
else
{
cout << head->data << endl;
display(head->next);
}
}

static void concatenate(node *a,node *b)


{
if( a != NULL && b!= NULL )
{
if (a->next == NULL)
a->next = b;
else
concatenate(a->next,b);
}
else
{
cout << "Either a or b is NULL\n";
}
}

void front(int n)
{
node *tmp = new node;
tmp -> data = n;
tmp -> next = head;
head = tmp;
}

void after(node *a, int value)


{
node* p = new node;
p->data = value;
p->next = a->next;
a->next = p;
}

void del (node *before_del)


{
node* temp;
temp = before_del->next;
before_del->next = temp->next;
delete temp;
}

SHERYAR 16EL47
Data structure and algorithms EED BUETK

};

int main()
{
linked_list a;
a.add_node(1);
a.add_node(2);
a.front(3);
a.add_node(5);
a.add_node(15);
a.after(a.gethead()->next->next->next, 10);
a.del(a.gethead()->next);
linked_list::display(a.gethead());
return 0;
}

OUTPUT

SHERYAR 16EL47

Вам также может понравиться