求问C语言的一个链表程序

求问C语言的一个链表程序,第1张

同学你好,我帮你运行了一下没有死掉,你的程序有问题,

这是我帮你写的新代码,我运行了是可以的,你看看,希望你能采纳,给个赞,有什么问题可以问我

# include <stdio.h>

# include <stdlib.h>

# include <malloc.h>

typedef struct node

{

int data

struct node *pNext

}NODE, *PNODE

PNODE Create_List(void)

void Traveser_List(PNODE)

bool Search_List(PNODE, int, int *)

int main()

{

int Loc

PNODE pHead

pHead = Create_List()

Traveser_List(pHead)

if(Search_List(pHead, 6, &Loc))//查找链表中数值为3的结点,并输出结点的位置

{

printf("此元素在链表中的位置是第%d个结点\n", Loc)

}

else

{

printf("此链表中没有您要找的元素!\n")

}

return 0}

PNODE Create_List(void)

{

int len

int val//存放创造结点的有效数值

printf("请输入链表长度len= ")

scanf("%d", &len)

PNODE pHead = (PNODE)malloc(sizeof(NODE))//创造头结点

if(pHead == NULL)

{

printf("动态内存分配失败!\n")

exit(-1)

}

PNODE Tail = pHeadTail->pNext = NULL//创造一个指针使之始终指向为为节点

for(int i = 0i<leni++)

{

printf("请输入第%d个节点的值:", i+1)

scanf("%d", &val)

PNODE pNew = (PNODE)malloc(sizeof(NODE))

if(pNew == NULL)

{

printf("动态内存分配失败!\n")

exit(-1)

}

pNew->data = val

Tail->pNext = pNew

pNew->pNext = NULL

Tail = pNew

}

return pHead

}

void Traveser_List(PNODE pHead)

{

PNODE p = pHead->pNext

while(p != NULL)

{

printf("%d ", p->data)

p = p->pNext

}

printf("\n")

}

bool Search_List(PNODE pHead, int i, int *Loc)

{

PNODE p = pHead->pNext

int count = 1

while(NULL != p &&p->data != i)

{

p = p->pNext

count++

}

if(p == NULL)return false

*Loc = count

return true

}

/************************结构体链表******************************************************/

struct

student

{

unsigned

int

stu_ID

char

sex

int

age

struct

student

*st

}

struct

student

*p,*h,*s

student*

crate_list(){//建立链表,返回链表的头结点

int

i=20

unsigned

short

c

p

=

new

student

h

=

p

p->stu_ID=1000

p->sex

=

'M'

p->age

=

i

do{

s=new

student

s->stu_ID=i+1000

s->sex

=

'M'

s->age

=

i

p->st

=

s

p

=

p->st

i++

}while(i<31)

p

=

NULL

return

h

}

bool

delete_node(student*

p,int

a){//传入头结点和年龄

if(p){

student

*

temp=p,*p_pre

while(temp->age!=a){

p_pre=temp

temp=temp->st

if(!temp)

return

false

}

p_pre->st=temp->st

delete

temp

temp=NULL

return

true

}

return

false

}

void

display_list(student

*

h){//打印链表中的数据

student*

temp=p

while(temp->st)

cout<<temp->stu_ID<<"

"<<temp->sex<<"

"<<temp->age<<endl

}

List是一个比较简单的单向链表类,拥有插入删除等基本功能。

Node是结点类,只在List中用到。

我加了比较详细的注释,相信你能看的懂:)

#include <iostream>

//结点类

class Node

{

//申明List为友元

friend class List

private:

//构造函数设为私有,只有友元可以调用

Node(int data = 0, Node* pNext = NULL) : m_data(data), m_pNext(pNext)

{

}

private:

int m_data //数据

Node* m_pNext//下一个结点的指针

}

class List

{

public:

//构造函数

List() : m_nCount(0), m_pHead(NULL)

{

}

//构造函数,初始化时就创建一个链表

//nCount 链表结点数

//InitData 初始数据

//Step 步长,每个结点的值会递增这个步长

List(int nCount, int InitData = 0, int Step = 0) : m_nCount(0), m_pHead(NULL)

{

for (int i = 0i <nCounti++)

{

Insert(i, InitData)

InitData += Step

}

}

//析构函数,析构时删除链表各结点,回收空间

~List()

{

DeleteAll()

}

//复制构造函数

List(const List&list) : m_nCount(0), m_pHead(NULL)

{

//使用=号 *** 作符函数

operator=(list)

}

//重载=号 *** 作

List&operator=(const List &list)

{

//如果是本身则直接返回

if (&list == this)

{

return *this

}

//先删除所有结点

DeleteAll()

//重新分配空间并赋值

if ( !list.IsEmpty() )

{

m_pHead = new Node(list.m_pHead->m_data, list.m_pHead->m_pNext)

m_nCount = list.m_nCount

Node *pSrcNode = list.m_pHead->m_pNext

Node *pDestNode = m_pHead

while (pSrcNode != NULL)

{

pDestNode->m_pNext = new Node(pSrcNode->m_data)

pDestNode = pDestNode->m_pNext

pSrcNode = pSrcNode->m_pNext

}

}

return *this

}

//是否为空,以结点数是否为0来判断

bool IsEmpty() const

{

return (m_nCount == 0)

}

//得到结点的数量

int GetCount() const

{

return m_nCount

}

//插入 *** 作

//index 要插入的位置,从0开始算起

//data 插入结点的值

//成功返回插入的位置,失败则返回-1

int Insert(int index, int data)

{

//计算正确的index

index = index >= 0 ? index : 0

index = index >m_nCount ? m_nCount : index

Node *pAdd = new Node(data)

//如果是插入到链表头

if (index == 0)

{

/*

* head

* |

* newnode node(0)->node(1)->...

*

* head

*|

* newnode->node0->node1->...

*

* head

* |

* newnode->node0->node1->...

*/

pAdd->m_pNext = m_pHead

m_pHead = pAdd

}

//如果是插入到链表的其他位置

else

{

/*

* newnode->NULL

*

* ...->node(i)->node(i+1)->...

*

* newnode

* \

* ...->node(i)->node(i+1)->...

*

* newnode

* /\

* ...->node(i) node(i+1)->...

*/

Node *pNode = m_pHead

for (int i = 0i <index - 1i++)

{

pNode = pNode->m_pNext

}

pAdd->m_pNext = pNode->m_pNext

pNode->m_pNext = pAdd

}

m_nCount++

return index

}

//删除 *** 作

//index 要删除的位置,从0开始算起

//成功返回删除的位置,失败则返回-1

int Delete(int index)

{

//为空不能进行删除 *** 作

if ( IsEmpty() )

{

return -1

}

//计算正确的index

index = index >= 0 ? index : 0

index = index >= m_nCount ? m_nCount - 1 : index

Node *pDelete = m_pHead

if (index == 0)

{

/*

* head

* |

* node(0)->node(1)->...

*

*head

* |

* node(0)->node(1)->...

*

*head

* |

* node(1)->...

*/

m_pHead = m_pHead->m_pNext

}

else

{

/*

* node(i)

* /\

* ...->node(i-1) node(i+1)->...

*

* node(i)

* \

* ...->node(i-1)->node(i+1)->...

*

*

* ...->node(i-1)->node(i+1)->...

*/

for (int i = 0i <index - 1i++)

{

pDelete = pDelete->m_pNext

}

Node *pTemp = pDelete

pDelete = pDelete->m_pNext

pTemp->m_pNext = pTemp->m_pNext->m_pNext

}

delete pDelete

m_nCount--

return index

}

//全部删除

void DeleteAll()

{

while (m_pHead != NULL)

{

Node *pTemp = m_pHead

m_pHead = m_pHead->m_pNext

delete pTemp

}

m_nCount = 0

}

//打印链表

void Print() const

{

Node *pNode = m_pHead

while (pNode != NULL)

{

std::cout<<"["<<pNode->m_data<<"]->"

pNode = pNode->m_pNext

}

std::cout<<"NULL"<<std::endl

}

private:

int m_nCount

Node* m_pHead

}

int main()

{

List list1(10, 0, 2)

List list2(list1)

list1.Delete(3)

list1.Print()

list2.Print()

List list3

list3 = list2

list3.Insert(0, -1)

list3.Print()

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12076248.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存