C++ 单链表的实现(一)

//网上关于这方面的挺多,由于自己以前上课没好好学数据结构,现在重新认识数据结构,
//以下是自己写的基于C++的单链表的创建及其一些操作,与实现,没用模板,所以适合
//有一点C++基础的人,但可移植不够.有什么bug的话,欢迎指出。或有什么问题也可以联系我
//                                made by virgil (2009.2.8)
//                                MSN:hangyu_628@hotmail.com)
#include <iostream>
using namespace std;
int N=10;

typedef struct Node
{
 char name[20];
 struct Node *next;
}Node;

Node *create(int n)
{
 Node *h,*p,*s;
 if ((h=new Node)==NULL)
 {
  cout<<"分配内存失败"<<endl;
  exit(1);
 }
 h->name[0]='/0';
 h->next=NULL;
 p=h;
 for (int i=0;i!=n;++i)
 {
  if((s=new Node)==NULL)
  {
   cout<<"分配内存失败"<<endl;
   exit(1);
  }
  p->next=s;
  cout<<"请输入第"<<i+1<<"个人的姓名:";
  cin>>s->name;
  s->next=NULL;
  p=s;
 }
 return h;
}

//查询结点
Node *search(const Node *h, const char *name)
{
 Node *p=h->next;
 for (int i=0;i!=N;++i)
 {
  if (strcmp(name,p->name)==0)
  {
   return p;
  }
  p=p->next;
 }
 return p;
}

//后插入
void Insert(Node *p)
{
 Node *s;
 if ((s=new Node)==NULL)
 {
  cout<<"分配内存失败"<<endl;
  exit(1);
 }
 cout<<"请输入你要插入的姓名:";
 cin>>s->name;
 s->next=p->next;
 p->next=s;
 ++N;
}
//查询前向结点
Node* searchPre(Node *head, const char *name)
{
 Node *p=head->next;
 Node *q=p->next;
 for (int i=0;i!=N;++i)
 {
  if (q!=NULL)
  {
   if (strcmp(q->name,name)==0)
   {
    return p;
   }
   else
   {
    p=p->next;
    q=p->next;
   }
  }
 }
 return p;
}
//删除结点
void Delete(Node *p, Node *q)  //p为删除的结点指针,q为前一个结点指针 
{
 q->next=p->next;
 delete p;
 --N;
};

//显示并释放内存空间...
void display(Node *h)
{
 Node *p;
 p=h->next;
 for (int i=0;i!=N;++i)
 {
  cout<<"第"<<i+1<<"个人的姓名:"<<p->name<<endl;
  delete p;
  p=p->next;
 }
  delete h;
}

int main()
{
 Node *head,*p=0,*q=0;
 char str[20];
 cout<<"创建结点数据如下:"<<endl;
 head=create(N);
 cout<<endl;
 //display(head);
 cout<<"所要查询的查询结点是: ";
 cin>>str; 
 //查询当前结点并插入于后
 p=search(head,str);
 // cout<<"插入结点中...";
 // Insert(p); 
 //查询前驱结点并删除当前结点.
 q=searchPre(head,str);
// Insert(q);
 Delete(p,q);
 //  cout<<"插入完的结点数据如下:"<<endl;
 display(head);

 return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供一个基本的单链表通讯录的实现。首先,我们需要定义一个通讯录的节点结构体,包含姓名、电话号码、邮箱等信息: ```c++ struct Contact { std::string name; std::string phone; std::string email; Contact* next; }; ``` 其中,`next`指向下一个节点。接下来,我们需要定义一个用于管理通讯录的类 `ContactList`,其中包括添加、删除、查找联系人等方法: ```c++ class ContactList { public: ContactList() { head = nullptr; } ~ContactList() { Contact* current = head; while (current != nullptr) { Contact* next = current->next; delete current; current = next; } } void addContact(const std::string& name, const std::string& phone, const std::string& email) { Contact* newContact = new Contact(); newContact->name = name; newContact->phone = phone; newContact->email = email; newContact->next = head; head = newContact; } void removeContact(const std::string& name) { Contact* current = head; Contact* previous = nullptr; while (current != nullptr) { if (current->name == name) { if (previous == nullptr) { head = current->next; } else { previous->next = current->next; } delete current; return; } previous = current; current = current->next; } } Contact* findContact(const std::string& name) { Contact* current = head; while (current != nullptr) { if (current->name == name) { return current; } current = current->next; } return nullptr; } private: Contact* head; }; ``` 上述代码中,`addContact`方法在链表头部插入一个新的节点,`removeContact`方法删除指定姓名的节点,`findContact`方法查找指定姓名的节点。 使用示例: ```c++ ContactList myContacts; myContacts.addContact("Alice", "123456", "[email protected]"); myContacts.addContact("Bob", "789012", "[email protected]"); myContacts.addContact("Charlie", "345678", "ch[email protected]"); Contact* contact = myContacts.findContact("Bob"); if (contact != nullptr) { std::cout << "Phone: " << contact->phone << std::endl; } myContacts.removeContact("Charlie"); ``` 以上是一个基本的单链表通讯录实现,您可以根据需要进行扩展和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值