Skip to content

Instantly share code, notes, and snippets.

@mukeshkdangi
Created January 12, 2014 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mukeshkdangi/8384341 to your computer and use it in GitHub Desktop.
Save mukeshkdangi/8384341 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<stdlib.h>
struct dlist{
int info;
struct dlist *next;
struct dlist *prev;
};
struct dlist *start;
void deletenode(struct dlist *);
void insertbynode(struct dlist *);
void insert(struct dlist *);
int count(struct dlist *);
void creatdlist(struct dlist *);
void display(struct dlist *);
int main(){
int e,i,n,c;
printf("\nDoubly link list .....");
struct dlist *node;
//clrscr();
node=(struct dlist *)malloc(sizeof( struct dlist ));
start=node;
creatdlist(node);
printf("\n1.Insert.\n2.displa\3.Quit\n4.Count node\n5.insert by node\n6.delete node");
fflush(stdin);
scanf("%d",&c);
while(c!=3){
switch(c){
case 1:
insert(node);
node=start;
break;
case 2:
display(node);
break;
case 3:
break;
case 4:
e=count(node);
printf("\nTotal number of node :%d",e);
break;
case 5:
insertbynode(node);
node;
break;
case 6:
deletenode(node);
break;
}
printf("\n1.Insert.\n2.displa\3.Quit\n4.Count node\n5.insert by node\n6.delete node");
fflush(stdin);
scanf("%d",&c);
}
return 0;
}
void creatdlist(struct dlist *node){
int i=1;
char ch;
node=start;
struct dlist *curr;
printf("\nENter %d node value :",i++);
fflush(stdin);
scanf("%d",&node->info);
node->next=NULL;
node->prev=NULL;
printf("\nEnter n ->quit :");
fflush(stdin);
scanf("%c",&ch);
while(ch!='n'){
curr=(struct dlist *)malloc(sizeof(struct dlist ));
node->next=curr;
curr->prev=node;
node=node->next;
printf("\nEnter the %d node value:",i++);
fflush(stdin);
scanf("%d",&node->info);
node->next=NULL;
printf("\nEnter n ->quit:");
fflush(stdin);
scanf("%c",&ch);
}
}
void display(struct dlist *node){
while(node!=NULL){
printf("%d->",node->info);
node=node->next;
}
}
void insert(struct dlist *node){
int i=1,pos=0;
struct dlist *pprev=start,*curr=start;
int t=count(node);
printf("\nEnter the location of node :");
fflush(stdin);
scanf("%d",&pos);
if(pos>t){
printf("\nOut of memory:");
}
else{
curr=(struct dlist*)malloc(sizeof(struct dlist ));
printf("\nEnter the value of node:");
fflush(stdin);
scanf("%d",&curr->info);
if(pos==1){
curr->next=node;
node->prev=curr;
curr->prev=NULL;
//node=curr;
start =curr;
}else{
while(i!=pos){
i++;
pprev=node;
node=node->next;
}
pprev->next=curr;
curr->prev=pprev;
curr->next=node;
node->prev=curr;
node=curr;
}
}
}
int count(struct dlist *node){
int j=0;
while(node!=NULL){
node=node->next;
j++;
}
return j;
}
void insertbynode(struct dlist *node){
int i=1,val,r;
r=count(node);
struct dlist *pprev,*curr;
printf("\nEnter the node value after u want to enter the new node :");
scanf("%d",&val);
curr=(struct dlist *)malloc(sizeof(struct dlist ));
while(val!=node->info){
pprev=node;
node=node->next;
i++;
}
if(i>r){
printf("\nNode doen't exist :");
return ;
}else{
curr=(struct dlist *)malloc(sizeof(struct dlist ));
printf("\nEnter the new node value :");
fflush(stdin);
scanf("%d",&curr->info);
pprev->next=curr;
curr->prev=pprev;
curr->next=node;
node->prev=curr;
node=curr;
}
}
void deletenode(struct dlist *node){
int i=1,pos;
struct dlist *pprev,*curr,*temp;
printf("\nEnter the location of node which u want to delete:");
fflush(stdin);
scanf("%d",&pos);
int t=count(node);
if(pos>t){
printf("\nCan't be deleteed:");
}
else{
while(i!=pos){
pprev=node;
node=node->next;
i++;
}
temp=node;
pprev->next=node->next;
node->next->prev=pprev;
free(temp);
node=node->next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment