// Pointers As Paramerter
// Raj Sahin N.
#include
#include
struct LL
{
char Name[30];
int Age;
struct LL *Next;
};
typedef struct LL Node;
void Insert(Node **Head,char *Name,int Age,int Pos)
{
int Temp=1;
Node *Curr,*NewNode;
NewNode=(Node *)malloc(sizeof(Node));
strcpy(NewNode->Name,Name);
NewNode->Age=Age;
if(Pos > Size(*Head)+1)
{
printf("Position Out Of Bound Exception...");
getch();
}
else if(Pos==1)
{
NewNode->Next=*Head;
*Head=NewNode;
}
else
{
Curr=*Head;
while(Curr->Next != NULL && Temp
Curr=Curr->Next;
Temp++;
}
NewNode->Next=Curr->Next;
Curr->Next=NewNode;
}
}
void Create(Node **Head)
{
Node *Curr,*NewNode;
int Ch=0,Age;
char Name[30];
do
{
NewNode=(Node *)malloc(sizeof(Node));
printf("Enter Name : ");
fflush(stdin);
gets(NewNode->Name);
printf("Enter Age : ");
scanf("%d",&NewNode->Age);
NewNode->Next=NULL;
if(*Head==NULL)
*Head=NewNode;
else
{
Curr=*Head;
while(Curr->Next != NULL)
Curr=Curr->Next;
Curr->Next=NewNode;
}
printf("\n Enter Choice (1-Enter Data / 0-Exit):");
scanf("%d",&Ch);
}while(Ch);
}
int Delete(Node **Head,char *Name)
{
int Pos=0;
Node *Curr=*Head,*Prev=NULL;
while(Curr!=NULL)
{
Pos++;
if(strcmp(Curr->Name,Name) == 0)
{
if(Pos==1)
*Head=(*Head)->Next;
else
Prev->Next=Curr->Next;
return Pos;
}
Prev=Curr;
Curr=Curr->Next;
}
return 0;
}
void Show(Node *Head)
{
if(Head != NULL)
{
printf("---------------------------------------------\n");
printf("Name : %s\n",Head->Name);
printf("Age : %d\n",Head->Age);
Show(Head->Next);
}
}
void Reverse(Node *Head)
{
if(Head != NULL)
{
Reverse(Head->Next);
printf("---------------------------------------------\n");
printf("Name : %s\n",Head->Name);
printf("Age : %d\n",Head->Age);
}
}
void ReverseLL(Node **Head)
{
Node *TempCurr,*TempPrev,*Curr,*Prev;
Prev=*Head;
Curr=(*Head)->Next;
while(1)
{
TempCurr=Curr;
TempPrev=Prev;
Prev=Curr;
Curr=Curr->Next;
if(TempPrev==*Head)
TempPrev->Next=NULL;
if(TempCurr!=NULL)
{
TempCurr->Next=TempPrev;
}
else
{
*Head=TempPrev;
break;
}
}
}
int Size(Node *Head)
{
if(Head == NULL)
return 0;
else
return 1+Size(Head->Next);
}
void main()
{
char Name[30];
int Age;
Node *Head=NULL;
int Ch,Temp;
do
{
clrscr();
printf("-------------------------------------\n");
printf("1. Create Linked List.\n");
printf("2. Insert In Linked List.\n");
printf("3. Delete From Linked List.\n");
printf("4. Print Linked List in Order.\n");
printf("5. Print Linked List in Reverse Order.\n");
printf("6. Reverse The Whole Linked List.\n");
printf("0. Exit.\n");
printf("-------------------------------------\n\n");
printf("Enter Your Choice : ");
scanf("%d",&Ch);
switch(Ch)
{
case 1:
Create(&Head);
break;
case 2:
printf("Enter Name : ");
fflush(stdin);
gets(Name);
printf("Enter Age : ");
scanf("%d",&Age);
printf("Enter Position For Insert Node (Less Than %d) : ",Size(Head));
scanf("%d",&Temp);
Insert(&Head,Name,Age,Temp);
break;
case 3:
printf("Enter Name For Delete: ");
fflush(stdin);
gets(Name);
if((Temp = Delete(&Head,Name)) == 0)
printf("Record Not Found...");
else
printf("Record Deleted Successfully At Position ");
getch();
break;
case 4:
Show(Head);
getch();
break;
case 5:
Reverse(Head);
getch();
break;
case 6:
ReverseLL(&Head);
break;
}
}while(Ch);
}
No comments:
Post a Comment