单链表的操作
来源:互联网 发布:dnf一上线就网络中断 编辑:程序博客网 时间:2024/06/09 19:26
#include<stdio.h>
#include<stdlib.h>
typedef struct ListNode{
int value;
ListNode* pNext;
}Node;
Node* head=NULL;
//创建
bool create_node(){
//动态分配内存
head=(Node*)malloc(sizeof(Node));
//判断head是否为空
if(head==NULL){
return false;
}
else{
head->value=0;
head->pNext=NULL;
return true;
}
}
//添加
bool add_node(Node* node){
if(head==NULL){
return false;
}
Node* q=head;
Node* p=head->pNext;
while(p!=NULL){
q=p;
p=p->pNext;
}
q->pNext=node;
node->pNext=NULL;
return true;
}
//删除节点
bool dele_node(int index){
if(head==NULL){
return false;
}
int length=0;
Node* p=head;
while(p->pNext!=NULL){
length++;
p=p->pNext;
}
if(index>length){
return false;
}else{
Node* q=head;
p=q;
for(int i=0;i<index;i++){
q=p;
p=p->pNext;
}
Node* t=p->pNext;
q->pNext=t;
free(p);
return true;
}
}
//逆序
void reverseNodeList()
{
if(NULL == head)
{
return;
}
//如果链表长度为1
if(head->pNext == NULL)
{
return;
}
Node* current=head->pNext;
Node* p=head;
Node* q=head;
while(current->pNext!=NULL){
p=current->pNext;
current->pNext=p->pNext;
p->pNext=head->pNext;
head->pNext=p;
}
// Node* p = head->pNext;
// Node* q = p->pNext;
// Node* t = NULL;
// while(NULL != q)
// {
// t = q->pNext;
// q->pNext = p;
// p = q;
// q = t;
// }
// //p->pNext= NULL;
// head->pNext->pNext = NULL;
// head->pNext = p;
}
void Print_list(){
Node* p=head;
while(p->pNext!=NULL){
p=p->pNext;
printf("%-2d",p->value);
}
}
int main(){
create_node();
for(int i=1;i<=10;i++){
Node* node_i=(Node*)malloc(sizeof(Node));
node_i->value=i;
node_i->pNext=NULL;
add_node(node_i);
}
reverseNodeList();
//dele_node(1);
//dele_node(2);
Print_list();
return 0;
}
- 简单的单链表操作
- 单链表的基本操作
- 简单的单链表操作
- 单链表的基本操作
- 单链表的操作(C#)
- 单链表的操作
- 单链表的各种操作
- 单链表的操作实现
- 单链表的 操作
- 单链表的基本操作
- 单链表的一些操作
- 单链表的操作
- 关于单链表的操作
- 单链表的各种操作
- 单链表的常用操作
- 单链表的一些操作
- 单链表的一些操作
- 单链表的操作
- 主机虚拟机和开发板ping不通的问题
- jQuery攻略
- ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
- 欢迎使用CSDN-markdown编辑器
- PID代码及注释
- 单链表的操作
- 设计模式之责任链模式
- 定位多线程内存越界问题实践总结
- Scala的react、loop代码编程
- poj2590steps数列递推
- OPENCV下SIFT算法使用方法笔记
- nginx access.log 忽略favicon.ico访问记录的方法
- 理想倍频器/分频器对相噪/杂散的影响
- 指向指针的指针的一种用法