C结构体与链表
好久没有更新这一板块的东西了,今天就补回前面的一些部分给小伙伴们看。今天就先讲一下结构体和链表这个经典的东西。
**我前面的博客介绍过在STL中有双向链表,详见STL双向链表
**
总结一下就是任何地方删除插入都是常熟时间完成,不支持随机的存取
今天这里我们就来看看在C语言当中是如何自己实现的吧,至于链表的一些·特性可以看一下我上面的这个博客,内容大同小异
示意图如下
为了方便起见,我们把它封装为一个函数
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
struct list_node
{
int data;
struct list_node *next;//指针
};
/*也可以写成
typedef struct listpoint
{
int data;
listpoint *next;
}listpoint;
*/
typedef struct list_node list_single;//单向,我喜欢用一下别名,当然你用list_node也是没有问题的
list_single *create_list_node(int data)//封装为函数
{
list_single *node=NULL;//一开始的头指针肯定为NULL
node=(list_single *)malloc(sizeof(list_single));//分配空间
if(node==NULL)
{
cout<<"malloc fair"<<endl;
}
memset(node,0,sizeof(list_single));
node->data=data;
node->next=NULL;
return node;
}
int main()
{
int data = 1314 ;
list_single *node_ptr = create_list_node(data); //创建一个节点
cout<<"node_ptr->data="<<node_ptr->data<<endl; //打印节点里的数据
cout<<"node_ptr->next="<<node_ptr->next<<endl
;
free(node_ptr);
return 0 ;
}
下面代码演示作用并不大,主要还是自己多画图,注意几个问题就可以了
- 1.头指针,数据域,指针域
- 2.new出来的东西是否有delete,网上很多代码很多都存在这个问题
- 3.画图显示出你自己想要的功能就ok了
#include <string>
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
class List
{
public:
List();
~List();
Node * createList();
Node* tavalList();
Node* insertList(Node *data);
Node* deleteList();
int GetLen();
bool IsEmply();
private:
Node *head;
int size;
};
List::List()//初始化
{
head=new Node;
head->data=0;
head->next=NULL;
size=0;
}
List::~List()//销毁
{
delete head;
}
Node * List::createList()
{ Node * s, * p ;
s = new Node ;
cin >> s->data;
while ( s->data != 0 )
{
size++;
p->next = s ;
p = s ;
s = new Node ;
cin >> s->data ;
}
p -> next = NULL ;
delete s ;
return head ;
}
Node* List::tavalList()//遍历
{
Node *ptemp = head->next;
if (head == NULL) {
cout << "Empty List" << endl;
}
while(ptemp)
{
cout << ptemp->data << "->";
ptemp = ptemp->next;
}
cout <<"NULL"<< endl;
return head;
}
Node* List::insertList(Node *data)
{
Node *ptemp;
if (head == NULL) {
cout << "Empty List" << endl;
return head;
}
if (data == NULL) {
cout << "Empty Node" << endl;
return head;
}
int n;
cout<<"the location of the insertion:";
cin>>n;
//头插
if (n<2) {
size++;
data->next=head;
head=data;
return 0;
}
//尾插
if (n > size) {
ptemp=head;
for(int i=0;i<size+1;i++)
ptemp=ptemp->next;
data->next=NULL;
ptemp->next=data;
size++;
}
//中间插
else {
ptemp = head;
for (int i = 1; i < n; i++) {
ptemp = ptemp->next;
}
Node *p=ptemp->next;
ptemp->next=data;
data->next=p;
size++;
}
return head;
}
Node* List::deleteList()
{
cout<<"Which one do you want to delete:"<<endl;
int n;
cin>>n;
Node *ptemp;
Node *ptemp2;
if (n > this->size) {
cout << "ERROR,n is too big!" << endl;
return head;
}
//删头节点
if (n < 2) {
ptemp = this->head->next;
this->head->next = ptemp->next;
free(ptemp);
this->size--;
return head;
}
//尾部删除
if (n == this->size) {
ptemp = this->head;
for (int i = 1; i < this->size;i++) {
ptemp = ptemp->next;
}
ptemp2 = ptemp->next;
ptemp->next = NULL;
free(ptemp2);
this->size--;
return 0;
}
//中间删除
else
{
ptemp = this->head;
for (int i = 1; i < n; i++) {
ptemp = ptemp->next;
}
ptemp2 = ptemp->next;
ptemp->next = ptemp2->next;
free(ptemp2);
this->size--;
return 0;
}
}
int List::GetLen()
{
return this->size;
}
bool List::IsEmply()
{
if (this->head == NULL) {
return true;
}
else{
return false;
}
}
学会程序和算法,走遍天下都不怕
冰岛瓦特纳冰川