Sei sulla pagina 1di 20

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER


ENGINEERING
LAB TITLE: Introduction To Data Structure

Student Name: M. Hamza Amin Reg.No: 191862

Objective: To learn about types of data structure and mainly the concept of single
linked list.

LAB ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)
Ability to Conduct
Experiment
Ability to assimilate the
results
Effective use of lab
equipment and follows
the lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:


Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:

Date: Signature:
LAB# 01

DATA STRUCTURE

STUDENT NAME: M. HAMZA AMIN


ID NO: 191862
SUBMITTED TO: SIR UZAIR
TOPIC:
Introduction to data structure
Linked list:
A liked list is a linear data structure where each element is a
separate object. Each element (we will call it node) of a list is
comprising of two items - the data and a reference to the next
node. The last node has reference to null.

Lab Task # 1:
Make a function with name add link. Whenever you call this
function in the main, it will create a new node containing data.
SOURCE CODE:
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class list
{
private:
node *head;
public:
//constructor to intialize
list()
{
head = NULL;
}
// make function add fuction to call this function in main
void add_link(int d)
{
node *temp,*ptr;
if(head==NULL)
{
head = new node;
head->data=d;
head->next=NULL;
}
else
{
ptr=head;
while(ptr->next!=NULL)ptr=ptr->next;
temp = new node;
temp->data=d;
temp->next=NULL;
ptr->next=temp;
}
}
//function to display value
void show()
{
node *temp;
temp = head;
cout<<"The nodes containing data is as follow:"<<endl;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
};
//main function
int main()
{
list l;
l.add_link(10);
l.add_link(20);
l.add_link(30);
l.add_link(12);
l.show();
cout<<endl;
}
Screen shot of program
OUTPUT:
TASK # 2:
Make a function which deletes node from your linked list.
SOURCE CODE:
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class list
{
private:
node *head;
public:
list()
{
head = NULL;
}
void add(int d)
{
node *temp,*ptr;
if(head==NULL)
{
head = new node;
head->data=d;
head->next=NULL;
}
else
{
ptr=head;
while(ptr->next!=NULL)ptr=ptr->next;
temp = new node;
temp->data=d;
temp->next=NULL;
ptr->next=temp;
}
}
void del(int v)
{
node *temp, *pre;
temp = head;
if(temp->data==v)
{
head = temp->next;
delete temp;
cout<<endl<<v<<"has been deleted."<<endl;
return;

}
pre = temp;
while(temp!=NULL)
{
if(temp->data==v)
{
pre->next=temp->next;
delete temp;
cout<<"/nValue deleted."<<endl;
return;
}
pre = temp;
temp = temp->next;
}
cout<<endl<<v

}
void display()
{
node *temp;
temp = head;
cout<<"The list is as follow:"<<endl;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}

}
};
int main()
{
list l;
l.add(12);
l.add(13);
l.add(14);
l.add(15);
l.display();
cout<<endl;
l.del(12);
l.display();
}
SCREEN SHOTS OF PROGRAM:
OUTPUT:

TASK #3
Write a function to display all the data of your linked list
sequentially.
SOURCE CODE:
#include<iostream>
using namespace std;
//node decleration
struct node
{
int data;
node *next;
};
//class decleration
class list
{
private:
node *head;
public:
//constructor
list()
{
head = NULL;
}
//memeber function to call nodes in main
void creat_node(int d)
{
node *temp,*ptr;
if(head==NULL)
{
head = new node;
head->data=d;
head->next=NULL;
}
else
{
ptr=head;
while(ptr->next!=NULL)ptr=ptr->next;
temp = new node;
temp->data=d;
temp->next=NULL;
ptr->next=temp;
}
}
//memeber function to display all nodes
void display()
{
node *temp;
temp = head;
cout<<"The nodes containing data is as follow:"<<endl;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
};
// main :constrain menu
int main()
{
list l;
l.creat_node(1);
l.creat_node(2);
l.creat_node(3);
l.creat_node(4);
l.display();
}
SCREENSHOTS OF PROGRAM:
OUTPUT:

CONCLUSION:
In this lab we introduce data structure and learn one of the type
of data structure liked list. There we see advantage of linked list
over array. Then we implement the single linked list and
perform some tasks. So we learnt type of liked list which is
single linked list.

Potrebbero piacerti anche