数据结构与算法:链表(源码)!

简介:

今天抽了点时间,复习了一下链表,总结了建立,删除,插入,查找的操作方法。

源码如下:

 

复制代码
#include < iostream >
using   namespace  std;

typedef 
struct  LNode 

int  data; 
struct  LNode  * next; 
}LNode,
* Llist; 

// 方法声明
LNode  * creat_head(); // 创建一个空表 
void  creat_list(LNode  * , int ); // 创建一个长度为n的线性链表 
void  insert_list(LNode  * , int , int  ); // 插入一个元素 
int  delete_list(LNode  * , int ); // 删除一个元素 

// 创建一个空链表 
LNode  * creat_head()

LNode 
* p; 

p
= (Llist)malloc( sizeof (LNode)); 

p
-> next = NULL; 

return (p); 


// 创建一个长度为n的线性链表 
void  creat_list(LNode  * head, int  n) 

LNode 
* p, * q;
int  i;
p
= head; 
for (i = 1 ;i <= n;i ++

q
= (Llist)malloc( sizeof (LNode)); 
cout
<< " data: " ;
cin
>> q -> data; 
q
-> next = NULL; 
p
-> next = q; 
= q; 



// 插入一个元素 
void  insert_list(LNode  * head, int  x, int  i ) 

int  j = 0 ;
LNode 
* p, * s; 
p
= head; 
while ((p != NULL) && (j < i - 1 )) 

p
= p -> next; 
j
++

if (p == NULL) 
exit(
0 ); 
s
= (Llist)malloc( sizeof (LNode)); 
s
-> data = x; 
s
-> next = p -> next; 
p
-> next = s; 
}

// 删除一个元素 
int  delete_list(LNode  * head, int  i) 

LNode 
* p, * q; 
int  j = 0
int  x; 
p
= head; 
while ((p != NULL) && (j < i - 1 )) 

p
= p -> next; 
j
++

if (p == NULL) 
exit(
0 ); 
q
= p -> next; 
p
-> next = q -> next; 
x
= q -> data; 
delete(q); 
return (x); 

// 输出
void  Print(LNode  * head,LNode  * p){
     
for (p = head -> next;p != NULL;) 
     { 
     cout
<< p -> data << endl; 
     p
= p -> next;
     } 
     } 
// 按序号查找
int  Find(LNode  * head,LNode  * p, int  i){
    
int  j = 0 ;
    
int  k;
    
for (p = head -> next;p != NULL;){
    j
++ ;
    
if (i == j)
    k
= p -> data;
    p
= p -> next;
    }
    
return  k;
}
// 主函数 
int  main() 

LNode 
* head, * p; 
int  find; 
int  n; 
int  x,i; 
int  b; 
int  clrscr(); 
head
= creat_head(); 
cout
<< " 请输入链表长: " << endl; 
cout
<< " n= " ;
cin
>> n; 
cout
<< " 请输入数值: " << endl; 
creat_list(head,n);
cout
<< " 您输入的链表为: " << endl; 
Print(head,p);
cout
<< " \n请输入您要插入的数:\n "
cout
<< " x= " ;
cin
>> x; 
cout
<< " \n请输入您要插入的位置:\n "
cout
<< " i= " ;
cin
>> i; 
insert_list(head,x,i); 
cout
<< " 您输入的链表为: " << endl; 
Print(head,p);
cout
<< " \n请输入您要删除的位置:\n "
cout
<< " i= " ;
cin
>> i; 
b
= delete_list(head,i);
cout
<< " 删除后的链表为: " << endl; 
Print(head,p);
cout
<< " 请输入您要查找的位置: " << endl;
cin
>> find;
cout
<< Find(head,p,find) << endl;
cout
<< " 请输入您要查找的位置: " << endl;
cin
>> find;
cout
<< Find(head,p,find) << endl;
cout
<< " 请输入您要查找的位置: " << endl;
cin
>> find;
cout
<< Find(head,p,find) << endl;
return   0 ;

复制代码

 本文转自施杨博客园博客,原文链接:http://www.cnblogs.com/shiyangxt/archive/2008/12/01/1345303.html,如需转载请自行联系原作者

相关文章
|
10天前
|
算法
【❤️算法笔记❤️】-每日一刷-19、删除链表的倒数第 N个结点
【❤️算法笔记❤️】-每日一刷-19、删除链表的倒数第 N个结点
39 1
|
10天前
|
算法 索引
❤️算法笔记❤️-(每日一刷-141、环形链表)
❤️算法笔记❤️-(每日一刷-141、环形链表)
23 0
|
10天前
|
算法
【❤️算法笔记❤️】-(每日一刷-876、单链表的中点)
【❤️算法笔记❤️】-(每日一刷-876、单链表的中点)
29 0
|
8天前
|
Java C++ 索引
让星星⭐月亮告诉你,LinkedList和ArrayList底层数据结构及方法源码说明
`LinkedList` 和 `ArrayList` 是 Java 中两种常见的列表实现。`LinkedList` 基于双向链表,适合频繁的插入和删除操作,但按索引访问元素效率较低。`ArrayList` 基于动态数组,支持快速随机访问,但在中间位置插入或删除元素时性能较差。两者均实现了 `List` 接口,`LinkedList` 还额外实现了 `Deque` 接口,提供了更多队列操作。
13 3
|
9天前
|
存储 缓存 算法
经典算法之链表篇(三)
经典算法之链表篇(三)
|
9天前
|
算法
经典算法之链表篇(二)
经典算法之链表篇(二)
|
9天前
|
算法 索引
经典算法之链表篇
经典算法之链表篇
|
8天前
|
存储 Java
HashMap之链表转红黑树(树化 )-treefyBin方法源码解读(所有涉及到的方法均有详细解读,欢迎指正)
本文详细解析了Java HashMap中链表转红黑树的机制,包括树化条件(链表长度达8且数组长度≥64)及转换流程,确保高效处理大量数据。
24 1
|
10天前
|
算法
❤️算法笔记❤️-(每日一刷-160、相交链表)
❤️算法笔记❤️-(每日一刷-160、相交链表)
12 1
|
5天前
|
存储
[数据结构] -- 双向循环链表
[数据结构] -- 双向循环链表
12 0