Link List Programming Linked List in Cpp

8 views 10 slides Feb 12, 2025
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

Link List Programming Linked List in Cpp


Slide Content

Programming Linked List
class LinkedList{
// declaration of the
node struct node{
int info;
node *next;
};
//private variable
declared node *start;
public:
LinkedList()//
Constructor
{
start = NULL;
}
//public fucntion
declared void
AddAtBeg(int);
void AddAfter(int,
int); void Delete();
void Count();
void
Search(int);
void Display();
};
20

Programming Linked List
// following function will add new element at the
beginning
// also used to create first node
void LinkedList::AddAtBeg(int
data)
{
node * newNode;
newNode = new
node; newNode-
>info = data;
newNode->next =
start; start =
newNode;
}/*End of addatbeg()*/
21

Programming Linked List
//This function will add new element at any
position void LinkedList::AddAfter(int data,
int pos)
{ node *newNode, *q;
q = start;
if(q == NULL) {
cout<<"\n\nEmpty linked list" <<
endl;
return;
}
//Finding the position in the linked list to
insert for(int i = 1; i < pos; i++)
q = q->next;
newNode = new node;
newNode->info = data;
newNode->next = q-
>next; q->next =
newNode;
}
22

Programming Linked List
void LinkedList::Delete()
{
node *tmp,
*ptr; int data;
if(start ==
NULL) {
cout<<"\n\n List is
empty"<<endl; return;
}
cout<<"\n\nEnter the element for
deletion : "; cin>>data;// delete first
node
if(start->info ==
data){ tmp =
start;
start = start-
>next;
delete(tmp);
return;
}
23

ptr = start;
while(ptr->info != data) {
temp = ptr;
ptr = ptr ->next;
}
tmp->next = ptr->next;
delete(ptr); // Delete node in between, or
last node
}/*End of del()*/
24

Programming Linked List
void LinkedList::Display()
{
// Fill in the code that displays the contents of the
linked list
} /*End of display() */
void
LinkedList::Count()
{
// Fill in the code
that counts the
nodes of the
linked list
} /*End of count() */
void LinkedList::Search(int data)
{
// Fill in the code that will find the position of a
node that holds the ‘data’
} 25

Programming Linked List
int main()
{
int choice, n, m,
position, i; LinkedList
po;
while(1)
{
cout<<"1. Add at
beginning\n"; cout<<"2. Add
after \n"; cout<<"3. Delete\
n";
cout<<"4. Display\
n"; cout<<"5.
Count\n";
26

Programming Linked List
cout<<"6. Search\
n" cout<<"7. Quit\
n";
cout<<"\n Enter your
choice:"; cin>> choice;
switch(choice) {
case 1:
cout<<"\n\nEnter the
element:"; cin>>m;
po.AddAtBeg(m);
break;
27

Programming Linked List
case 2:
cout<<"\n\nEnter the
element:"; cin>>m;
cout<<"\nPosition after inserted
element:"; cin>>position;
po.AddAfter(m,position);
break;
case 3:
po.Delete(
); break;
case 4:
po.Display(
); break;
case 5:
po.Count();
break;
case 6: cout<<"\
n\nEnter the
element to
search:";
28

Programming Linked List
cin>>m;
po.Search(
m); break;
case 7:
exit(0)
; default:
cout<
<"\n\
nWro
ng
choice
";
}/*End of switch */
}/*End of while */
}
29
Tags