数据结构之链表(一)
来源:互联网 发布:热火队球员数据 编辑:程序博客网 时间:2024/05/22 01:35
线性表分为顺序存储结构和链式存储结构2种。
顺序存储结构的特点:任何一个元素都可以进行随即存取,存取速度高。但不适合濒繁的插入和删除操作。
链式存储结构(链表):不可以随即存取元素。但适合频繁的插入和删除操作。
一个静态链表的例子:
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
typedef struct node nodeType;
int main()
{
nodeType sOne,sTwo,sThree, *begin, *p;
clrscr();
sOne.data = 1;
sTwo.data = 2;
sThree.data = 3;
begin = &sOne;
sOne.next = &sTwo;
sTwo.next = &sThree;
sThree.next = '/0';
p=begin;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("/n");
getch();
return 0;
}
一个动态链表的例子:
#include<stdio.h>
#include<alloc.h>
typedef struct node
{
int data;
struct node *next;
}nodeType;
nodeType *CreateList() /*这个方法的作用:返回头指针(头结点),头结点没有数据域*/
{
int i;
nodeType *begin,*end,*current;
begin = (nodeType *)malloc(sizeof(nodeType));
end = begin;
/* begin->data = 1000;*/
scanf("%d",&i);
while(i!=-1) /*输入为-1时,退出循环*/
{
current = (nodeType *)malloc(sizeof(nodeType));
current->data = i;
end->next = current;
end = current;
scanf("%d",&i);
}
end->next = '/0';
return begin;
}
int main()
{
nodeType *head;
head = CreateList();
while(head)
{/* 顺序访问链表中各结点的数据域*/
printf("%d ",head->data); /*头结点没有数据域,所以打印头结点时,数据是随即的。
head=head->next;
/* head = head->next;
printf("%d ",head->data);
*/
}
getch();
return 0;
}
实际应用中动态链表更为常用。
- 数据结构之链表(一)
- 数据结构(一)之链表
- (一)Android数据结构学习之链表
- 数据结构之链表(一)
- 数据结构(一)之链表
- 数据结构之链表(一)
- 数据结构(一)之链表
- 数据结构之线性表一:
- 数据结构之线性表一
- 数据结构之链表(一)创建,插入,删除
- 数据结构之Linux Kernel双向链表(一)
- 基础数据结构之数组与链表(一)
- 探究数据结构之链表Java代码实现(一)
- 数据结构与算法之单向链表 <一>
- 数据结构之线性表(linear_list)一
- 数据结构之线性表(一)
- 数据结构之线性表(一)
- 数据结构学习之顺序表(一)
- Exchange 2000 Server的传递状态通知
- ACE学习笔记--持续更新中
- 一个强类型ArrayList类。
- Inside the Windows Vista Kernel: Part 1
- javascript判断窗口刷新及关闭
- 数据结构之链表(一)
- Exchange Server退信及解决方案总汇
- 浅谈HOOK技术在VC编程中的应用
- javascript技巧集合
- 螺旋矩阵
- 主题:你为什么还没有好工作?
- 当开发遭遇国情
- 移动硬盘过热水冷系统
- Set-ExecutionPolicy 帮助信息