采用表尾插入元素创建一个单链表。
发布网友
发布时间:2024-05-14 12:23
我来回答
共3个回答
热心网友
时间:2024-05-31 13:25
#include<iostream>
using namespace std;
template <typename T>class List;
template <typename T>
class Node
{
friend class List<T>;
private:
T data;
Node<T>* next;
public:
Node(){data=NULL;next=NULL;}
Node(T& da) {data =da;next=NULL;}
Node(T& da,Node<T>* ne) {data=da;next = ne};
};
template <typename T>
class List
{
private:
int length;
Node<T>* head;//头结点,其next节点为第一个节点
Node<T>* last;//指向最后一个结点的指针
public:
List();
void insert (T& data);
void remove (T data);
int getLength() const{return length;};
void print () const;
void reverse();
};
template <typename T>
List<T>::List()
{
length = 0;
head = new Node<T>();
last = head;
}
template <typename T>
void List<T>::insert(T &data)
{
last = last ->next =new Node<T>(data);
length++;
}
template <typename T>
void List<T>::remove(T data)
{
Node<T>* current = head;
while (current->next!=NULL)
{
if(current->next->data==data)
{
Node<T>* temp = current->next;
current->next = current->next->next;
delete temp;
length--;
}
else
current = current->next;
}
}
template <typename T>
void List<T>::print() const
{
Node<T>* current =head;
while (current->next!=NULL)
{
current = current->next;
cout<<current->data<<" ";
}
}
template <typename T>
void List<T>::reverse()
{
Node<T>* temp1;//利用三个临时指针降List中每个结点的next倒向
Node<T>* temp2;
Node<T>* temp3;
last = head->next;
temp1 = head;
temp2 = temp1->next;
while(temp2!=NULL)
{
temp3 = temp2->next;
temp2->next = temp1;
temp1 = temp2;
temp2 = temp3;
}//倒向完毕
last->next = NULL;//重新设置head和last
head->next = temp1;
}
int main ()//测试代码
{
List<int> one;
int i1=34,i2=89,i3=232;
one.insert(i1);
one.insert(i1);
one.insert(i1);
one.insert(i1);
one.insert(i2);
one.insert(i3);
one.print();
cout<<endl;
one.reverse();
one.print();
cout<<endl;
one.remove(34);
one.print();
}
我用的是vc2008,应该没问题,都按照要求了
热心网友
时间:2024-05-31 13:24
第一个问题:
#include "stdio.h"
typedef struct lnode
{
int data;
struct lnode *next;
}lnode,*link;
lnode *creat()用尾插入元素创建一个单链表函数;
{
link La,p,q;int i,n;
La=(link)malloc(sizeof(lnode));创建头结点
La->next=NULL;
q=La;
printf("input the number of element n:\n");
scanf("%d",&n);输入创建元素的个数n
for(i=1;i<=n;++i)
{
p=(link)malloc(sizeof(lnode));生成结点并用p指向
printf("input the value of %dth:\n",i);
scanf("%d",&p->data);输入元素的值
printf("\n");
q->next=p;q=p;
}
q->next=NULL;将最后一个结点的next指向空
return (La);返回头结点的指针
}
void out(lnode *La)完成链表的输出函数
{
link p;
p=La->next;
printf("success output the every element:\n");
while(p)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
}
void main()
{
lnode *Lc,*Ld;
Lc=creat();
out(Lc);
}
我用的版本stdio里面包函了malloc函数,如果你用的没有的话,需在最前面引用到源程序文件中去;程序的功能是尾插入法创建一个大小由你输入的n的大小决定的一个带头结点的单链表;运行的时候,先提示你输入n,(n就是要创建的元素个数),然后提示你输入每个元素(由于为了方便,定义元素为int型,当你输入n个数后;则显示创建结果;
第二个问题:
#include "stdio.h"
typedef struct lnode
{
int data;
struct lnode *next;
}lnode,*link;
lnode *creat()
{
link La,p,q;int i,n;
La=(link)malloc(sizeof(lnode));
La->next=NULL;
q=La;
printf("input the number of element n:\n");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
p=(link)malloc(sizeof(lnode));
printf("input the value of %dth:\n",i);
scanf("%d",&p->data);
printf("\n");
q->next=p;q=p;
}
q->next=NULL;
return (La);
}
void out(lnode *La)
{
link p;
p=La->next;
printf("success output the every element:\n");
while(p)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
}
lnode *lizhi(lnode *Lb)与上一个程序不同之处是多了这个函数,实现单链表
{ lnode *p,*s;的就地逆置;
p=Lb->next;
Lb->next=NULL;
s=p->next;
while(p)
{
p->next=Lb->next;
Lb->next=p;
p=p->next;
p=s;
s=s->next;
}
return (Lb);
}
void main()
{
lnode *Lc,*Ld;
Lc=creat();
out(Lc);
Ld=lizhi(Lc);
out(Ld);
}
与上一个程序差不多,唯一不同的是多了一个逆置函数;
以上两个程序都可以成功运行的,你拿去试试吧,祝你成功。
热心网友
时间:2024-05-31 13:27
#include<iostream>
using
namespace
std;
template
<typename
T>class
List;
template
<typename
T>
class
Node
{
friend
class
List<T>;
private:
T
data;
Node<T>*
next;
public:
Node(){data=NULL;next=NULL;}
Node(T&
da)
{data
=da;next=NULL;}
Node(T&
da,Node<T>*
ne)
{data=da;next
=
ne};
};
template
<typename
T>
class
List
{
private:
int
length;
Node<T>*
head;//头结点,其next节点为第一个节点
Node<T>*
last;//指向最后一个结点的指针
public:
List();
void
insert
(T&
data);
void
remove
(T
data);
int
getLength()
const{return
length;};
void
print
()
const;
void
reverse();
};
template
<typename
T>
List<T>::List()
{
length
=
0;
head
=
new
Node<T>();
last
=
head;
}
template
<typename
T>
void
List<T>::insert(T
&data)
{
last
=
last
->next
=new
Node<T>(data);
length++;
}
template
<typename
T>
void
List<T>::remove(T
data)
{
Node<T>*
current
=
head;
while
(current->next!=NULL)
{
if(current->next->data==data)
{
Node<T>*
temp
=
current->next;
current->next
=
current->next->next;
delete
temp;
length--;
}
else
current
=
current->next;
}
}
template
<typename
T>
void
List<T>::print()
const
{
Node<T>*
current
=head;
while
(current->next!=NULL)
{
current
=
current->next;
cout<<current->data<<"
";
}
}
template
<typename
T>
void
List<T>::reverse()
{
Node<T>*
temp1;//利用三个临时指针降List中每个结点的next倒向
Node<T>*
temp2;
Node<T>*
temp3;
last
=
head->next;
temp1
=
head;
temp2
=
temp1->next;
while(temp2!=NULL)
{
temp3
=
temp2->next;
temp2->next
=
temp1;
temp1
=
temp2;
temp2
=
temp3;
}//倒向完毕
last->next
=
NULL;//重新设置head和last
head->next
=
temp1;
}
int
main
()//测试代码
{
List<int>
one;
int
i1=34,i2=89,i3=232;
one.insert(i1);
one.insert(i1);
one.insert(i1);
one.insert(i1);
one.insert(i2);
one.insert(i3);
one.print();
cout<<endl;
one.reverse();
one.print();
cout<<endl;
one.remove(34);
one.print();
}
我用的是vc2008,应该没问题,都按照要求了