当前位置:文档之家› 链表的基本操作

链表的基本操作

2.链表的基本操作
对链表施行的操作有很多种,最基本的操作是在链表中插入结点、在链表中删除结点、在链表中查找结点等。

(1) 链表结点的插入
①在空链表中插入一个结点
空链表就是头指针为空的链表。

a)用如下语句申请一个new结点:
new=(struct node)calloc(1,sizeof(struct node));
b)为new结点填充数据:将要存储的数据对应传递给new结点数据域的各个成员。

c)修改有关指针的指向:将new的next成员置空,使new结点成为链表的最后一个结点;将head指向new结点。

②在链表的结点之后插入一个结点
要在链表head的C、D结点之间出入一个new结点,就是将new结点变成C结点的下一个结点,而new结点的下一个结点为D结点.
操作过程为:
a) 使new的指针域存储结点D的首地址。

b) 使C结点的指针域存储结点new的地址。

例2 建立学生成绩链表,链表有3个结点。

#include <stdio.h>
#define N 3
struct s_node
{
char num[4];
int score;
struct s_node *next;
};
main()
{
struct s_node *creat_node(void); /*生成链表结点的函数*/ struct s_node *creat_list(int n); /*建立链表的函数*/ void out_list(struct s_node *head); /*输出链表函数*/
struct s_node *head=NULL;
head=creat_list(N);
out_list(head);
}
struct s_node *creat_node(void) /*生成链表结点的函数*/ {
struct s_node *p;
int score;
fflush(stdin);
p=(struct s_node *)calloc(1,sizeof(struct s_node));
gets(p->num);
scanf("%d",&score);
p->score=score;
p->next=NULL;
return(p);
}
struct s_node * creat_list(int n) /*建立链表的函数*/ {
struct s_node *new,*p;
struct s_node *head;
int i;
if(n>=1)
{
new=creat_node();
head=new;
p=new;
}
for(i=2;i<=n;i++)
{
new=creat_node();
p->next=new;
p=new;
}
if(n>=1)
return(head);
else
return(NULL);
}
void out_list(struct s_node *head) /*输出链表函数*/
{
struct s_node *p;
if(head!=NULL)
{
p=head;
while(p!=NULL)
{
printf("%s %d\n",p->num,p->score);
p=p->next;
}
}
}
(2) 链表结点的删除
从链表中删除结点,就是撤销结点在链表中的链接,把结点从链表中孤立出来。

在链表链表中删除结点一般有两个过程:一是把指定的结点从链表中拿下来,它需要通过修改有关结点的指针域来完成;二是释放该结点使用的内存空间,它需要使用free()函数来完成。

如下是在head链表中删除p结点的delete_p()函数:
void delete_p(struct node *head,struct node *p)
{
struct node *q;
if(p==NULL)
{
printf("no found\n");
return;
}
if(p==head)
{
head=p->next;
q=p;
}
else
{
q=p->next;
p->next=q->next;
}
printf("\ndelete :%d\n",q->data);
free(q);
}
(3) 链表结点的查找
在链表中进行查找,就是从链表的第一个结点开始,沿着指针链,用查找值与链表结点逐个比较的过程。

找到符合要求的结点之后,停止查找过程,返回相应的结点指针,否则返回一个空指针。

例3 查找值为x的结点并删除
#include <stdio.h>
struct node
{
int data;
struct node *next;
};
struct node * creat_number(int n) /*建立链表的函数*/ {
struct node *new,*p;
struct node *head;
int i;
if(n>=1)
{
new=(struct node *)malloc(sizeof(struct node));
new->data=1;
new->next=NULL;
head=new;
p=new;
}
for(i=2;i<=n;i++)
{
new=(struct node *)malloc(sizeof(struct node));
new->data=i;
new->next=NULL;
p->next=new;
p=new;
}
if(n>=1)
return(head);
else
return(NULL);
}
void out_list(struct node *head) /*输出链表函数*/ {
struct node *p;
if(head!=NULL)
{
p=head;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
}
struct node *find_x(struct node *head,int x) /*在链表中查找的函数*/
{
struct node *p,*q;
p=q=head;
while(p!=NULL&&p->data!=x)
{
q=p;
p=p->next;
}
if(p==NULL)
return(NULL);
else
return(q);
}
void delete_p(struct node *head,struct node *p) /*在链表中删除p结点的函数*/
{
struct node *q;
if(p==NULL)
{
printf("no found\n");
return;
}
if(p==head)
{
head=p->next;
q=p;
}
else
{
q=p->next;
p->next=q->next;
}
printf("\ndelete :%d\n",q->data); free(q);
}
main()
{
int n,x;
struct node *head=NULL,*p;
printf("Enter n,x:");
scanf("%d,%d",&n,&x);
head=creat_number(n);
out_list(head); p=find_x(head,x); delete_p(head,p); out_list(head); }。

相关主题