链表源码之单向链表
来源:互联网 发布:bilibili mac版 编辑:程序博客网 时间:2024/06/16 06:37
C数据结构单向链表
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
typedef struct PNode
{
int data;
struct PNode *next;
}Node;
void CreateListFirstInsert(Node *&N,int a[],int n)
{
Node *T;
N =(Node *)malloc(sizeof(Node));
N->data = 0;
N->next = NULL;
for(int i = 0;i<= n;i++)
{
T = (Node *)malloc(sizeof(Node));
T->data = a[i];
T->next = N->next;
N->next = T;
}
}
void CreateListLastInsert(Node *&N,int a[],int n)
{
Node *T,*R;
N =(Node *)malloc(sizeof(Node));
N->data = 0;
R = N;
for(int i = 0;i< n;i++)
{
T = (Node *)malloc(sizeof(Node));
T->data = a[i];
R->next = T;
R = R->next;
}
T->next = NULL;
}
int GetLength(Node *N)
{
assert(N);
int i = 0;
Node *p = N;
while(p != NULL)
{
p = p->next;
i++;
}
return i;
}
void DisplayList(Node *N)
{
if(N == NULL)
return;
Node *p = N->next;
while(p != NULL)
{
printf("%d/n",p->data);
p = p->next;
}
}
int InsertList(Node *N,int n,int x)
{
assert(N);
int i = 0;
Node *p = N;
Node *L = (Node *)malloc(sizeof(Node));
L->data = x;
if(n <=0 || n >= GetLength(N))
{
printf("please enter a right number!!");
return 0;
}
while(i < n)
{
p = p->next;
i++;
}
L->next = p->next;
p->next = L;
return 1;
}
//首先要保证变量n的正确性,用p指向它,q指向要删除的结点。
int DeletetList(Node *N,int n)
{
assert(N);
int i = 1;
Node *p = N,*q;
if(n <=0 || n >= GetLength(N))
{
printf("please enter a right number!!");
return 0;
}
while(i < n)
{
p = p->next;
i++;
}
q = p->next;
p->next = q->next;
return 1;
}
void main()
{
Node *Node = NULL;
int a[]={1, 2, 3, 4, 5};
CreateListLastInsert(Node,a,5);
if(Node != NULL)
DisplayList(Node);
if(InsertList(Node,3,6))
printf("Insert success!!/n");
if(DeletetList(Node,3))
printf("Delete success!!/n");
}
===================================================================
C数据结构单向循环链表
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
typedef struct PNode
{
int data;
struct PNode *next;
}Node;
void CreateListFirstInsert(Node *&N,int a[],int n)
{
Node *T;
N =(Node *)malloc(sizeof(Node));
N->data = 0;
N->next = N; //注意:此处和单向链表的差异
for(int i = 0;i<= n;i++)
{
T = (Node *)malloc(sizeof(Node));
T->data = a[i];
T->next = N->next;
N->next = T;
}
}
void CreateListLastInsert(Node *&N,int a[],int n)
{
Node *T,*R;
N =(Node *)malloc(sizeof(Node));
N->data = 0;
R = N;
for(int i = 0;i< n;i++)
{
T = (Node *)malloc(sizeof(Node));
T->data = a[i];
R->next = T;
R = R->next;
}
T->next = NULL;
}
//设置i为计数器,当p指针等于头结点时结束查找
int GetLength(Node *N)
{
assert(N);
int i = 0;
Node *p = N->next;
while(p != N)
{
i++;
p = p->next;
}
return i;
}
void DisplayList(Node *N)
{
if(N == NULL)
return;
Node *p = N->next;
while(p != NULL)
{
printf("%d/n",p->data);
p = p->next;
}
}
int InsertList(Node *N,int n,int x)
{
assert(N);
int i = 0;
Node *p = N;
Node *L = (Node *)malloc(sizeof(Node));
L->data = x;
if(n <=0 || n >= GetLength(N))
{
printf("please enter a right number!!");
return 0;
}
while(i < n)
{
p = p->next;
i++;
}
L->next = p->next;
p->next = L;
return 1;
}
//首先要保证变量n的正确性,用p指向它,q指向要删除的结点。
int DeletetList(Node *N,int n)
{
assert(N);
int i = 1;
Node *p = N,*q;
if(n <=0 || n >= GetLength(N))
{
printf("please enter a right number!!");
return 0;
}
while(i < n)
{
p = p->next;
i++;
}
q = p->next;
p->next = q->next;
return 1;
}
void main()
{
Node *Node = NULL;
int a[]={1, 2, 3, 4, 5};
CreateListFirstInsert(Node,a,5);
if(Node != NULL)
printf("create success length is %d!!/n",GetLength(Node));
if(InsertList(Node,3,6))
printf("Insert success!!/n");
if(DeletetList(Node,3))
printf("Delete success!!/n");/**/
}
- 链表源码之单向链表
- AMPS:单向链表源码解读
- 单向链表(六) 工程源码
- 数据结构之单向链表
- 数据结构之单向链表
- 数据结构之单向链表
- 数据结构之单向链表
- 数据结构之单向链表
- C++学习笔记之单向链表
- 静下心来写算法(之单向链表)
- 数据结构学习之单向链表结构
- 单向链表之c++实现二
- 数据结构之单向链表实现
- 数据结构之单向循环链表
- 数据结构之单向链表02
- 数据结构之单向链表03
- 第十八节 数据结构之单向链表
- 单向链表之C++实现
- 古代官职VS现代官职
- 常见资源网站和技术论坛
- 2010年的一些回顾
- 不容易
- 修改eclipse下创建JSP的默认编码
- 链表源码之单向链表
- Linux中的缩写
- 几个开源平台
- 大家好
- mac os x 释放磁盘空间
- 51testing博客
- 【转帖】了解你的敌人
- 【Android】Http请求
- window.open()与window.opener()