Doubly Linked List
struct Node
{
int element;
struct Node *Next;
struct Node *Prev;
};
typedef struct Node *Position, *List;
//typedef PtrToNode Position;
//typedef PtrToNode List;
Position Find(int x, List L)
{
Position P;
P = L->Next;
while(P!=NULL && P->element!=x)
P = P->Next;
return P;
}
void Delete(int x, List L)
{
Position P, Tmpcell;
P=L->Next;
while(P->element!=x)
P=P->Next;
if(P!=NULL)
{
Tmpcell = P;
P->Prev->Next = Tmpcell->Next;
P->Next->Prev = Tmpcell->Prev;
free(Tmpcell);
}
}
void InsertAtBegining(int x, List L)
{
Position Tmpcell;
Tmpcell = (struct Node *)malloc(sizeof(struct Node));
if(Tmpcell == NULL)
printf("Error. Unable to allocate memory\n");
Tmpcell->element = x;
Tmpcell->Next = L->Next;
Tmpcell->Prev = L;
L->Next = Tmpcell;
}
void InsertAtEnd(int x, List L)
{
Position P, Tmpcell;
P = L;
Tmpcell = (struct Node *)malloc(sizeof(struct Node));
if(Tmpcell == NULL)
printf("Error. Unable to allocate memory\n");
while(P->Next!=NULL)
P = P->Next;
Tmpcell->element = x;
Tmpcell->Next = P->Next;
Tmpcell->Prev = P;
P->Next = Tmpcell;
}
void InsertAtPosition(int x, int index, List L)
{
Position P, Tmpcell;
int i=1;
P=L;
Tmpcell = (struct Node *)malloc(sizeof(struct Node));
if(Tmpcell == NULL)
printf("Error. Unable to allocate memory\n");
while(i<index)
{
P = P->Next;
i++;
}
Tmpcell->element = x;
Tmpcell->Next = P->Next;
Tmpcell->Prev = P;
P->Next = Tmpcell;
}
List Create()
{
List L;
L = (struct Node *)malloc(sizeof(struct Node));
if(L==NULL)
printf("Error. Unable to allocate mempry.\n");
else
{
L->element = -1;
L->Next = NULL;
L->Prev = NULL;
}
return L;
}
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu