Lab Week 6
Lab Week 6
Lab 6th
Objective:
Use of DevC++/C-free
Creating a Singly linked list
Traverse the list
Insert new node at beginning
Insert new node at the end of the list
Delete a node from beginning of the list
Delete node by Element
Delete a node from end of the list
Instructions:
Ensure that the system is in proper working condition (Especially DevC++).
Consultancy is allowed during practice session only.
Perform all the tasks yourself. Discussions are not allowed while working on
such tasks.
Submission of the task solutions is necessary whatsoever.
Complete your work within the time constraint.
1 of 1
Link Lists in C++
A linked list is a linear data structure, in which the elements are not stored at
contiguous memory locations. The elements in a linked list are linked using
pointers as shown in the below image:
In simple words, a linked list consists of nodes where each node contains a
data field and a reference (link) to the next node in the list.
struct Node{
int data;
Node* next;
};
2 of 1
Example:
#include<iostream>
#include<conio.h>
using namespace std;
struct Node
{
int data;
Node* next;
};
Node *first=NULL,*last=NULL;
void addItem(int d);
void display();
int main(){
addItem(2);
addItem(84);
addItem(6);
display();
getch();
return 0;
}
void addItem(int n){
Node* ptrNew = new Node;
ptrNew->data = n;
ptrNew->next = NULL;
if (first==NULL){
first = ptrNew;
last = ptrNew;
}
else{
last->next = ptrNew;
last = ptrNew;
}
}
void display()
{
Node* ptrCur = first;
while(ptrCur!=NULL){
cout<<ptrCur->data<<endl;
ptrCur = ptrCur->next;
}
}
3 of 1
Task
Write a program in C++ to implement Singly List using class and pointers.
#include<iostream>
using namespace std;
class node
{
int data;
node *next;
public:
void addItem();
void display();
};
node *first=NULL,*last=NULL;
}
}
void node::display()
{
node* ptrCur = first;
while(ptrCur!=NULL){
cout<<ptrCur->data<<endl;
ptrCur = ptrCur->next;
}
}
main()
{
node obj1;
obj1.addItem();
obj1.display();
}
4 of 1
To help you better understand the concept of a linked list and a node, some important
properties of linked lists are described through pictures
Suppose that current is a pointer of the same type as the pointer head. Then, the statement:
current = head;
copies the value of head into current
5 of 1
Moreover
6 of 1
Insert new node at beginning
Task
Write a C++ program to insert five new nodes at the beginning of the singly
linked List.
7 of 1
#include<iostream>
using namespace std;
void disp();
struct node
{ int data;
node *link;
};
node *p;
void insert_at_begining(int x)
{ node *q;
q=p;
p=new node;
p->data=x;
p->link=q;
cout<<" \nInserted successfully at the begining..";
disp();
}
void disp()
{ node *q;
q=p;
if(q==NULL)
{
cout<<" \nNo data is in the list..";
return;
}
cout<<" \nThe items present in the list are :";
while(q!=NULL)
{
cout<<" "<<q->data;
q=q->link;
}
}
main()
{
int element;
for(int n=0;n<=5;n++)
{
cout<<"\nenter element :"<<endl;
cin>> element;
insert_at_begining(element);
}
}
8 of 1
Insert new node at the End of the List
Task
Write a C++ program to insert five new nodes at the End of the singly linked
List.
struct node
{ int data;
node *link;
};
node *p=NULL;
void insert_at_last(int x)
{
node *q,*t;
if(p==NULL)
{ p=new node;
p->data=x;
p->link=NULL;
}
else
{ q=p;
while(q->link!=NULL)
{
q=q->link; }
t=new node;
t->data=x;
t->link=NULL;
q->link=t;
}
cout<<"\nInserted successfully at the end..";
disp();
}
9 of 1
Insert new node at a specified location in the list
Task
Write a C++ program to insert a new node in your linked list at any given location.
Task
Write a C++ program to delete a node from the beginning of the list.
10 of 1
Function to perform deleting node at beginning
void delbeg()
{
cout<<" \nThe list before deletion:";
disp();
node *q;
q=p;
if(q==NULL)
{
cout<<" \nNo data is present..";
return;
}
p=q->link;
delete q;
cout<<"\n The list After Deletion:";
disp();
return;
}
Element to delete: 99
Task
Write a C++ program to delete the required element node from the list.
11 of 1
Function to perform Delete node by taking required element from user.
void delelement(int x)
{
node *q,*r;
q=p;
if(q->data==x)
{
p=q->link;
delete q;
return;
}
r=q;
while(q!=NULL)
{
if(q->data==x)
{
r->link=q->link;
delete q;
disp();
return;
}
r=q;
q=q->link;
}
cout<<" \nElement u entered "<<x<<" is not found..";
}
12 of 1