#ifndef _const_h_
#define _const_h_
#include <stdio.h>
#include <stdlib.h>
typedef enum
{
False = 0,
True,
}bool;
typedef int ELEMTYPE;
#define QUEUE_MAX_SIZE 10
#define STACK_INIT_SIZE 10
#define STACK_INCREMENT_SIZE 2
#define NULL ((void *) 0)
typedef enum
{
NORMAL = 0,
ERROR,
Underflow,
OVERFLOW,
Statuscount,
}status;
#endif
#ifndef _linked_table_h_
#define _linked_table_h_
#include "Const.h"
typedef struct LINKEDNODE
{
Elemtype data;
struct Linkednode *pnext;
struct Linkednode *pprev;
}linkednode, *plinkednode;
Plinkednode initlinkedtable ();
Bool islinkedtableempty (Plinkednode PLN);
Status Addnodeinhead (Plinkednode PLN, elemtype elem);
Status Addnodeintail (Plinkednode PLN, elemtype elem);
void Traverselinkedtable (Plinkednode pLN);
void Destorylinkedtable (Plinkednode pLN);
int Getlinkedtablelength (Plinkednode PLN);
void Removeeleminlinkedtable (Plinkednode PLN, elemtype elem);
#endif
#include "LinkedTable.h"
Plinkednode initlinkedtable ()
{
Plinkednode PLN = (plinkednode) malloc (sizeof (Linkednode));
if (PLN = = Null)
{
printf ("No accessable free memeory.\n");
return Null;
}
Data in head node was used to store linked table data count
Pln->data = 0;
Pln->pnext = Null;
return PLN;
}
Bool islinkedtableempty (Plinkednode PLN)
{
Plinkednode phead = PLN;
if (Phead->pnext = = Null)
{
return True;
}
Else
{
return False;
}
}
Status Addnodeinhead (Plinkednode PLN, Elemtype elem)
{
printf ("in Function addnodeinhead.\n");
Plinkednode phead = PLN;
Plinkednode Ptempnode = (plinkednode) malloc (sizeof (Linkednode));
if (Ptempnode = = Null)
{
printf ("Can not add the node for no accessable free memory.\n");
return ERROR;
}
Ptempnode->data = Elem;
Ptempnode->pnext = phead->pnext;
Phead->data + = 1;
Phead->pnext = Ptempnode;
return NORMAL;
}
Status Addnodeintail (Plinkednode PLN, Elemtype elem)
{
printf ("in Function addnodeintail.\n");
Plinkednode phead = PLN;
Plinkednode Ptempnode = (plinkednode) malloc (sizeof (Linkednode));
if (Ptempnode = = Null)
{
printf ("Can not add the node for no accessable free memory.\n");
return ERROR;
}
Phead->data + = 1;
if (Islinkedtableempty (Phead))
{
Phead->pnext = Ptempnode;
}
Else
{
Plinkednode ptemp = Phead;
while (ptemp->pnext! = Null)
{
Ptemp = ptemp->pnext;
}
Ptemp->pnext = Ptempnode;
}
Ptempnode->data = Elem;
Ptempnode->pnext = Null;
return NORMAL;
}
void Traverselinkedtable (Plinkednode pLN)
{
if (Islinkedtableempty (PLN))
{
printf ("The Linked Table is empty.\n");
}
Else
{
Plinkednode phead = PLN;
Plinkednode ptemp = phead->pnext;
while (ptemp! = Null)
{
printf ("Value:%d\n", ptemp->data);
Ptemp = ptemp->pnext;
}
}
}
void Destorylinkedtable (Plinkednode pLN)
{
if (Islinkedtableempty (PLN))
{
printf ("The Linked Table is empty.\n");
Free (PLN);
}
Else
{
Plinkednode phead = PLN;
Plinkednode Q;
Plinkednode ptemp = Phead;
while (ptemp! = Null)
{
Q = ptemp->pnext;
Free (ptemp);
ptemp = q;
}
}
}
int Getlinkedtablelength (Plinkednode PLN)
{
Return pln->data;
}
void Sortlinkedtable (Plinkednode pLN)
{
}
void Removeeleminlinkedtable (Plinkednode PLN, Elemtype elem)
{
if (Islinkedtableempty (PLN))
{
printf ("The Linked Table is empty.\n");
}
Else
{
Bool Hastargetnode = False;
Plinkednode phead = PLN;
Plinkednode ptemp = Phead;
while (ptemp->pnext! = Null)
{
if (Ptemp->pnext->data = = Elem)
{
Hastargetnode = True;
Ptemp->pnext = ptemp->pnext->pnext;
}
Else
{
Ptemp = ptemp->pnext;
}
}
if (Hastargetnode = = False)
{
printf ("The data is not in current Linked table.\n");
}
Else
{
printf ("Elem%d has been removed successfully.\n", Elem);
}
}
}
C language implementation of linked list