#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
struct node* prev;
};
int flag; //temporary variable to handle error condition..
void display(struct node* thead);
int length(struct node* thead);
struct node* insertatfront(struct node* thead,int x);
struct node* insertatend(struct node* thead,int x);
struct node* insertatposition(struct node* thead,int x,int pos);
struct node* insertbefore(struct node* thead,int x,int e);
struct node* insertafter(struct node* thead,int x,int e);
struct node* deleteatfront(struct node* thead,int* pans);
struct node* deleteatend(struct node* thead,int* pans);
struct node* deleteatposition(struct node* thead,int pos,int* pans);
struct node* deletebefore(struct node* thead,int e,int* pans);
struct node* deleteafter(struct node* thead,int e,int* pans);
void main()
{
struct node* head=NULL;
int ch,val,pos,ele,dele;
int breakcondition=0;
printf("Choice:\n\n\n 1)Insertatfront.\n 2)Insertatend.\n 3)Insertatposition.\n 4)Insertbefore.\n 5)Insertafter.\n\n\n 6)Deleteatfront.\n 7)Deleteatend.\n 8)Deleteatposition.\n 9)Deletebefore.\n 10)Deleteafter.\n\n");
while(1){
printf("\nEnter your choice..");
scanf("%d",&ch);
switch(ch)
{
case 1:{
printf("\nEnter the element to be inserted..");
scanf("%d",&val);
head=insertatfront(head,val);
display(head);
break;
}
case 2:{
printf("\nEnter the element to be inserted..");
scanf("%d",&val);
head=insertatend(head,val);
display(head);
break;
}
case 3:{
printf("\nEnter the element to be inserted..");
scanf("%d",&val);
printf("\nEnter the position..");
scanf("%d",&pos);
head=insertatposition(head,val,pos);
display(head);
break;
}
case 4:{
printf("\nEnter the element to be inserted..");
scanf("%d",&val);
printf("\nEnter the reference element before which the element is to be inserted..");
scanf("%d",&ele);
head=insertbefore(head,val,ele);
display(head);
break;
}
case 5:{
printf("\nEnter the element to be inserted..");
scanf("%d",&val);
printf("\nEnter the reference element after which the element is to be inserted..");
scanf("%d",&ele);
head=insertafter(head,val,ele);
display(head);
break;
}
case 6:{
flag=0;
head=deleteatfront(head,&dele);
if(flag==0){
printf("\nThe element which is deleted is..%d",dele);
display(head);
}
break;
}
case 7:{
flag=0;
head=deleteatend(head,&dele);
if(flag==0){
printf("\nThe element which is deleted is..%d",dele);
display(head);
}
break;
}
case 8:{
flag=0;
printf("\nEnter the position..");
scanf("%d",&pos);
head=deleteatposition(head,pos,&dele);
if(flag==0){
printf("\nThe element which is deleted is..%d",dele);
display(head);
}
break;
}
case 9:{
flag=0;
printf("\nEnter the reference element before which the element is to be deleted..");
scanf("%d",&ele);
head=deletebefore(head,ele,&dele);
if(flag==0){
printf("\nThe element which is deleted is..%d",dele);
display(head);
}
break;
}
case 10:{
flag=0;
printf("\nEnter the reference element after which the element is to be deleted..");
scanf("%d",&ele);
head=deleteafter(head,ele,&dele);
if(flag==0){
printf("\nThe element which is deleted is..%d",dele);
display(head);
}
break;
}
default:{
breakcondition=1;
}
}
if(breakcondition==1)
break;
}
}
void display(struct node* thead)
{
struct node* temp=thead;
if(temp==NULL){
printf("\nThe list is empty..");
return;
}
printf("\nThe list is..");
while(temp!=NULL){
printf("%d ",temp->data);
temp=temp->next;
}
}
int length(struct node* thead)
{
int c=0;
struct node* temp=thead;
if(temp==NULL){
return c;
}
while(temp!=NULL){
c++;
temp=temp->next;
}
return c;
}
struct node* insertatfront(struct node* thead,int x)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->prev=NULL;
if(thead==NULL){ //corner case to handle if the list is empty..
newnode->next=NULL;
return newnode;
}
thead->prev=newnode;
newnode->next=thead;
return newnode;
}
struct node* insertatend(struct node* thead,int x)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=NULL;
if(thead==NULL){ //corner case to handle if the list is empty..
newnode->prev=NULL;
return newnode;
}
struct node* temp=thead;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newnode;
newnode->prev=temp;
return thead;
}
struct node* insertatposition(struct node* thead,int x,int pos)
{
int i;
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
if(pos==1){ //corner case to insert element at front..
thead=insertatfront(thead,x);
return thead;
}
if(pos==length(thead)+1){ //corner case to insert element at end;
thead=insertatend(thead,x);
return thead;
}
struct node* temp=thead;
for(i=1;i<=pos-2;i++){
temp=temp->next;
}
newnode->next=temp->next;
newnode->prev=temp;
temp->next->prev=newnode;
temp->next=newnode;
return thead;
}
struct node* insertbefore(struct node* thead,int x,int e)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
struct node* temp=thead;
if(temp->data==e){ //corner case to insert element before the first element..
thead=insertatfront(thead,x);
return thead;
}
while(temp->next!=NULL && temp->next->data!=e){
temp=temp->next;
}
newnode->next=temp->next;
newnode->prev=temp;
temp->next->prev=newnode;
temp->next=newnode;
return thead;
}
struct node* insertafter(struct node* thead,int x,int e)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
struct node* temp=thead;
while(temp!=NULL && temp->data!=e){
temp=temp->next;
}
if(temp->next==NULL){ //corner case to insert element at the end of the list..
thead=insertatend(thead,x);
return thead;
}
newnode->next=temp->next;
newnode->prev=temp;
temp->next->prev=newnode;
temp->next=newnode;
return thead;
}
struct node* deleteatfront(struct node* thead,int* pans)
{
if(thead==NULL){ //corner case to handle if the list is empty..
printf("\nThe list is empty..");
flag=1;
return thead;
}
struct node* temp=thead;
*pans=temp->data;
thead=thead->next;
thead->prev=NULL;
free(temp);
return thead;
}
struct node* deleteatend(struct node* thead,int* pans)
{
if(thead==NULL){ //corner case to handle if the list is empty..
printf("\nThe list is empty..");
flag=1;
return thead;
}
struct node* temp=thead;
while(temp->next!=NULL){
temp=temp->next;
}
*pans=temp->data;
if(temp->prev==NULL){ //corner case to handle if the list has only one element..
free(temp);
return NULL;
}
temp->prev->next=NULL;
free(temp);
return thead;
}
struct node* deleteatposition(struct node* thead,int pos,int* pans)
{
if(thead==NULL){ //corner case to handle if the list is empty..
printf("\nThe list is empty..");
flag=1;
return thead;
}
if(pos==1){ //corner case to delete element at front..
thead=deleteatfront(thead,pans);
return thead;
}
if(pos==length(thead)){ //corner case to delete element at end..
thead=deleteatend(thead,pans);
return thead;
}
int i;
struct node* temp=thead;
for(i=1;i<=pos-1;i++){
temp=temp->next;
}
*pans=temp->data;
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
return thead;
}
struct node* deletebefore(struct node* thead,int e,int* pans)
{
if(thead==NULL){ //corner case to handle if the list is empty..
printf("\nThe list is empty..");
flag=1;
return thead;
}
struct node* temp=thead;
if(temp->data==e){ //corner case to handle error condition to delete element before the first element..
printf("\nPlease enter a valid element..");
return thead;
}
if(temp->next->data==e){ //corner case to delete front element..
thead=deleteatfront(thead,pans);
return thead;
}
while(temp->next!=NULL && temp->next->data!=e){
temp=temp->next;
}
*pans=temp->data;
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
return thead;
}
struct node* deleteafter(struct node* thead,int e,int* pans)
{
if(thead==NULL){ //corner case to handle if the list is empty..
printf("\nThe list is empty..");
flag=1;
return thead;
}
struct node* temp=thead->next;
while(temp->next!=NULL && temp->prev->data!=e){
temp=temp->next;
}
if(temp->next==NULL && temp->prev->data==e){ //corner case to delete element at end..
thead=deleteatend(thead,pans);
return thead;
}
if(temp->next==NULL && temp->data==e){ //corner case to handle error condition to delete element after the last element..
printf("\nPlease enter a valid element..");
return thead;
}
*pans=temp->data;
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
return thead;
}
No comments:
Post a Comment