C++ 单链表操作

#include<iostream>
using namespace std;
struct Node
{
int Date;
Node * next;
};


class List
{
private:
Node * head,*bottom;    //一个头指针 一个尾指针 建表时使用
public:
List();                                       //无参构造函数,初始化
void Outputlist();
void HeadCreatelist(int x);                  //头插法建立单链表
void BottomCreatelist(int x);                //尾插法建立单链表
void InsertlistDate(int aDate,int bDate);    //在某个元素之后插入元素
void InsertlistPosition(int i, int Date);    //在某个位置i插入元素
void Deletelist(int i);                      //删除某个元素
};
List::List()
{
head = NULL;
bottom = NULL;
}
void List::HeadCreatelist(int x)
{
Node *s;
s = (Node*)new(Node);
s->Date = x;
if (head == NULL)
{
head = s;
s->next = NULL;
bottom = s;
}
else
{
s->next = head;
head = s;
}
}
void List::BottomCreatelist(int x)
{
Node *s=(Node *)new (Node);  //为s在栈里分配内存空间
//Node *s = new Node;        // 也可以new
//Node *s = new Node();      // 也可以new
s->Date = x;
if (head == NULL)
{
head = s;
s->next = NULL;
bottom = s;
}
else
{
bottom->next = s;
bottom = s;
s->next = NULL;  //该句不能省掉,指针
}
}
void List::InsertlistPosition(int i, int Date)
{
Node *p=head;
Node *s = new Node;
s->Date = Date;
s->next = NULL;
if (head == NULL)
{
head = s;
bottom = s;
}
else
{
for (int k = 1; k < i-1; k++)
{
if (p == NULL) break;
else
p = p->next;
}
if (p == NULL)
cout << "cuowu"<<endl;
else
{
s->next = p->next;
p->next = s;
}
}
}
void List::InsertlistDate(int aDate, int bDate)
{
Node *p=head;
Node *s = new Node;
s->Date = bDate;
s->next = NULL;
if (head == NULL)                //如果头节点是空
{
head = s;
bottom = s;
}
else
{
if (p->Date == aDate)       //如果第一个节点为所插入位置
{
s->next = head;
head = s;
p = head;
}
else 
{
while (p->Date != aDate&&p->next != NULL)
{
p = p->next;
}
if (p->Date == aDate)
{
s->next = p->next;
p->next = s;
}
else
{
p->next = s;
bottom = s;
}
}

}
}
void List::Deletelist(int i)
{
Node *p=head;
if (i == 1)
{
head = head->next;
}
else
{
for (int k = 1; k < i-1; k++)
{
if (p == NULL) break;
else
{
p = p->next;
}
}
p->next = p->next->next;
}


}
void List::Outputlist()
{
Node *p = head;
while (p != NULL)
{
cout << p->Date<<" ";
p = p->next;
}
cout << endl;
}




void main()
{
List A;
cout << "头插法插入7:";
A.HeadCreatelist(7);    
A.Outputlist();
cout << "头插法插入8:";
A.HeadCreatelist(8);    
A.Outputlist();
cout << "尾插法插入4:";
A.BottomCreatelist(4);  
A.Outputlist();
cout << "尾插法插入3:";
A.BottomCreatelist(3);  
A.Outputlist();
cout << "尾插法插入2:";
A.BottomCreatelist(2);
A.Outputlist();
cout << "头插法插入9:";
A.HeadCreatelist(9);
A.Outputlist();
cout << "尾插法插入1:";
A.BottomCreatelist(1);
A.Outputlist();
cout << "头插法插入10:";
A.HeadCreatelist(10);
A.Outputlist();
cout << "尾插法插入0:";
A.BottomCreatelist(0);
A.Outputlist();
cout << "建立的链表为:";
A.Outputlist();
A.InsertlistDate(7, 6);
cout << "在元素7后面面插入6" << endl;
A.Outputlist();
A.InsertlistPosition(6, 5);
cout << "在第6个位置插入元素5" << endl;
A.Outputlist();
cout << "删除第三个位置上的元素" << endl;
A.Deletelist(3);
A.Outputlist();
system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值