C语言从链表中删除素数,Data Structures 之 链表

链表(Linked List),由一系列不必在内存中相连的结构组成。

按C的约定,函数的声明都在头文件中,具体的Node声明在.c文件中。

adt.h

//按照C 的约定,作为类型的List表和Position以及函数原型都在头文件中

typedef int ElementType;

#ifndef ADT_H

#define ADT_H

struct Node;

typedef struct Node *PtrToNode; //定义指针指向结构体

typedef PtrToNode List;

typedef PtrToNode pNode;

bool IsEmpt(List L);

bool IsLast(pNode P, List L);

pNode Find(ElementType X, List L);

void Delete(ElementType X, List L);

pNode FindPrevious(ElementType X, List L);

void Insert(ElementType X, List L, pNode P);

void DeleteList(List L);

#endif // ADT_H_INCLUDED

结构体几点说明:

1.Element 表示节点的数值,struct存储在内存上,而 pNode为指向struct的指针。

2. typedef struct X{} X0 ; ,X0为类型struct X 的别名。

struct Y{}y; , y 是struct Y的一个变量。

main.cpp

#include

#include

#include

#include "adt.h"

#include "fatal.h"

using namespace std;

//定义链表中的节点

struct Node

{

ElementType Element;

struct Node *Next;

};

/*

struct Node *create()

{

struct Node *head, *p;

int x;

head = NULL;

cout << "Input Data " << endl;

cin >> x;

p = (struct Node *)malloc (sizeof (struct Node));

p -> Element = x;

p -> Next = NULL ;

head = p;

return head;

}

*/

struct Node *CreateList()

{

int len;

int val;

struct Node *pHead, *pTail;

pHead = (struct Node *)malloc (sizeof (struct Node));

cout << "please input the length of the List : " << endl;

cin >> len;

pHead -> Element = len;

pTail = pHead;

pTail -> Next = NULL;

for(int i=0; i < len; i++)

{

cout << "第" << i+1 <

cin >> val;

struct Node *pNew = (struct Node *)malloc (sizeof (struct Node));

pNew -> Element = val;

pTail -> Next = pNew;

pNew -> Next = NULL;

pTail = pNew;

}

return pHead;

}

//遍历链表函数

void TraverList(List pHead)

{

pNode p = pHead -> Next;

while(NULL != p)

{

cout << p -> Element << " ";

p = p -> Next;

}

cout << endl;

}

//判断列表是否为空

bool IsEmpt(List L)

{

if(L -> Next == NULL)

{

cout << "Empty !!!" << endl;

return true;

}

else

{

cout << "NOT Empty !!! ";

return false;

}

}

//判断节点P 是不是链表的最后一个节点

bool IsLast(pNode P, List L)

{

if(P->Next == NULL)

{

cout << "the last one " << endl;

return true;

}

else

{

cout << "the Node P is Not the last !!!" << endl;

return false;

}

}

//查找X, 返回指针P

pNode Find(ElementType x, List L)

{

pNode p;

p = L -> Next;

while(p != NULL && p -> Element != x)

{

p = p -> Next;

}

return p;

}

//查找前一个节点

pNode FindPrevious(ElementType x, List L)

{

pNode p;

p = L;

while(p->Next != NULL && p->Next->Element !=x)

{

p = p->Next;

}

return p;

}

//删除一个节点

void Delete(ElementType x, List L)

{

pNode p, q;

p = FindPrevious(x,L);

q = p -> Next;

if( !IsLast(q,L))

{

p -> Next = q -> Next;

free(q);

}

else

{

p -> Next = NULL;

free(q);

}

}

//插入一个节点

void Insert(ElementType x, List L, pNode P)

{

pNode temp;

temp = (struct Node *)malloc (sizeof (struct Node));

if( temp == NULL)

{

FatalError("out of space ");

}

temp -> Element = x;

temp -> Next = P-> Next;

P -> Next = temp;

}

int main()

{

cout << "the program is starting ..." << endl;

pNode pHead = NULL;

pHead = CreateList();

TraverList(pHead);

IsEmpt(pHead);

pNode p;

p = Find(3,pHead);

if(p)

{

cout << endl << "Find the element : ";

cout << p -> Element << endl;

}

else {cout << endl << "Not Find !" << endl ;}

Delete(2,pHead);

pNode Pos ;

Pos = Find(3,pHead);

Insert(5, pHead, Pos);

TraverList(pHead);

/*

struct Node *p, *q;

q = create();

while(q){

cout << q -> Element;

p = q -> Next;

free(q);

q = p;

}

*/

return 0;

}

fff

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值