一、链表结构
顺序表结构包含两部分:
1.除head节点外都含有数据信息
2.指向下一个节点的指针信息
操作:
插入:找到节点前一个节点p,新节点指向插入位置节点,p指向新节点
删除:将前一个节点的next指针指向下一个节点地址
两种链表:
无头链表:头部指针没有存储信息的单元,即头部只是一个指针
有头链表:头部节点是以一个链表节点,有存储信息的单元但不存储信息。因此叫虚拟头节点。
二、链表代码
1.引库
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
2.代码
//无头链表定义及操作
typedef struct Node{
int data;
struct Node *next;
} Node;
Node *getNewNode(int val){
Node *p=(Node *)malloc(sizeof(Node));
p->data=val;
p->next=NULL;
return p;
}
Node *insert(Node *head,int pos,int val){
if(pos==0){
Node *p=getNewNode(val);
p->next=head;
return p;
}
Node *p=head;
for(int i=1;i<pos;i++) p=p->next;
Node *node=getNewNode(val);
node->next=p->next;
p->next=node;
return head;
}
void clear(Node *head){
if(head==NULL) return ;
for(Node *p=head,*q;p;p=q){
q=p->next;
free(p);
}
}
void output_linklist(Node *head){
int n=0;
for(Node *p=head;p;p=p->next){
printf("%3d",n);
printf(" ");
n+=1;
}
/*for(int i=0;i<n;i++){
printf("%3d",i);
printf(" ");
}*/
printf("\n");
for(Node *p=head;p;p=p->next){
printf("%3d",p->data);
if(p->next) printf("->");
}
printf("\n\n\n");
return ;
}
int main(){
srand(time(0));
#define MAX_OP 20
Node *head=NULL;
for(int i=0;i<MAX_OP;i++){
int pos=rand()%(i+1),val=rand()%100;
printf("insert %d at %d to linklist\n",val,pos);
head=insert(head,pos,val);
output_linklist(head);
}
clear(head);
return 0;
}
3.查找
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define DL 3
#define STR(n) #n
#define DIGIT_LEN_STR(n) "%" STR(n) "d"
//无头链表定义及操作
typedef struct Node{
int data;
struct Node *next;
} Node;
Node *getNewNode(int val){
Node *p=(Node *)malloc(sizeof(Node));
p->data=val;
p->next=NULL;
return p;
}
Node *insert(Node *head,int pos,int val){
if(pos==0){
Node *p=getNewNode(val);
p->next=head;
return p;
}
Node *p=head;
for(int i=1;i<pos;i++) p=p->next;
Node *node=getNewNode(val);
node->next=p->next;
p->next=node;
return head;
}
void clear(Node *head){
if(head==NULL) return ;
for(Node *p=head,*q;p;p=q){
q=p->next;
free(p);
}
}
void output_linklist(Node *head,int flag=0){
int n=0;
for(Node *p=head;p;p=p->next){
printf(DIGIT_LEN_STR(DL),n);
printf(" ");
n+=1;
}
printf("\n");
for(Node *p=head;p;p=p->next){
printf(DIGIT_LEN_STR(DL),p->data);
if(p->next) printf("->");
}
printf("\n");
if(flag==0) printf("\n\n");
return ;
}
int find(Node *head,int val){
Node *p=head;
int n=0;
while(p){
if(p->data==val){
output_linklist(head,1);
int len =n*(DL+2)+2;
for(int i=0;i<len;i++) printf(" ");
printf("^\n");
for(int i=0;i<len;i++) printf(" ");
printf("|\n");
return 1;
}
n+=1;
p=p->next;
}
return 0;
}
int main(){
srand(time(0));
#define MAX_OP 20
Node *head=NULL;
for(int i=0;i<MAX_OP;i++){
int pos=rand()%(i+1),val=rand()%100;
printf("insert %d at %d to linklist\n",val,pos);
head=insert(head,pos,val);
output_linklist(head);
}
//查找
int val;
while(~scanf("%d",&val)){
if(!find(head,val)){
printf("not found\n");
}
}
clear(head);
return 0;
}
4.有头链表进行插入改进
无需单独处理链首插入,直接统一处理
Node *insert(Node *head,int pos,int val){
Node new_head,*p=&new_head,*node=getNewNode(val);
new_head.next=head;
for(int i=0;i<pos;i++) p=p->next;
node->next=p->next;
p->next=node;
return new_head.next;
}
本文详细介绍了链表的基本结构,包括顺序表和链表的节点构成,重点讲解了无头链表和有头链表的区别,以及提供了链表插入和查找的C语言实现代码示例。
1113

被折叠的 条评论
为什么被折叠?



