标签:next node getch lse 头结点 ext element 直接 位置
第i个数据元素的存储位置是:LOC(ai)=LOC(a1)+(i-1)*m
线性表的表长表示为(*L).len或者L->len。第i个元素写为L->v[i-1]
单链表的头指针指向头结点称其为带头结点的单链接。若无特别说明,用的都是带头结点的单链表。
p->data表示p指向的结点的数据域。
p->next表示p指向的结点的指针域。
p=(Lnode *)malloc(sizeof(Lnode);
free(p)释放p所指向的结点空间。
单链表的相关操作
#include<stdio.h>
#include<stdlib.h>
typedef char elmentype;
typedef struct node
{
elementype data;
struct node *next;
}Lnode,*linklist;//Lnode结点类型,linklist是指向结点的指针类型
void main()
{
Lnode *h;
Lnode *creat();
h = creat();
}
Lnode *creat()//头插法
{
elementype ch;
h=(Lnode *)malloc(sizeof(Lnode));//建立头结点
h->next=NULL;//使头结点的指针域为空
while((ch=getchar(!)=‘\n‘))
{
p=(Lnode *)malloc(sizeof(Lnode));//建立新结点p
p->data=ch;//ch赋给p的数据域
p->next=h->next;//改变指针状况
h->next=p;//h的直接后继是p
}
return h;
}
Lnode *creat()//尾插法
{
Lnode *h,*p,*t;
elementype ch;
h = (Lnode *)malloc(sizeof(Lnode));
h->next=NULL;
t=h;
while((ch=getchar()!=‘\n‘));
{
p=(Lnode *)malloc(sizeof(Lnode));
p->data=ch;
p->next=NULL;
t->next=p;//t指向最后一个元素
t=p;
}
return h;
}
int lenth(Lnode *h)
{
Lnode *p;
int i = 0;
p = h->next;//p访问第一个结点
while(p)
{
i++;
p=p->next;
}
return i;
}
void insert(Lnode *p,elementype x)//值为x的结点插在p后
{
Lnode *s;
s = (Lnode *)malloc(sizeof(Lnode));//生成结点s
s->data = x;
s->next = p->next;
p->next = s;
}
int insert(Lnode *h,int i,elementype x)//第i个元素中插入一个元素
{
Lnode *p,*s;
int j=0;
p=h;
while(p&&(j<i-1))//找第i-1个结点
{
p=p->next;
j++;
}
if(p)
{
s = (Lnode *)malloc(sizoef(Lnode));
s->data=x;
s->next=p->next;
p->next=s;
return 1;
}
else
return 0;
}
void dele(Lnode *p)
{
Lnode *p;
if(p->next!=NULL)
{
q=p->next;//q是p的后继
p->next=q->next;//删除q
free(q);
}
}
标签:next node getch lse 头结点 ext element 直接 位置
原文地址:https://www.cnblogs.com/claudia529/p/11103373.html