单链表是数据结构最基本的数据存储结构,当需要频繁的插入或删除数据是可利用单链表的结构来存储数据。
1:自定义单链表单个元素的结构,包括当前结点信息和与下一结点之间的关系:
#ifndef _LINKEDLIST_H_
#define _LINKEDLIST_H_
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct node{
char data;
struct node *next;
}*Linklist, Node;
int InitLinklist(Linklist head);
Linklist Insert_ahead(Linklist head);
Linklist Insert_stren(Linklist head);
int Length(Linklist head);
Linklist Delet_Linklist(Linklist head, int location);
Linklist Delet_Linklist1(Linklist head, char elem);
void Print(Linklist head);
Linklist Insert_Linklist(Linklist head, int loc, char elem);
Linklist Reserve_Linklist(Linklist head);
#endif
2:初始化单链表:
//初始化链表
int InitLinklist(Linklist head){
if (head){
head->next = NULL;
return 1;
}
else{
return 0;
}
}
3:带有头结点的单链表的头插法和尾插法:
//头插法创建单链表
Linklist Insert_ahead(Linklist head){
Linklist l;
Node *s;
l = head;
char c;
int flag = 1;
while (flag){
scanf("%c", &c);
if (c != '$'){
s = (Linklist)malloc(sizeof(Node));
s->data = c;
s->next = l->next;
l->next = s;
}
else{
flag = 0;
}
}
return l;
}
//尾插法创建单链表
Linklist Insert_stren(Linklist head){
Linklist l;
Node *s;
l = head;
char c;
int flag = 1;
while (flag){
scanf("%c", &c);
if (c != '$'){
s = (Linklist)malloc(sizeof(Node));
s->data = c;
s->next = NULL;
l->next = s;
l = s;
}
else{
flag = 0;
}
}
return l;
}
3:不带有头结点的单链表的头插法和尾插法:
//无头结点的单链表操作
int InitLinklist(Linklist head){
if (head){
head->next = NULL;
return 1;
}
else{
return 0;
}
}
//头插法
Linklist Insert_ahead(Linklist head){
Linklist l;
Node *s;
char c;
l = head;
int flag = 1;
int count = 1;
while (flag){
scanf("%c", &c);
if (c != '$'){
if (count == 1){
head->data = c;
head->next = NULL;
count++;
}
else{
s = (Linklist)malloc(sizeof(Node));
s->data = c;
s->next = NULL;
head->next = s;
head = s;
}
}
else{
flag = 0;
}
}
return l;
}
//尾插法
Linklist Insert_stren(Linklist head){
Linklist l;
Node *s;
l = head;
char c;
int flag = 1;
int count = 0;
while (flag){
scanf("%c", &c);
if (c != '$'){
s = (Linklist)malloc(sizeof(Node));
s->data = '0';
s->next = NULL;
l->data = c;
l->next = s;
l = s;
}
else{
flag = 0;
}
}
return l;
}
4:带有头结点的单链表的操作:
//删除指定位置
Linklist Delet_Linklist(Linklist head, int location){
Linklist del = NULL;
Linklist l;
l = head->next;
int i = 0;
if (location<0 || location>Length(head)){
printf("Plese input a right locatioon:>\n");
}
if (location == Length(head)){
for (i = 1; i < location - 1; i++){
l = l->next;
}
del = l->next;
l->next = NULL;
}
else if (l->next != NULL){
for (i = 1; i < location - 1; i++){
l = l->next;
}
del = l->next;
l->next = l->next->next;
}
free(del);
return head;
}
//删除指定元素
Linklist Delet_Linklist1(Linklist head, char elem){
Linklist l;
Linklist del = NULL;
l = head->next;
while (l->next->data != elem){
l = l->next;
}
del = l->next;
l->next = l->next ->next;
free(del);
return head;
}
//在指定位置插入指定元素
Linklist Insert_Linklist(Linklist head, int loc, char elem){
Linklist l;
Linklist s = malloc(sizeof(Node));
s->data = elem;
s->next = NULL;
l = head->next;
int i = 0;
for (i = 1; i < loc-1; i++){
l = l->next;
}
s->next = l->next;
l->next = s;
return l;
}
//就地逆转链表
Linklist Reserve_Linklist(Linklist head){
Linklist p, q;
p = head->next;
q = p->next;
p->next=NULL;
while (p != NULL&&q != NULL){
p = q;
q = q->next;
p->next = head->next;
head->next = p;
}
return p;
}
//打印链表
void Print(Linklist head){
Linklist l;
l = head->next;
while (l != NULL){
printf("%c", l->data);
l = l->next;
}
printf("\n");
}
5:不带有头结点的单链表的操作:
//打印结点
void Print(Linklist head){
while (head!= NULL){
printf("%c", head->data);
head = head->next;
}
printf("\n");
}
//插入结点
Linklist Insert_Linklist(Linklist head, int loc, char elem){
Linklist l;
Linklist s = (Linklist)malloc(sizeof(Node));
s->data = elem;
s->next = NULL;
l = head;
int i = 0;
for (i = 1; i < loc - 1; i++){
l = l->next;
}
s->next = l->next;
l->next = s;
return l;
}
//删除结点
Linklist Delet_Linklist1(Linklist head, char elem){
Linklist l;
Linklist del = NULL;
l = head;
if (l->data == elem){
while (l->next != NULL){
l->data = l->next->data;
l->next = l->next->next;
}
}
else{
while (l->next->data != elem){
l = l->next;
}
del = l->next;
l->next = l->next->next;
free(del);
}
return l;
}