线性表—由链表实现
来源:互联网 发布:http index.php 编辑:程序博客网 时间:2024/06/11 12:11
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
typedef int datatype;
typedef struct _node_
{
datatype data;
struct _node_ *next;
} linknode, *linklist;
{
datatype data;
struct _node_ *next;
} linknode, *linklist;
linklist CreateEmptyLinklist_1()
{
linklist h;
{
linklist h;
h = (linklist)malloc(sizeof(linknode));
h->next = NULL;
h->next = NULL;
return h;
}
}
void CreateEmptyLinklist_2(linklist *h)
{
*h = (linklist)malloc(sizeof(linknode));
(*h)->next = NULL;
{
*h = (linklist)malloc(sizeof(linknode));
(*h)->next = NULL;
return;
}
}
int LengthLinklist(linklist h)
{
int length = 0;
{
int length = 0;
h = h->next;
while (h != NULL)
{
length++;
h = h->next;
}
while (h != NULL)
{
length++;
h = h->next;
}
return length;
}
}
int EmptyLinklist(linklist h)
{
return (NULL == h->next);
}
{
return (NULL == h->next);
}
void VisitLinklist(linklist h)
{
h = h->next;
{
h = h->next;
while (h != NULL)
{
printf("%d ", h->data);
h = h->next;
}
{
printf("%d ", h->data);
h = h->next;
}
return;
}
}
int LocateLinklist(linklist h, datatype x)
{
int pos = 0;
{
int pos = 0;
h = h->next;
while (h != NULL)
{
if (h->data == x) return pos;
pos++;
h = h->next;
}
while (h != NULL)
{
if (h->data == x) return pos;
pos++;
h = h->next;
}
return -1;
}
}
int InsertLinklist_1(linklist h, datatype x, int pos)
{
linklist p;
{
linklist p;
if ((pos < 0) || (pos > LengthLinklist(h))) return -1;
while ( pos-- ) h = h->next;
p = (linklist)malloc(sizeof(linknode));
p->data = x;
p->next = h->next;
h->next = p;
p = (linklist)malloc(sizeof(linknode));
p->data = x;
p->next = h->next;
h->next = p;
return;
}
}
void InsertLinklist_2(linklist h, datatype x)
{
linklist p = h, q;
{
linklist p = h, q;
q = (linklist)malloc(sizeof(linknode));
q->data = x;
q->data = x;
while ((p->next != NULL) && (p->next->data < x)) p = p->next;
q->next = p->next;
p->next = q;
q->next = p->next;
p->next = q;
return;
}
}
int DeleteLinklist_1(linklist h, int pos)
{
linklist q;
{
linklist q;
if ((pos < 0) || (pos >= LengthLinklist(h))) return -1;
while ( pos-- ) h = h->next;
q = h->next;
h->next = q->next;
free(q);
q = h->next;
h->next = q->next;
free(q);
return 0;
}
}
void DeleteLinklist_2(linklist h, datatype x)
{
linklist q = h->next;
{
linklist q = h->next;
while (NULL != q)
{
if (q->data == x)
{
h->next = q->next;
free(q);
}
else
{
h = q;
}
q = h->next;
}
{
if (q->data == x)
{
h->next = q->next;
free(q);
}
else
{
h = q;
}
q = h->next;
}
return;
}
}
void ClearLinklist(linklist h)
{
linklist p = h->next, q;
{
linklist p = h->next, q;
while (p != NULL)
{
q = p;
p = p->next;
free(q);
}
{
q = p;
p = p->next;
free(q);
}
h->next = NULL;
return;
}
}
int main()
{
int i;
linklist h, p, q;
h = CreateEmptyLinklist_1();
//CreateEmptyLinklist_2(&h);
p = h;
for (i=0; i<10; i++)
{
q = (linklist)malloc(sizeof(linknode));
q->data = i;
q->next = NULL;
p->next = q;
p = q;
}
p = h->next;
while (p != NULL)
{
printf("%d " ,p->data);
p = p->next;
}
printf("\n");
//CreateEmptyLinklist_2(&h);
p = h;
for (i=0; i<10; i++)
{
q = (linklist)malloc(sizeof(linknode));
q->data = i;
q->next = NULL;
p->next = q;
p = q;
}
p = h->next;
while (p != NULL)
{
printf("%d " ,p->data);
p = p->next;
}
printf("\n");
return 0;
}
}
- 线性表—由链表实现
- 线性表—由数组实现
- 约瑟夫问题—由线性链表实现
- 线性结构--->线性表实现
- 栈的实现—由链表实现
- 队列的实现—由链表实现
- 线性表-链表实现
- 类实现线性链表
- 线性链表实现注意事项
- 线性链表 java实现
- java实现线性链表
- C++实现线性链表
- 线性表—顺序表-创建线性表的实现
- 由链表实现的容器
- 线性表的实现
- 线性表的实现
- 线性表(C#实现)
- C++实现线性表
- shell编程实例六
- Makefile文件简介
- 实例二、从字符串中取出所有整数
- 实例一、求二维数组的和
- 线性表—由数组实现
- 线性表—由链表实现
- 约瑟夫问题—由线性链表实现
- GPGPU简介
- 栈的实现—由数组实现
- 栈的实现—由链表实现
- 栈的实例—表达式求值
- 队列的实现—由数组实现
- 队列的实现—由链表实现
- 利用队列和栈实现球钟问题