数据结构之链表(一)

简介:
线性表分为顺序存储结构和链式存储结构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;
}
实际应用中动态链表更为常用。
 














本文转自terryli51CTO博客,原文链接: http://blog.51cto.com/terryli/520620,如需转载请自行联系原作者


相关文章
|
29天前
|
存储 缓存 算法
数据结构-链表(一)
链表(Linked List)是一种常见的数据结构,用于存储和组织数据。与数组不同,链表的元素(节点)在内存中不必连续存储,而是通过指针链接在一起。 链表由多个节点组成,每个节点包含两部分:数据(存储实际的元素值)和指针(指向下一个节点的引用)。链表的第一个节点称为头节点,最后一个节点称为尾节点,尾节点的指针通常指向空值(null)。
31 1
|
1月前
|
存储 C++
数据结构第六弹---带头双向循环链表
数据结构第六弹---带头双向循环链表
|
1月前
|
存储
【单链表】数据结构单链表的实现
【单链表】数据结构单链表的实现
|
1月前
|
C++
从0开始回顾数据结构---链表与堆
#include <iostream> #include <algorithm> #include <string.h> using namespace std; const int N = 100010; int h[N], ph[N], hp[N], cnt; void heap_swap(int a, int b) { swap(ph[hp[a]],ph[hp[b]]); swap(hp[a], hp[b]); swap(h[a], h[b]); } void down(int u) { int t = u; if (u * 2 <= cnt &&
|
1天前
|
存储 C语言
数据结构基础:双链表结构、实现
数据结构基础:双链表结构、实现
|
11天前
数据结构—链表(超详细)(山东大学)(数据结构实验三)
数据结构—链表(超详细)(山东大学)(数据结构实验三)
数据结构|双向链表|带头结点|头插|尾插|尾删|头删
数据结构|双向链表|带头结点|头插|尾插|尾删|头删
|
14天前
数据结构--链表刷题(一)快慢指针(上)
数据结构--链表刷题(一)快慢指针
16 0
|
23天前
|
缓存 算法 搜索推荐
【数据结构】链表(单链表与双链表实现+原理+源码)
【数据结构】链表(单链表与双链表实现+原理+源码)
|
27天前
|
存储 编译器 C语言
【数据结构】深入浅出理解链表中二级指针的应用
【数据结构】深入浅出理解链表中二级指针的应用
28 0