Created
February 22, 2016 11:44
-
-
Save ronakjain2012/465c375fa804cf0dde19 to your computer and use it in GitHub Desktop.
Doubly link list with header circular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<stdlib.h> | |
#include<conio.h> | |
using namespace std; | |
struct node { | |
int data; | |
struct node* next; | |
struct node* per; | |
}; | |
struct node* Start_; | |
struct node* End_; | |
struct node *temp, *head, *ptr; | |
void insert_in_list(); | |
void disp_list(); | |
int main() { | |
head = (struct node*) malloc(sizeof(struct node)); | |
Start_ = head; | |
End_ = head; | |
head->per = End_; | |
head->next= Start_; | |
head->data = 0; | |
int sel=1; | |
while(sel) { | |
cout<<"------------ Select Choice --------------"; | |
cout<<"\n1. Insert\n"; | |
cout<<"2. Display\n"; | |
int a; | |
cout<<"Select :"; | |
cin>>a; | |
switch(a) { | |
case 1 : insert_in_list(); | |
break; | |
case 2 : disp_list(); | |
break; | |
} | |
cout<<"1 To Count. 0 To exit : "; | |
cin>>sel; | |
} | |
} | |
void insert_in_list() { | |
temp = (struct node*) malloc(sizeof(struct node)); | |
int counter; | |
counter = head->data; | |
cout<<"Node Value :"; | |
int X=0; | |
cin>>X; | |
temp->per = head; | |
temp->data = X; | |
temp->next = head->next; | |
head->next = temp; | |
counter++; | |
head->data = counter; | |
ptr = head; | |
temp = head; // Calculation Previous Nodes | |
while(ptr->next != Start_) { | |
ptr=ptr->next; | |
ptr->per=temp; | |
temp=temp->next; | |
} | |
//ptr->per=temp; | |
End_ = ptr; | |
cout<<"\n\n Inserted \n\n"; | |
} | |
void disp_list() { | |
ptr = Start_->next; | |
cout<<"\nStart :"<<Start_; | |
cout<<"\nTail :"<<End_<<"\n"; | |
while(ptr != Start_) { | |
cout<<"\n------ \\ \\ \\ \\ "<<ptr<<" \\ \\ \\ \\ ------\n"; | |
cout<<"\t"<<ptr->per<<"\t"<<ptr->data<<"\t"<<ptr->next<<"\n"; | |
ptr=ptr->next; | |
cout<<"|---------------------------------------|>\n"; | |
} | |
getch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment