C language implementation of linked list

Source: Internet
Author: User
Tags define null

#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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.