Doubly Link List
Author |
Message |
alpesh
|
Posted: Wed Feb 04, 2004 10:04 pm Post subject: Doubly Link List |
|
|
// TC++
#include <stdio.h>
#include <conio.h>
typedef struct list
{
int x;
struct list *next;
struct list *prev;
}node;
node *create(void);
node *insert(node *);
node *del(node *);
void print(node *);
void main()
{
node *first;
char ch,ans='y';
clrscr();
first=create();
while(ans=='y')
{
printf("What Do You Want To Do ? ");
printf("\nTo Insert Press I\nTo Delete Press D\nTo Print Press P\nChoice----> ");
scanf(" %c",&ch);
switch(ch)
{
case 'i':
first=insert(first);
print(first);
break;
case 'd':
first=del(first);
print(first);
break;
case 'p':
print(first);
}
printf("Do You Want To Continue(Y/N) ? ");
scanf(" %c",&ans);
}
getch();
}
node *create(void)
{
char ans = 'y';
node *first=NULL,*temp,*curr;
while(ans=='y')
{
temp=(node *)malloc(sizeof(node));
printf("Please Enter The Value Of X----> ");
scanf(" %d",&temp->x);
temp->prev=temp->next=NULL;
if(!first)
first=temp;
else
{
curr->next=temp;
temp->prev=curr;
}
curr=temp;
printf("Want To Continue(Y/N) ? ");
scanf(" %c",&ans);
}
return(first);
}
void print(node *first)
{
node *curr;
curr=first;
for(;curr;curr=curr->next)
printf("%4d",curr->x);
}
node *insert(node *first)
{
node *curr,*temp;
int i,x;
printf("At Which Position Do You Want To Insert ---> ");
scanf(" %d",&x);
temp=(node *)malloc(sizeof(node));
temp->next=temp->prev=NULL;
printf("Please Enter The Value Of X----> ");
scanf(" %d",&temp->x);
if(x==1)
{
first->prev=temp;
temp->next=first;
first=temp;
}
else
{
curr=first;
for(i=1;i<x-1 && curr->next;i++)
curr=curr->next;
curr->next->prev=temp;
temp->next=curr->next;
temp->prev=curr;
curr->next=temp;
}
return(first);
}
node *del(node *first)
{
node *curr;
int x,i;
curr=first;
printf("At Which Position Do You Want To Delete ? ");
scanf(" %d",&x);
if(x==1)
{
first=first->next;
first->prev=NULL;
}
else
{
for(i=1;i<x-1;i++)
curr=curr->next;
curr->next=curr->next->next;
curr->next->prev=curr;
}
return(first);
} |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|