Doubly Link List
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct node{
struct node *prev;
int data;
struct node *next;
}*head,*tail,*loc,*nn;
enterValue(){
int val;
printf("\nEnter Value:");
scanf("%d",&val);
return val;
}
memAlloc(){
nn=malloc(sizeof(struct node));
return 0;
}
insertB(){
memAlloc();
nn->data=enterValue();
nn->prev=NULL;
if(head==NULL && tail==NULL){
tail=nn;
nn->next=NULL;
head=nn;
}
else{
nn->next=head;
head->prev=nn;
head=nn;
}
return 0;
}
int insertL(){
memAlloc();
nn->data=enterValue();
nn->next=NULL;
if(head==NULL && tail==NULL){
head=nn;
tail=nn;
nn->prev=NULL;
}
else{
tail->next=nn;
nn->prev=tail;
tail=nn;
}
return 0;
}
insertAV(){
}
traverseF(){
loc=head;
printf("Forward Traverse:\n");
while(loc!=NULL){
printf("%u+%d+%u \n",loc->prev,loc->data,loc->next);
loc=loc->next;
}
return 0;
}
traverseB(){
loc=tail;
printf("Backward Traverse:\n");
while(loc!=NULL){
printf("%u+%d+%u \n",loc->prev,loc->data,loc->next);
loc=loc->prev;
}
}
int main(int argc, char *argv[]) {
int c;
header:
printf("\n1. Insert Before First Link");
printf("\n2. Insert After Last Link");
printf("\n3. Insert After Value");
printf("\n4. Traverse Forward");
printf("\n5. Traverse Backward");
printf("\n6. Exit");
printf("\nYour Choice:");
if(getch()=='1')
insertB(22);
else if(getch()=='2')
insertL(43);
else if(getch()=='3')
insertAV();
else if(getch()=='4')
traverseF();
else if(getch()=='5')
traverseB();
else if(getch()=='6')
goto footer;
else
{
printf("Wrong Choice Please Enter Again!");
goto header;
}
footer:
return 0;
}
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct node{
struct node *prev;
int data;
struct node *next;
}*head,*tail,*loc,*nn;
enterValue(){
int val;
printf("\nEnter Value:");
scanf("%d",&val);
return val;
}
memAlloc(){
nn=malloc(sizeof(struct node));
return 0;
}
insertB(){
memAlloc();
nn->data=enterValue();
nn->prev=NULL;
if(head==NULL && tail==NULL){
tail=nn;
nn->next=NULL;
head=nn;
}
else{
nn->next=head;
head->prev=nn;
head=nn;
}
return 0;
}
int insertL(){
memAlloc();
nn->data=enterValue();
nn->next=NULL;
if(head==NULL && tail==NULL){
head=nn;
tail=nn;
nn->prev=NULL;
}
else{
tail->next=nn;
nn->prev=tail;
tail=nn;
}
return 0;
}
insertAV(){
}
traverseF(){
loc=head;
printf("Forward Traverse:\n");
while(loc!=NULL){
printf("%u+%d+%u \n",loc->prev,loc->data,loc->next);
loc=loc->next;
}
return 0;
}
traverseB(){
loc=tail;
printf("Backward Traverse:\n");
while(loc!=NULL){
printf("%u+%d+%u \n",loc->prev,loc->data,loc->next);
loc=loc->prev;
}
}
int main(int argc, char *argv[]) {
int c;
header:
printf("\n1. Insert Before First Link");
printf("\n2. Insert After Last Link");
printf("\n3. Insert After Value");
printf("\n4. Traverse Forward");
printf("\n5. Traverse Backward");
printf("\n6. Exit");
printf("\nYour Choice:");
if(getch()=='1')
insertB(22);
else if(getch()=='2')
insertL(43);
else if(getch()=='3')
insertAV();
else if(getch()=='4')
traverseF();
else if(getch()=='5')
traverseB();
else if(getch()=='6')
goto footer;
else
{
printf("Wrong Choice Please Enter Again!");
goto header;
}
footer:
return 0;
}
No comments:
Post a Comment