自定义实现的带头结点的链表
来源:互联网 发布:国金证券交易软件 编辑:程序博客网 时间:2024/06/11 12:09
//链表实现
#include <stdio.h>
#include <stdlib.h>
//链表结构体
typedef int datatype;
typedef struct NODE
{
datatype data;
struct NODE *next;
}Node;
typedef struct LIST
{
Node *phead;
int length;
}List;
//创建空链表
List *CreateList()
{
Node *phead=(Node *)malloc(sizeof(Node));
phead->next=NULL;
if(NULL==phead)
{
return NULL;
}
List *list=(List *)malloc(sizeof(List));
if(NULL==list)
{
return NULL;
}
list->phead=phead;
list->length=0;
return list;
}
//获取链表的长度
int GetLength(List *pList)
{
return list->length;
}
bool DeleteNewNode(Node *newnode)
{
if(newNode==NULL)
{
return false;
}
Node *ptr=newNode->next;
while(ptr)
{
newNode->next=ptr->next;
delete ptr;
newNode=newNode->next;
}
return true;
}
bool DestoryList(List *pList)
{
Node *ptr=pList->phead;
if(DeleteNewNode(ptr))
{
free(ptr);
ptr=NULL;
return true;
}
}
//添加一个节点
List *InsertNode(List *pList,int pos,datatype data)
{
if(pos<1 || pos>GetLength(pList))
{
printf("the postion is invaliadate\n");
return NULL;
}
else
{
Node *newNode=(Node *)malloc(sizeof(Node));
newNode->data=data;
if(pos==1)
{
if(plist->phead->next==NULL)
{
Node *pTemp=pList->phead;
pTemp->next=newNode;
newNode->next=NULL;
}
else
{
Node *pTemp=pList->phead;
newNode->next=pTemp->next;
pTemp->next=newNode;
}
}
else if(pos==GetLength(plist))
{
Node *pTail=plist->phead;
while(pTail->next)
{
pTail=pTail->next;
}
newNode->next=pTail->next;
pTail->next=newNode;
newNode->next=NULL;
}
else
{
Node *ptr=plist->phead;
for(int i=1;i<pos;++i)
{
ptr=ptr->next;
}
newNode->next=ptr->next;
ptr->next=newNode;
}
++plist->length;
}
return plist;
}
//删除一个节点
List *DeleteNode(List *pList,int pos,datatype &data)
{
if(pos<1 || pos>GetLength(plist))
{
return NULL;
}
else
{
if(pos==1)
{
Node *ptr=pList->phead;
Node *qtr=plist->phead->next;
if(ptr->next==NULL)
{
ptr->next=qptr->next=NULL;
data=qtr->data;
free(qtr);
}
else
{
ptr->next=qtr->next;
data=qtr->data;
free(qtr);
}
}
else if(pos==GetLength(plist))
{
Node *ptr=plist->phead;
Node *ftr=plist->phead->next;
while(ftr)
{
ftr=ftr->next;
ptr=ptr->next;
}
ptr->next=ftr->next;
data=ftr->data;
free(ftr);
}
else
{
Node *ptr=plist->phead;
for(int i=1;i<pos;++i)
{
ptr=ptr->next;
}
Node *q=ptr->next;
ptr->next=q->next;
data=q->data;
free(ptr);
}
--plist->length;
}
return plist;
#include <stdio.h>
#include <stdlib.h>
//链表结构体
typedef int datatype;
typedef struct NODE
{
datatype data;
struct NODE *next;
}Node;
typedef struct LIST
{
Node *phead;
int length;
}List;
//创建空链表
List *CreateList()
{
Node *phead=(Node *)malloc(sizeof(Node));
phead->next=NULL;
if(NULL==phead)
{
return NULL;
}
List *list=(List *)malloc(sizeof(List));
if(NULL==list)
{
return NULL;
}
list->phead=phead;
list->length=0;
return list;
}
//获取链表的长度
int GetLength(List *pList)
{
return list->length;
}
bool DeleteNewNode(Node *newnode)
{
if(newNode==NULL)
{
return false;
}
Node *ptr=newNode->next;
while(ptr)
{
newNode->next=ptr->next;
delete ptr;
newNode=newNode->next;
}
return true;
}
bool DestoryList(List *pList)
{
Node *ptr=pList->phead;
if(DeleteNewNode(ptr))
{
free(ptr);
ptr=NULL;
return true;
}
}
//添加一个节点
List *InsertNode(List *pList,int pos,datatype data)
{
if(pos<1 || pos>GetLength(pList))
{
printf("the postion is invaliadate\n");
return NULL;
}
else
{
Node *newNode=(Node *)malloc(sizeof(Node));
newNode->data=data;
if(pos==1)
{
if(plist->phead->next==NULL)
{
Node *pTemp=pList->phead;
pTemp->next=newNode;
newNode->next=NULL;
}
else
{
Node *pTemp=pList->phead;
newNode->next=pTemp->next;
pTemp->next=newNode;
}
}
else if(pos==GetLength(plist))
{
Node *pTail=plist->phead;
while(pTail->next)
{
pTail=pTail->next;
}
newNode->next=pTail->next;
pTail->next=newNode;
newNode->next=NULL;
}
else
{
Node *ptr=plist->phead;
for(int i=1;i<pos;++i)
{
ptr=ptr->next;
}
newNode->next=ptr->next;
ptr->next=newNode;
}
++plist->length;
}
return plist;
}
//删除一个节点
List *DeleteNode(List *pList,int pos,datatype &data)
{
if(pos<1 || pos>GetLength(plist))
{
return NULL;
}
else
{
if(pos==1)
{
Node *ptr=pList->phead;
Node *qtr=plist->phead->next;
if(ptr->next==NULL)
{
ptr->next=qptr->next=NULL;
data=qtr->data;
free(qtr);
}
else
{
ptr->next=qtr->next;
data=qtr->data;
free(qtr);
}
}
else if(pos==GetLength(plist))
{
Node *ptr=plist->phead;
Node *ftr=plist->phead->next;
while(ftr)
{
ftr=ftr->next;
ptr=ptr->next;
}
ptr->next=ftr->next;
data=ftr->data;
free(ftr);
}
else
{
Node *ptr=plist->phead;
for(int i=1;i<pos;++i)
{
ptr=ptr->next;
}
Node *q=ptr->next;
ptr->next=q->next;
data=q->data;
free(ptr);
}
--plist->length;
}
return plist;
}
还需要进行优化处理。。。
0 0
- 自定义实现的带头结点的链表
- 不带头结点的链表实现
- 带头结点链表的实现
- 带头结点的链表
- 带头结点的链表
- 带头结点的链表
- 带头结点的链队列实现
- 带头结点的线性链表的实现
- 带头结点的线性链表的编写与实现
- 程序一:带头结点双向链表的简单实现
- 单链表 、带头结点的单链表、循环链表 及其实现
- 单链表的实现(带头结点)
- 单链表的实现(带头结点)
- 带头结点的单链表实现
- 带头结点的双向循环链表
- 带头结点的链表基本操作
- 带头结点的双向循环链表
- 顺序链表 带头结点的
- opensns服务器功能完善
- 常见包
- python3判断字典、列表、元组为空以及字典是否存在某个key的方法
- MYSQL获取昨天的0点和今天的0点
- Java学习技能树
- 自定义实现的带头结点的链表
- 塑型
- Qt只能运行一个实例的3种方法
- 292. Nim Game
- 多态的使用
- python3操作mysql数据库增删改查
- 接口
- mysql5.7版本root密码的问题
- Yii2安装