#include <stdio.h>
#include <malloc.h>
typedef struct node
{
int data;
node *rlink;
node *llink;
}node;
void insertnode(node *head);
void deletenode(node *head);
void clearall(node *head);
void printall(node *head);
void menu();
void insertnode(node *head)
{
int x;
int num;
int i=0;
node *newnode;
node *nextr;
node *nextl;
newnode = (node*)malloc(sizeof(node));
printf("Please enter the position you wish to insert.\n");
scanf("%d", &x);
while(x<=0)
{
printf("Please enter a number bigger than 0.\n");
scanf("%d", &x);
}
node *current;
current=head;
node *before;
while(current!=NULL)
{
if(i==x)
break;
before=current;
current=current->rlink;
i++;
}
if(x>i)
{
printf("there aren't enough nodes.\n");
return;
}
printf("Please insert the number you want to insert.\n");
scanf("%d", &num);
newnode->data = num;
newnode->rlink=current;
newnode->llink=before;
if(current!=NULL)
current->llink=newnode;
if(before!=NULL)
before->rlink=newnode;
printf("insert complete.\n");
}
void deletenode(node *head)
{
int x;
printf("Please enter the position you wish to delete.\n");
scanf("%d", &x);
while(x<=0)
{
printf("Please enter a number bigger than 0.\n");
scanf("%d", &x);
}
int i;
node *current;
current=head;
if(current->rlink==NULL)
{
printf("the list is empty\n");
return;
}
else
{
node *nextl;
node *nextr;
nextl=current->llink;
nextr=current->rlink;
for(i=0;i<x;i++)
{
current=current->rlink;
if(current==NULL)
{
printf("there aren't enough nodes.\n");
return;
}
if(current!=NULL)
nextl=current->llink;
else
nextl=nextl->rlink;
if(current!=NULL)
nextr=current->rlink;
}
if(x>i)
{
printf("there aren't enough nodes.\n");
return;
}
if(nextr==NULL)
{
nextl->rlink=NULL;
free(current);
}
else
{
nextl->rlink=nextr;
nextr->llink=nextl;
}
printf("delete complete\n");
}
}
void printall(node *head)
{
if(head->rlink==NULL)
{
printf("the list is empty.\n");
return;
}
else
{
node *current;
current=head->rlink;
printf("List = ");
while(current->rlink!=NULL)
{
printf("%d, ", current->data);
current=current->rlink;
}
printf("%d\n", current->data);
}
}
void deleteall(node *head)
{
if(head->rlink==NULL)
{
printf("the list is already empty\n");
return;
}
else
{
node *current;
node *next;
next=head->rlink;
current=next;
while(current->rlink!=NULL)
{
next=current->rlink;
free(current);
current=next;
}
free(current);
head->rlink=NULL;
printf("delete complete\n");
}
}
void menu()
{
node *head;
head=(node*)malloc(sizeof(node));
head->llink=NULL;
head->rlink=NULL;
int type;
while(type!=5)
{
printf("-----------------------------------------------\n");
printf("Welcome to list management!\n");
printf("Insert 1 to insert in a desired position.\n");
printf("Insert 2 to delete a desired position.\n");
printf("Insert 3 to print all nodes.\n");
printf("Insert 4 to delete all nodes.\n");
printf("Insert 5 to quit.\n");
scanf("%d", &type);
if(type==1)
insertnode(head);
else if(type==2)
deletenode(head);
else if(type==3)
printall(head);
else if(type==4)
deleteall(head);
else if(type==5)
{
printf("BYE.\n");
return;
}
else
printf("please insert a number between 1 and 5\n");
printf("-----------------------------------------------\n");
}
}
int main()
{
menu();
return 0;
}