单链表的基本操作--c++

本文介绍了一个简单的单链表实现案例,包括单链表的创建、测长、打印、插入及删除等基本操作。
  1 #include <iostream>     //实现单链表的建立,测长和打印
  2 #include <string>
  3 using namespace std;
  4 struct node            // 定义节点类型,包含一个数据域和一个指针域
  5 {
  6     int data;
  7     node *next;
  8 };
  9 node * creat()
 10 {
 11     node *head,*p,*s;   // p指针为当前的指针,s为新建结点的指针
 12     int temp,cycle = 1;
 13     //head = (node*)malloc(sizeof(node));
 14     head = new node;
 15     p = head;
 16     while(cycle)
 17     {
 18         cout << "请输入int型数据" << endl;
 19         cin >> temp;
 20         if (temp != 0)             // 当temp不为零时,创建新的
 21         {
 22             //s = (node*)malloc(sizeof(node));
 23             s = new node;         //创建新的结点
 24             s -> data = temp;
 25             cout << "新节点为" << s -> data;
 26             p -> next = s;  // 使头结点的指针指向新生成的结点
 27             p = s;          //  使p指向当前新的结点
 28         }
 29         else
 30         {
 31             cout << "输入完成" <<endl;
 32             cycle = 0;
 33         }
 34     }
 35     head = head -> next;      //头指针
 36     p -> next = nullptr;     //置最后一个结点的指针为空
 37     return head;
 38 }
 39 // 单链表测长
 40 int length(node *head)
 41 {
 42     int  n = 0;
 43     node *p = head;
 44     while (p != nullptr )
 45     {
 46         p = p -> next;
 47         n++;
 48     }
 49     return n;
 50 }
 51 // 单链表打印
 52 void printlinklist(node * head)
 53 {
 54     node *p = head;
 55     while( p != nullptr)
 56     {
 57         cout << "data is" << p -> data << endl;
 58         p = p -> next;
 59     }
 60 }
 61 // 单链表插入
 62 node *insert(node * head, int num)
 63 {
 64     node *p0,*p1,*p2;
 65     p1 = head;
 66     p2 = new node;
 67     p0 = new node;// 插入结点
 68     p0 -> data = num;  // 插入数据
 69     // 若插入结点的数据大于原结点的数据,并且原结点的存在下一个元素
 70     while(p0 -> data > p1 -> data && p1 -> next != nullptr)
 71     {
 72         p2 = p1;
 73         p1 = p1 -> next;  // 这时  p2->p1->p0  ??
 74     }
 75     if (p0 -> data <= p1 -> data)
 76     {
 77         if ( p1 == head)
 78         {  //  头部前插入    p0和p1的位置是 P0->P1->
 79             head = p0;
 80             p0 -> next =p1;
 81         }
 82         else
 83         {  // 插入中间节点,   p2->p0->p1
 84             p2 -> next = p0;
 85             p0 -> next = p1;
 86         }
 87     }
 88     else
 89     {// 尾部插入结点,  p2->p1->p0->null
 90         p1 -> next = p0;
 91         p0 -> next = nullptr;
 92     }
 93     return head;
 94 }
 95 //删除单链表
 96 node *del(node *head, int num)
 97 {
 98     node *p1,*p2;
 99     p2 = new node;
100     p1 = head;
101     while(num != p1 -> data && p1 -> next != nullptr)
102     {
103         p2 = p1;
104         p1 = p1 -> next;   //  p2->p1
105     }
106     if (num == p1->data)
107     {
108         if (p1 == head)  // 删除头结点
109         {
110             head =p1 -> next;
111             delete p1;
112         }
113         else
114         {
115             p2 -> next = p1 -> next;
116             delete p1;
117         }
118     }
119     else
120     {
121         cout << "could not been found in current single linklist"<< endl;
122     }
123     return head;
124 }
125 int main()
126 {
127 
128     cout << "创建单链表" << endl;
129     node *head = creat();
130     cout << endl;
131 
132     cout << "计算链表的长度" << endl;
133     int n = length(head);
134     cout << "the length of the linklist is " << n << endl;
135     cout << endl;
136 
137     cout << "print the linklist" << endl;
138     printlinklist(head);
139     cout << endl;
140 
141     cout << "insert the node" << endl;
142     cout << "please insert the data";
143     int indata;
144     cin >> indata;
145     head = insert(head,indata);
146     printlinklist(head);
147     cout << endl;
148 
149     cout << "delete the node" << endl;
150     cout << "input the data for deleting ";
151     int deldata;
152     cin >> deldata;
153     head = del(head,deldata);
154     printlinklist(head);
155     cout <<endl;
156     return 0;
157 }

 

转载于:https://www.cnblogs.com/simplepaul/p/6728260.html

### 单链表算法实现通讯录管理系统设计方案 #### 1. 数据结构设计 为了有效地管理和操作联系人信息,可以采用单链表作为底层的数据存储结构。每个节点代表一位联系人的记录,包含姓名、电话号码和其他相关信息。 ```c++ struct Contact { string name; string phoneNumber; struct Contact* next; // 指向下一个结点的指针 }; ``` 此结构体`Contact`用于表示每一个联系人实体,并通过成员变量`next`链接到下一条记录形成链条[^1]。 #### 2. 基本功能模块 ##### 添加新联系人 当用户想要保存新的联系方式时,在列表头部或尾部插入一个新的节点即可完成这项工作。这里以头插法为例: ```cpp void add_contact(Contact*& head, const string& name, const string& phone){ Contact *newNode = new Contact(); newNode->name = name; newNode->phoneNumber = phone; newNode->next = head; head = newNode; } ``` 上述函数实现了创建并连接一个含有指定名字和手机号的新节点至现有链表前端的操作过程。 ##### 删除特定联系人 如果要移除某位已存在的朋友,则需遍历整个序列直到找到匹配项为止;之后调整前后两个相邻单元之间的关系从而达到目的。 ```cpp bool delete_contact_by_name(Contact*& head, const string& targetName){ if (!head || (head && head->name != targetName)) { Contact *prev = nullptr,*current = head; while(current && current->name!=targetName){ prev=current; current=current->next; } if(!current)return false;//not found if(prev==nullptr)//if the node to be deleted is at beginning of list. head=head->next; else{ prev->next=current->next; } free(current); }else{ // If linked list has only one element and that matches with key head=nullptr; } return true; } ``` 这段代码展示了如何定位待删对象以及更新周围环境来确保逻辑上的连续性。 ##### 查找联系人 提供按名称快速检索对应条目服务也是必不可少的一部分。这同样依赖于线性的扫描方式直至遇到目标位置停止。 ```cpp Contact* find_contact(const Contact* head, const string& searchName)const{ auto pCurrent = head; while(pCurrent&&pCurrent->name!=searchName)pCurrent=pCurrent->next; return pCurrent; } ``` 该片段说明了怎样沿着路径逐个检验各元素属性值是否相符进而返回符合条件的结果指向。 #### 3. 扩展特性支持 除了基本增删查改之外还可以考虑加入更多实用的功能比如分组管理、生日提醒等高级选项进一步提升用户体验感。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值