Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct node{
- float data;
- node *next;
- node *prev;
- };
- class doublelinkedlist{
- node *head;
- public:
- doublelinkedlist(){
- head=NULL;
- }
- void insertatbegin(int d){
- node *nn=new node;
- nn->data=d;
- nn->prev=NULL;
- nn->next=head;
- if(head)
- {
- head->prev=nn;
- }
- head=nn;
- }
- void insertatend(int d){
- node *nn=new node;
- nn->data=d;
- nn->next=NULL;
- nn->prev=NULL;
- node *t=head;
- while(t->next){
- t=t->next;
- }
- t->next=nn;
- nn->prev=t;
- }
- void display(){
- node *t=head;
- while(t!=NULL){
- cout << t->data <<" -> ";
- t=t->next;
- }
- cout<<"NULL\n";
- }
- void deleted(int v)
- {
- node *t=head;
- while(t->data!=v && t!=NULL){
- t=t->next;
- }
- if(t->prev){
- t->prev->next=t->next;
- }
- if(t->next){
- t->next->prev=t->prev;
- }
- free(t);
- }
- void displayreverse(){
- node *t = head;
- while(t -> next != NULL){
- t = t -> next;
- }
- while(t != NULL){
- cout << t->data <<" -> ";
- t=t->prev;
- }
- cout << "NULL" << endl;
- }
- void deletionAtBegin(){
- node *t = head;
- if(t == NULL){
- cout << "Empty list" << endl;
- }
- else{
- head = head -> next;
- if(head != NULL){
- head -> prev = NULL;
- }
- free(t);
- }
- }
- void deletionAtEnd(){
- node *t = head;
- while(t -> next != NULL){
- t = t-> next;
- }
- if(t -> prev != NULL){
- t -> prev -> next = NULL;
- free(t);
- }
- else{
- free(t);
- head = NULL;
- }
- }
- void insertAtPos(){
- node *nn = new node;
- int n, pos;
- cout << "Enter a data part and position respectively:" << endl;
- cin >> n;
- cin >> pos;
- nn -> data = n;
- nn -> next = NULL;
- nn -> prev = NULL;
- if(head == NULL){
- insertatbegin(n);
- }
- else{
- int i = 0;
- node *p = head;
- while(i != pos -2){
- p = p -> next;
- i++;
- }
- nn -> next = p -> next;
- p -> next = nn;
- nn -> prev = p;
- }
- }
- void insertAftervalue(int v, int aftervalue);
- };
- int main(){
- int ch=0,v1=0;
- doublelinkedlist d;
- do{
- cout<<"1: Insert element at beginning\n";
- cout<<"2: Insert element at Ending\n";
- cout<<"3: Delete element \n";
- cout<<"4: Print in reverse order\n";
- cout<<"5: Delete at beginning\n";
- cout<<"6: Delete at end\n";
- cout<<"7: Insert after value\n";
- cout<<"0: Exit\n";
- cout<<"Enter choice" << endl;
- cin>>ch;
- switch(ch){
- case 1:
- cout<<"\nEnter the element to insert at the beginning:" << endl;
- cin>>v1;
- d.insertatbegin(v1);
- d.display();
- break;
- case 2:
- cout<<"\nEnter the element want to be at end:" << endl;
- cin>>v1;
- d.insertatend(v1);
- d.display();
- break;
- case 3:
- cout<<"\nEnter the element to be delete:" << endl;
- cin>>v1;
- d.deleted(v1);
- d.display();
- break;
- case 4:
- cout<<"\nList in reversed order is:" << endl;
- d.displayreverse();
- break;
- case 5:
- d.deletionAtBegin();
- d.display();
- break;
- case 6:
- d.deletionAtEnd();
- d.display();
- break;
- case 7:
- d.insertAtPos();
- d.display();
- break;
- }
- }while(ch!=0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement