数据结构复习——线性表的链式存储实现(双向链表)
2016-06-30 15:27
513 查看
其实与单向链表大同小异,只是多了一个前驱指针。操作起来多了几个小步骤而已。
#include<bits/stdc++.h>
using namespace std;
typedef struct Node * Nodeptr;
typedef struct Node
{
int data; //数据
struct Node *pre,*next; //指针
} NODE; //NODE等价于struct Node,Nodeptr等价于struct Node *
Nodeptr createLinklist()//创建
{
int n,value; //记录创建节点的个数和值
Nodeptr Head=(Nodeptr)malloc(sizeof(Nodeptr));//创建头指针
if(Head==NULL) //判断失败操作
{
printf("分配内存失败!\n");
exit(-1);
}
Nodeptr p=Head; //指针p始终指向表尾
Head->pre=NULL;
printf("输入创建节点的个数:");
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d",&value);
Nodeptr NewNode=(Nodeptr)malloc(sizeof(NODE));
if(NewNode==NULL)
{
printf("分配内存失败!\n");
exit(-1);
}
NewNode->data=value; //值放入
NewNode->pre=p;
p->next=NewNode; //指向新节点
p=NewNode; //仍然让p指向末尾
}
p->next=NULL;
return Head;
}
void traverseLinklist(Nodeptr Head)//遍历
{
Nodeptr p=Head->next,q;
while(p!=NULL)
{
printf("%d ",p->data);
q=p;
p=p->next;
}
printf("\n");
while(q!=Head){
printf("%d ",q->data);
q=q->pre;
}
printf("\n");
return ;
}
void insertElement(Nodeptr Head,int pos, int value)//插入
{
//将值为value的元素插入到pos位置
int i=0;
Nodeptr p=Head;
while(p!=NULL&&i<pos-1)//将指针定位到第pos-1个节点,其中可能位置不存在
{
p=p->next;
i++;
}
if(p==NULL||i>pos-1)
{
printf("位置不存在!\n");
return ;
}
Nodeptr NewNode=(Nodeptr)malloc(sizeof(Node));//分配内存
if(NewNode==NULL)
{
printf("分配内存失败!\n");
exit(-1);
}
NewNode->data=value; //赋值
Nodeptr q=p->next; //指向下一个节点
p->next=NewNode;
NewNode->pre=p;
NewNode->next=q;
q->pre=NewNode;
return ;
}
void deleteElement(Nodeptr Head,int pos)//删除
{
//删除同上
int i=0;
Nodeptr p=Head;
while(p!=NULL&&i<pos-1)
{
p=p->next;
i++;
}
if(p->next==NULL||i>pos-1)
{
printf("位置不存在!\n");
return ;
}
Nodeptr q=p->next,t=q->next;//需要删除q
if(t!=NULL){
p->next=t;
t->pre=p;
}
else p->next=t;
free(q);
q=NULL;
return ;
}
int main()
{
Nodeptr head=NULL;
head=createLinklist();
traverseLinklist(head);
insertElement(head,3,10);
traverseLinklist(head);
deleteElement(head,3);
traverseLinklist(head);
return 0;
}
十字链表挖坑待填。有时间的话,就实现以下吧。
#include<bits/stdc++.h>
using namespace std;
typedef struct Node * Nodeptr;
typedef struct Node
{
int data; //数据
struct Node *pre,*next; //指针
} NODE; //NODE等价于struct Node,Nodeptr等价于struct Node *
Nodeptr createLinklist()//创建
{
int n,value; //记录创建节点的个数和值
Nodeptr Head=(Nodeptr)malloc(sizeof(Nodeptr));//创建头指针
if(Head==NULL) //判断失败操作
{
printf("分配内存失败!\n");
exit(-1);
}
Nodeptr p=Head; //指针p始终指向表尾
Head->pre=NULL;
printf("输入创建节点的个数:");
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d",&value);
Nodeptr NewNode=(Nodeptr)malloc(sizeof(NODE));
if(NewNode==NULL)
{
printf("分配内存失败!\n");
exit(-1);
}
NewNode->data=value; //值放入
NewNode->pre=p;
p->next=NewNode; //指向新节点
p=NewNode; //仍然让p指向末尾
}
p->next=NULL;
return Head;
}
void traverseLinklist(Nodeptr Head)//遍历
{
Nodeptr p=Head->next,q;
while(p!=NULL)
{
printf("%d ",p->data);
q=p;
p=p->next;
}
printf("\n");
while(q!=Head){
printf("%d ",q->data);
q=q->pre;
}
printf("\n");
return ;
}
void insertElement(Nodeptr Head,int pos, int value)//插入
{
//将值为value的元素插入到pos位置
int i=0;
Nodeptr p=Head;
while(p!=NULL&&i<pos-1)//将指针定位到第pos-1个节点,其中可能位置不存在
{
p=p->next;
i++;
}
if(p==NULL||i>pos-1)
{
printf("位置不存在!\n");
return ;
}
Nodeptr NewNode=(Nodeptr)malloc(sizeof(Node));//分配内存
if(NewNode==NULL)
{
printf("分配内存失败!\n");
exit(-1);
}
NewNode->data=value; //赋值
Nodeptr q=p->next; //指向下一个节点
p->next=NewNode;
NewNode->pre=p;
NewNode->next=q;
q->pre=NewNode;
return ;
}
void deleteElement(Nodeptr Head,int pos)//删除
{
//删除同上
int i=0;
Nodeptr p=Head;
while(p!=NULL&&i<pos-1)
{
p=p->next;
i++;
}
if(p->next==NULL||i>pos-1)
{
printf("位置不存在!\n");
return ;
}
Nodeptr q=p->next,t=q->next;//需要删除q
if(t!=NULL){
p->next=t;
t->pre=p;
}
else p->next=t;
free(q);
q=NULL;
return ;
}
int main()
{
Nodeptr head=NULL;
head=createLinklist();
traverseLinklist(head);
insertElement(head,3,10);
traverseLinklist(head);
deleteElement(head,3);
traverseLinklist(head);
return 0;
}
十字链表挖坑待填。有时间的话,就实现以下吧。
相关文章推荐
- [C/C++]反转链表
- C#数据结构之顺序表(SeqList)实例详解
- C#实现基于链表的内存记事本实例
- Lua教程(七):数据结构详解
- C#模拟链表数据结构的实例解析
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#实现单链表(线性表)完整实例
- C#数据结构之单链表(LinkList)实例详解
- C#实现顺序表(线性表)完整实例
- 数据结构之Treap详解
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- C++利用静态成员或类模板构建链表的方法讲解
- C++实现简单的学生管理系统
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表
- C#数据结构之堆栈(Stack)实例详解
- C#数据结构之双向链表(DbLinkList)实例详解
- JavaScript数据结构和算法之图和图算法
- Linux内核链表实现过程