Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct Node{
- int data;
- Node *next;
- };
- class singlylinkedlist{
- public:
- Node *head;
- Node *t = head;
- singlylinkedlist(){
- head=NULL;
- }
- void insertAtEnd(int v);
- void insertAtBeg(int v);
- void insertAtPos(int v,int pos);
- void insertAftervalue(int v,int aftervalue);
- void deletevalue(int v);
- int searchrecord(int v);
- void reverselist();
- void display();
- void reverselistusingrecusrion(Node *t){
- if(t -> next == NULL){
- cout << t -> data << "->";
- }
- else{
- reverselistusingrecusrion(t -> next);
- cout << t -> data << "->";
- }
- }
- };
- void singlylinkedlist::insertAtEnd(int v){
- Node *nn=new Node;
- nn->data=v;
- if(head==NULL){
- head=nn;
- nn->next=NULL;
- }
- else {
- Node *t=head;
- while(t->next!=NULL){
- t=t->next;
- }
- nn->next=NULL;
- t->next=nn;
- }
- }
- void singlylinkedlist::insertAtBeg(int v){
- Node *nn=new Node;
- nn->data=v;
- if(head==NULL){
- head=nn;
- nn->next=NULL;
- }
- else{
- nn->next=head;
- head=nn;
- }
- }
- void singlylinkedlist::insertAtPos(int v,int pos){
- Node *t=head;
- Node *nn=new Node;
- nn->data=v;
- for(int i=1;i<pos-1;i++){
- if(t->next!=NULL && head!=NULL){
- t=t->next;
- }
- else{
- cout<<"NO Sufficient Elements";
- break;
- }
- }
- nn->next=t->next;
- t->next=nn;
- }
- void singlylinkedlist::insertAftervalue(int v,int aftervalue){
- Node *nn=new Node;
- nn->data=v;
- nn->next=NULL;
- Node *t=head;
- while(t->data!=aftervalue){
- t=t->next;
- }
- nn->next=t->next;
- t->next=nn;
- }
- void singlylinkedlist::deletevalue(int v){
- Node *prev=NULL;
- Node *t=head;
- if(t->data==v){
- head=head->next;
- free(t);
- }
- else{
- while(t->data!=v){
- prev=t;
- t=t->next;
- if(t==NULL){
- break;
- }
- }
- prev->next=t->next;
- free(t);
- }
- }
- int singlylinkedlist::searchrecord(int v){
- Node *t=head;
- while(t!=NULL){
- if(t->data==v){
- return true;
- }
- else{
- t=t->next;
- }
- }
- return 12;
- }
- void singlylinkedlist::display()
- {
- Node *t=head;
- while(t!=NULL){
- cout<<t->data<<"->";
- t=t->next;
- }
- cout<<"NULL\n";
- }
- void singlylinkedlist::reverselist(){
- Node *c=head;
- Node *prev=NULL;
- Node *n=NULL;
- while(c!=NULL){
- n=c->next;
- c->next=prev;
- prev=c;
- c=n;
- }
- head=prev;
- }
- int main(){
- int ch=11,v1;
- singlylinkedlist s;
- while(ch!=0){
- cout<<"Enter 1 to insert node at begin\n";
- cout<<"Enter 2 to insert node at end.\n";
- cout<<"Enter 3 to add node at desired position.\n";
- cout<<"Enter 4 to add node after the value\n";
- cout<<"Enter 5 to delete the node\n";
- cout<<"Enter 6 to search the element\n";
- cout<<"Enter 7 to display the linkedlist\n";
- cout<<"Enter 8 to reverse linked list\n";
- cout<<"Enter 9 to reverse linked list using recursion\n";
- cout<<"Enter 0 to Exit\n"<<endl;
- cout<<"Enter your choice:";
- cin>>ch;
- switch(ch){
- case 1:
- cout<<"Enter the value which to be add at beginning:";
- cin>>v1;
- s.insertAtBeg(v1);
- break;
- case 2:
- cout<<"Enter the value to be add at ending:";
- cin>>v1;
- s.insertAtEnd(v1);
- break;
- case 3:
- int position;
- cout<<"Enter the value:";
- cin>>v1;
- cout<<"Enter the position:";
- cin>>position;
- s.insertAtPos(v1, position);
- break;
- case 4:
- int aftervalue;
- cout<<"Enter the value to be added:";
- cin>>v1;
- cout<<"Enter the node after which element to be added:";
- cin>>aftervalue;
- s.insertAftervalue(v1, aftervalue);
- break;
- case 5:
- cout<<"Enter the element to be delete:";
- cin>>v1;
- s.deletevalue(v1);
- break;
- case 6:
- cout<<"Enter the element to search:";
- cin>>v1;
- if(s.searchrecord(v1)){
- cout<<"Element is found"<<endl;;
- }
- else{
- cout<<"Element is not found"<<endl;
- }
- break;
- case 7:
- s.display();
- break;
- case 8:
- s.reverselist();
- break;
- case 9:
- s.reverselistusingrecusrion(s.head);
- cout << "NULL" << endl;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement