建立一个单链表,显示链表中每个结点的数据

要用C++编译出来的啦...各位大虾帮帮忙啊..
2024-11-22 17:32:35
推荐回答(2个)
回答1:

//单链表的动态创建,查找,遍历,删除,插入,添加,排序
#include
typedef struct node //定义一个结构体,在c++中也是一个类
{
int val;
struct node* pNext;
}NODE,*PNODE;
class MyList //创建一个类
{
private:
PNODE pHead;//struct node* pHead
public:
MyList() //构造函数,构造一个空链表头指针
{
this->pHead = NULL;
}
~MyList() {
while(this->pHead!=NULL)
{
PNODE pTemp = pHead->pNext;
delete pHead;
pHead = pTemp;
}
}
void Init() //初始化链表
{
int a;
char ans;

PNODE pTail,pNew;
do
{
cout<<"请输入一个节点值:";
cin>>a;
if(pHead == NULL)
{
pHead = new NODE;
pHead->val = a;
pHead->pNext = NULL;
pTail = pHead;
}
else
{
pTail = pHead;
while(pTail->pNext!=NULL)//把pTail移动到尾部
{
pTail = pTail->pNext;
}

pNew = new NODE;//new一个新的接点来接受新输入的值
pNew->val = a;
pNew->pNext = NULL;

pTail->pNext = pNew;
pTail = pTail->pNext;
}

cout<<"继续吗?(Y/N): ";
cin>>ans;

}while(ans=='Y'||ans=='y');
}

void Add(int val) //向链表中追加值方法
{
if(pHead == NULL)
{
pHead = new NODE;
pHead->val = val;
pHead->pNext = NULL;
}
else
{
PNODE pTemp = pHead;
while(pTemp->pNext!=NULL)
{
pTemp = pTemp->pNext;
}

PNODE pNew = new NODE;
pNew->val = val;
pNew->pNext = NULL;

pTemp->pNext = pNew;
}
}
int InsertAt(int val,int k)//向链表中插入元素,约定在k之前插入
{
PNODE p1,p2,pNew,pTemp;

if(pHead == NULL)//链表为空
{
return -1;
}

if(k<0 || k>this->GetNodeCnt()-1)//k越界
{
return -1;
}

if(k==0)//在头节点之前插入
{
pTemp = pHead;

pNew = new NODE;
pNew->val = val;
pNew->pNext = NULL;

pHead = pNew;

pNew->pNext = pTemp;

return 0;
}

p1 = pHead;
int i =0;
while(i {
p1 = p1->pNext;
i++;
}

p2 = p1->pNext;

pNew = new NODE;
pNew->val = val;
pNew->pNext = NULL;

p1->pNext = pNew;
pNew->pNext = p2;

return 0;

}

int Find(int val) //按输入的值查找方法
{
int i=0;
PNODE pTemp = pHead;
while(pTemp != NULL)
{
if(pTemp->val == val)
{
return i;
}
pTemp = pTemp->pNext;
i++;
}
return -1;
}

void Travel() //遍历单链表中的元素
{
PNODE pTemp = this->pHead;

while(pTemp!=NULL)
{
cout<val<<" ";
pTemp = pTemp->pNext;
}
cout< }

int GetNodeCnt() //获取单链表中元素的个数
{
int cnt=0;
PNODE pTemp = pHead;

while(pTemp!=NULL)
{
cnt++;
pTemp = pTemp->pNext;
}

return cnt;
}

void sort() //排序
{
int n = this->GetNodeCnt();
PNODE p1,p2;
for(int i=0;i {
p1 = pHead;
for(int j=0;j {
p2 = p1->pNext;
if(p1->val < p2->val)
{
int k = p1->val;
p1->val = p2->val;
p2->val = k;
}
p1 = p1->pNext;
}
}
}
};
void main()
{
MyList list;
for(int i=0;i<10;i++)
{
list.Add(i);
}
list.Travel(); //遍历单链表中的元素 并输出
list.sort();
list.Travel(); //排序后遍历链表中的元素 再次输出
}

回答2:

#include
using namespace std;

typedef int Status;
typedef int ElemType;
#define OK 1
#define ERROR 0

typedef struct LNode{//define of LNode
ElemType data;
struct LNode *next;
}Lnode,*LinkList;

Status CreateList(LinkList &L,int n)//建立单链表
{
cout<<"要建立链表的数据个数:";
cin>>n;
L=new LNode();
L->next=NULL;
LinkList p;
for(int i=n;i>=1;i--)
{
p=new LNode();
cin>>p->data;
p->next=L->next;
L->next=p;
}
cout<<"建立单链表成功!"<
return OK;
}

Status PrintList(LinkList &L)//遍历单链表的数据元素
{
LinkList p;
p=L;
while(p!=NULL)
{
p=p->next;
cout }
cout<
return OK;
}
void showmenu()
{
cout<<
cout<<" *1: 初始化并建立单链表 *"<
cout<<" *2: 遍历链表数据 *"<
cout<<" *3: 查找数据 *"<
cout<<" *4: 插入数据 *"<
cout<<" *5: 删除数据 *"<
cout<<" *5: *"<
cout<<" *9: 离开 *"<
cout<<" *请选择....";
}

void main()//主函数
{
int select,number;
LinkList L;
while(select!=9)
{
showmenu();
cin>>select;
switch(select)
{
case 1:
CreateList(L,number);
break;
case 2:
PrintList(L);
break;
case 9:
default:
cout<<"输入错误,请重新输入!"<
break;
}
}
}
可能根据编译器不同,有所差异,你可以再调试下。