单链表(带头结点)
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
/* data */
}Node, *Head;
//头插法
void head_creat_list(Head p, int n){
//n是容量
/*
p = (Head)malloc(sizeof(Node));
*/
//p->next = NULL;
int i;
for(i=1; i<=n; ++i){
Node *q = (Node *)malloc(sizeof(Node));
scanf("%d",&q->data);
q->next = p->next;
p->next = q;
}
}
//尾插法
void last_creat_list(Head p, int n){
//n是容量
/*
p = (Head)malloc(sizeof(Node));
*/
int i;
for(i=1; i<=n; ++i){
Node *q = (Node *)malloc(sizeof(Node));
scanf("%d",&q->data);
p->next = q;
p = q;
}
p->next = NULL;
}
void insert_elem(Head p, int i, ElemType e){
//i为插入位置
//e是插入元素
int locate = 1;
//p = p->next;
while(locate < i && p){
p = p->next;
locate++;
}
//循环结束
//p指向第i-1个元素
if(!p)
return ;
Node *q = (Node *)malloc(sizeof(Node));
q->data = e;
q->next = p->next;
p->next = q;
}
void delete_elem(Head p, int i, ElemType *e){
int locate = 1;
Node *q = p;
p = p->next;
while(locate < i && p){
q = p;
p = p->next;
locate++;
}
if(!p)
return ;
*e = p->data;
q->next = p->next;
free(p);
}
int locate_elem(Head p, ElemType e){
int i=1;
p = p->next;
while(p && p->data != e){
p= p->next;
++i;
}
if(!p)
return 0;
return i;
}
void show(Head p){
//第一个数据节点
p = p->next;
while(p){
printf("%3d",p->data);
p = p->next;
}
printf("\n");
}
int main(){
Node head;
last_creat_list(&head,3);
show(&head);
insert_elem(&head,1,4);
show(&head);
insert_elem(&head,2,3);
show(&head);
ElemType e;
delete_elem(&head,2,&e);
show(&head);
printf("%d\n",e);
int m;
m = locate_elem(&head,4);
printf("%d",m);
return 0;
}
单链表(不带头结点)
相较于带头结点的单链表,只需要插入删除时,对 第一个位置 进行特殊考虑。
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
/* data */
}Node;
typedef struct
{
/* data */
Node *the_first;
}List;
//头插法
void head_creat_list(List *list, int n){
//n是容量
Node *p = list->the_first;
scanf("%d",&p->data);
p->next = NULL;
int i;
for(i=1; i<n; ++i){
Node *q = (Node *)malloc(sizeof(Node));
scanf("%d",&q->data);
q->next = p;
p = q;;
}
}
//尾插法
void last_creat_list(List *list, int n){
//n是容量
Node *p = list->the_first;
scanf("%d",&p->data);
int i;
for(i=1; i<n; ++i){
Node *q = (Node *)malloc(sizeof(Node));
scanf("%d",&q->data);
p->next = q;
p = q;
}
p->next = NULL;
}
void insert_elem(List *list, int i, ElemType e){
//i为插入位置
//e是插入元素
Node *p = list->the_first;
if(i <= 0)
return ;
else if(i == 1){
Node *q = (Node *)malloc(sizeof(Node));
q->data = e;
q->next = p;
list->the_first = q;
}
else if(i > 1){
int locate = 1;
//p = p->next;
while(locate < i-1 && p){
p = p->next;
locate++;
}
//循环结束
//p指向第i-1个元素
if(!p)
return ;
Node *q = (Node *)malloc(sizeof(Node));
q->data = e;
q->next = p->next;
p->next = q;
}
}
void delete_elem(List *list, int i, ElemType *e){
Node *p = list->the_first;
if(i == 1){
*e = p->data;
list->the_first = p->next;
free(p);
}
else if(i > 1){
int locate = 1;
Node *q = p;
p = p->next;
while(locate < i && p){
q = p;
p = p->next;
locate++;
}
if(!p)
return ;
*e = p->data;
q->next = p->next;
free(p);
}
}
int locate_elem(List *list, ElemType e){
Node *p = list->the_first;
int i=1;
while(p && p->data != e){
p= p->next;
++i;
}
if(!p)
return 0;
return i;
}
void show(List *list){
//第一个数据节点
Node *p = list->the_first;
while(p){
printf("%3d",p->data);
p = p->next;
}
printf("\n");
}
int main(){
List list;
last_creat_list(&list,3);
show(&list);
insert_elem(&list,1,4);
show(&list);
insert_elem(&list,2,3);
show(&list);
ElemType e;
delete_elem(&list,1,&e);
show(&list);
printf("%d\n",e);
int m;
m = locate_elem(&list,4);
printf("%d",m);
return 0;
}
循环链表
单链表的循环链表就是初始化时将头结点的指针指向自身,然后经过一系列操作后,最后一个元素的指针域不再是NULL,而是指向头结点,实现循环。