学习笔记---通过c++完成一个单链表-创新互联

#include

创新互联成立与2013年,先为筠连等服务建站,筠连等地企业,进行企业商务咨询服务。为筠连企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

using namespace std;

typedef int data_t;

class LinkList

{

public:

  LinkList();

  void init(int n);

  void show() const;

  int getlen() const;

  void isempty() const;

  int find_by_pos(int pos) const;

  int find_by_data(data_t data) const;

  int insert_by_pos(int pos, data_t data) const;

  void delete_by_pos(int pos) const;

private:

  class Node

  {

  public:

  data_t data;

  Node *next;

  };

  Node *head;

  int len=0;

};

LinkList ::LinkList()

{

  head= new Node;

  head->data=0;

  head->next=NULL;

}

int LinkList ::getlen() const

{

  cout<<"len = "<

  return len;

}

void LinkList :: show() const

{

  Node *p=head->next;

  if(!len)

  {

  cout<<"list is empty";

  return ;

  }

  cout<<"list have element:"<

  while(p)

  {

  cout<data<<" ";

  p=p->next;

  }

  cout<

}

void LinkList::init(int n) {

  Node *pre = head;

  len = n;

  int i = 1;

  while (i<= n) {

  cout<< "请输入第"<< i<< "个结点的数据:";

  Node *cur = new Node;

  cin >>cur->data;

  ++i;

  pre->next = cur;

  pre = cur;

  cur->next = nullptr;

  }

}

void LinkList::isempty()const

{

  if(!len)

  cout<<"list is empty"<

  return ;

}

int LinkList::find_by_pos(int pos) const {

  if (pos< 0 || pos>= len)

  cout<< "pos is error"<< endl;

  else {

  Node *temp = head->next;

  int i = 0;

  while (temp) {

  if (i == pos) {

  cout<< "pos ="<< pos<< "data= "<< temp->data<< endl;

  return temp->data;

  } else {

  ++i;

  temp = temp->next;

  }

  }

  }

  return -1;

}

int LinkList ::find_by_data(data_t data) const

{

  Node *p=head->next;

  int pos=0;

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

  p = p->next;

  pos++;

  }

  return pos;

}

int LinkList :: insert_by_pos(int pos, data_t data) const

{

  if (pos< 0 || pos >= len) {

  cout<< "pos is error"<< endl;

  } else {

  Node *p = head;

 while(pos--)

  p = p->next;

  Node*temp = new Node;

  if(temp == NULL)

  return -1;

  temp->data = data;

  temp->next = p->next;

  p->next = temp;

  cout<< data<< "insert success"<< endl;

  return 0;

  }

}

void LinkList :: delete_by_pos(int pos) const

{

  if (pos< 0 || pos >= len) {

  cout<< "pos is error"<< endl;

  } else {

  Node *p = head;

while(pos != 0){

  pos = pos - 1;

  p = p->next;

  }

  Node *q = p->next;

  p->next = q->next;

  free(q);

 

  cout<< "delete success"<< endl;

  }

}

int main(void)

{

LinkList linkList;

  cout<< "请输入链表中结点的个数:";

  int n;

  cin >>n;

  linkList.init(n);

  linkList.show();

  linkList.getlen();

  linkList.isempty();

  cout<< "请输入需要查询的索引位置:";

  int index;

  cin >>index;

  linkList.find_by_pos(index);

  cout<< "请输入需要查询的数据:";

  int elem;

  cin >>elem;

  if (linkList.find_by_data(elem) != -1) {

  cout<< elem<< "在链表中的索引位置是"<< linkList.find_by_data(elem)<< endl;

  }

  cout<< "请输入需要插入结点的位置索引和数值:";

  int pos, data;

  cin >>pos >>data;

  linkList.insert_by_pos(pos, data);

  linkList.show();

  linkList.getlen();

  cout<< "请输入需要删除的结点索引:";

  int deleteIndex;

  cin >>deleteIndex;

  linkList.delete_by_pos(deleteIndex);

  linkList.show();

  linkList.getlen();

  return 0;

}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


新闻标题:学习笔记---通过c++完成一个单链表-创新互联
地址分享:http://ybzwz.com/article/cdieph.html