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

Linked list as ADT

Linked list is a chain in which nodes are connected with each other and each node has two fields
first field refers to data type and the second filed is address field which contain the address of
next node and data field holds elements
It is a simple and flexible data structure which can be used to easily manage our data in a
organize way
A link list can perform these operations.
Add(element)
Remove(element)
Search(element)
Types
Link list has two types singly and doubly link list
Singly linked list
In single linked list there is only one pointer which points to its successor
Doubly linked list
In this list there are two pointers one is pointing to its successor and the second is pointing to its
predecessor.
Uses and importance of linked list
When we are organizing data in a sequence we have to organize it in a way so that we can edit or
remove it easily we can use link list it help to organize data in a form so that we can perform the
above functions without having any issue or difficulty. It is easy to manage and modify, we can
delete, transverse, add any data at any point in that list
Problems in Linked lists
There is a main issue in the singly linked list is that when we jumps to the next node we can’t
access the previous node
CODE FOR LINKED LIST ADT
#include<iostream>
#include<windows.h>
using namespace std;

struct list {
int data;
struct list *next;
} *node = NULL, *first = NULL, *last = NULL, *node1 = NULL;

class linkedlist {
public:
/**--------------------function for creating linked list node-----------------*/

void insert()

{
list *addNew = new list;
cout << "ENTER VALUE FOR THE SELECTED NODE" << endl;
cin >> addNew->data;
if(first == NULL) {
addNew->next = NULL;
first = addNew;
last = addNew;
cout << "LINKED LIST CREATED SUCCESSFULLY" <<
endl;
}
else {
addNew->next = NULL;
last->next = addNew;
last = addNew;
cout << "DATA INSERTED IN LINKED LIST" << endl;
}
}

/**--------------------FUNCTION FOR DISPLYING NODE-----------------*/


void display() {
node = first;
cout << "LIST OF DATA INSIDE LIST" << endl;
while(node != NULL) {
cout << node->data << endl;
node = node->next;
}
}

/**--------------------FUNCTION FOR DELETING NODE-----------------*/

void del() {
int count = 0, number, i;
for(node = first; node != NULL; node = node->next)
count++;
display();
node = node1 = first;
cout << count << " NODES AVAILABLE HERE" << endl;
cout << "ENTER NUMBER OF NODE WHICH YOU WANT TO
DELETE" << endl;
cin >> number;
if(number != 1) {
if(number <= count) {
for(i = 2; i <= number; i++)
node = node->next;
for(i = 2; i <= number-1; i++)
node1 = node1->next;
node1->next = node->next;
node->next = NULL;
node = NULL;
}
else
cout << "INVALID NODE NUMBER" << endl;
}
else {
first = node->next;
node = NULL;
}
cout << "NODE DELETED!" << endl;
}

};

int main() {
system("color 0C");
int choice = 0;
linkedlist llist = linkedlist();

cout<<"YOU CAN PERFORM THESE FUCNTIONS:\n\nENTER THE


GIVEN NUMBER FOR PERFORMING FUNCTION";
cout<<"\n\nINSERTION PRESS 1.\n"<<endl;
cout<<"DELETION PRESS 2.\n"<<endl;
cout<<"DISPLAY PRESS 3.\n"<<endl;
cout<<"EXIT PRESS 4.\n"<<endl;
while(choice != 4)
{
cin >> choice;
switch(choice)
{
case 1:
llist.insert();
break;
case 2:
llist.del();
break;
case 3:
llist.display();
break;
case 4:
cout << "EXECUTION FINISHED...." << endl;
return 0;
break;
default:
cout<<"invalid choice!!\n\n";
}}

return 0;
}

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