双向链表:
func函数:
#include <stdio.h>
#include <stdlib.h>
#include "./double.h" //创建一个空的双向链表 doubleLinkList* create_doubleLinkList()
{ doubleLinkList* head = (doubleLinkList*)malloc(sizeof(doubleLinkList)); if(NULL==head) { printf("创建头结点失败,双向链表创建失败\n"); return NULL; } head->text.len=0; head->next = NULL; head->prev = NULL; return head;
} //头插法
void insertHead_doubleLinkList(doubleLinkList* head,dataType num)
{ doubleLinkList* temp=(doubleLinkList*)malloc(sizeof(doubleLinkList)); if(NULL==temp) { printf("双向链表创建失败\n"); return ; } temp->text.data = num; temp->next=NULL; temp->prev=NULL; //链表为空,链表中有数据的逻辑不一致 if(head->next==NULL) { temp->next = head->next; head->next = temp; temp->prev = head; } else { temp->next = head->next; head->next = temp; temp->next->prev = temp; temp->prev = head; } //更新头结点中链表的长度 head->text.len++; return;
} //遍历
void show_doubleLinkLit(doubleLinkList* head)
{ doubleLinkList* p=head; while(p->next != NULL) { p=p->next; printf("%d ",p->text.data); } printf("\n"); return;
} //判断链表是否为空
int isEmpty_doubleLinkList(doubleLinkList* head)
{ return head->next == NULL? 1:0;
} //尾插法
void insertTail_doubleLinkList(doubleLinkList* head,int num)
{ doubleLinkList* temp=(doubleLinkList*)malloc(sizeof(doubleLinkList)); if(NULL==temp) { printf("双向链表创建失败\n"); return ; } temp->text.data = num; temp->next=NULL; temp->prev=NULL; doubleLinkList* p =head; while(p->next!=NULL) { p=p->next; }
//该循环结束后,p指向最后一个结点 temp->next=NULL; p->next=temp; temp->prev=p; head->text.len++; return; } //按位置插入
void insertbyPos_doubleLinkList(doubleLinkList* head,int num,int pos)
{ doubleLinkList* temp=(doubleLinkList*)malloc(sizeof(doubleLinkList)); if(NULL==temp) { printf("双向链表创建失败\n"); return ; } temp->text.data = num; temp->next=NULL; temp->prev=NULL; doubleLinkList* p = head; for(int i=0;i<pos-1;i++) { p=p->next; } //在中间和在边上是两种插入方法 if(p->next!=NULL) { temp->next=p->next; p->next=temp; p->next->prev=temp; temp->prev=p; } else { temp->next=NULL; p->next=temp; temp->prev=p; } head->text.len++; return;
} //头删法
void deletebyhead_doubleLinkList(doubleLinkList* head)
{ if(isEmpty_doubleLinkList(head)==1) { printf("链表为空,不能删除\n"); return; } //删除有两种情况,一种只有两个,一种很多个 doubleLinkList* p = head->next; if(p->next!=NULL) { head->next=p->next; p->next->prev=head; } else { head->next=p->next; } free(p); return;
} //尾删法
void deletebyTail_doubleLinkList(doubleLinkList* head)
{ if(isEmpty_doubleLinkList(head)==1) { printf("链表为空,不能删除\n"); return; } //循环找到尾结点 doubleLinkList* p = head->next; while(p->next!=NULL)//该循环结束后,p指向最后一个结点 { p=p->next; } p->prev->next=NULL; free(p);
} //按位置删除
void deletebyPos_doubleLinkList(doubleLinkList* head,int pos)
{ if(isEmpty_doubleLinkList(head)==1) { printf("链表为空,不能删除\n"); return; } doubleLinkList* p=head; for(int i =0;i<pos;i++) { p=p->next; } if(p->next!=NULL) { p->prev->next=p->next; p->next->prev=p->prev; } else { p->prev->next=NULL; } free(p); return;
}
main.c函数:
#include <stdio.h>
#include "./double.h"int main(int argc, const char *argv[])
{doubleLinkList* head = create_doubleLinkList();insertHead_doubleLinkList(head,100);insertHead_doubleLinkList(head,100);insertHead_doubleLinkList(head,100);insertHead_doubleLinkList(head,100);show_doubleLinkLit(head);insertTail_doubleLinkList(head,200);insertTail_doubleLinkList(head,300);insertTail_doubleLinkList(head,400);insertTail_doubleLinkList(head,500);show_doubleLinkLit(head);insertbyPos_doubleLinkList(head,666,2);insertbyPos_doubleLinkList(head,666,6);show_doubleLinkLit(head);deletebyhead_doubleLinkList(head);deletebyhead_doubleLinkList(head);deletebyhead_doubleLinkList(head);show_doubleLinkLit(head);deletebyTail_doubleLinkList(head);deletebyTail_doubleLinkList(head);show_doubleLinkLit(head);deletebyPos_doubleLinkList(head,2);deletebyPos_doubleLinkList(head,4);show_doubleLinkLit(head);return 0;
}
.h函数:
#ifndef DOUBLE_H_#define DOUBLE_H_typedef int dataType;union msg{ dataType data;int len;};typedef struct node{ union msg text;struct node* next;struct node* prev;}doubleLinkList;doubleLinkList* create_doubleLinkList();void insertHead_doubleLinkList(doubleLinkList* head,dataType num);void show_doubleLinkLit(doubleLinkList* head); void insertTail_doubleLinkList(doubleLinkList* head,int num);void insertbyPos_doubleLinkList(doubleLinkList* head,int num,int pos);void deletebyhead_doubleLinkList(doubleLinkList* head);void deletebyTail_doubleLinkList(doubleLinkList* head);void deletebyPos_doubleLinkList(doubleLinkList* head,int pos); #endif
运行结果:
单向循环:
func.c:
#include <stdio.h>
#include <stdlib.h>
#include "./d221.h"
linkList* loop_LinkList()
{linkList* head=(linkList*)malloc(sizeof(linkList));if(head==NULL){printf("头结点创建失败\n");return NULL;}head->text.len=0;head->next=head;return head;
}//判断链表是否为空
int isEmpty_linkList(linkList* head)
{return head->next == head?1:0;
}
//遍历链表
void show_linkList(linkList* head)
{linkList* p = head;while(p->next != head){p=p->next;printf("%d ",p->text.data);}printf("\n");return;
}
//头插法
dataType insert_linkList(linkList* head,int num)
{linkList* temp = (linkList*)malloc(sizeof(linkList));if(temp==NULL){printf("结点创建失败\n");return -1;}temp->text.data=num;temp->next=NULL;temp->next=head->next;head->next=temp;head->text.len++;return 0;
}//尾插
void insertTail_linkList(linkList* head,int num)
{linkList* temp = (linkList*)malloc(sizeof(linkList));if(temp==NULL){printf("结点创建失败\n");return;}temp->text.data=num;temp->next=NULL;linkList* p = head;while(p->next != head){p=p->next;}temp->next=head;p->next=temp;head->text.len++;
}//头删
dataType deleteHead_linkList(linkList* head)
{//判断链表是否为空if(isEmpty_linkList(head)==1){printf("链表为空,无法删除\n");}linkList* temp = head->next;head->next = temp->next;dataType num = temp->text.data;free(temp);temp = NULL;head->text.len--;return num;
}//尾删
void deleteTail_linkList(linkList* head)
{//判断链表是否为空if(isEmpty_linkList(head)){printf("链表为空,无法删除\n");}linkList* temp = head;while(temp->next->next !=head){temp=temp->next;}free(temp->next);temp->next=head;head->text.len--;return ;
}//按位置插入
void insertbyPos_linkList(linkList* head,int num,int pos)
{linkList* temp = (linkList*)malloc(sizeof(linkList));if(temp==NULL){printf("结点创建失败\n");return;}temp->text.data=num;temp->next=NULL;linkList* p =head;for(int i=0;i<pos-1;i++){if(p->next==NULL){p->next=temp;temp->next=head;}p=p->next;}temp->next=p->next;temp->text.data=num;p->next=temp;head->text.len++;return;
}//按位置删除
void deletebyPos_linkList(linkList* head,int pos)
{//判断链表是否为空if(isEmpty_linkList(head)){printf("链表为空,无法删除\n");}linkList* p = head;for(int i=0;i<pos-1;i++){if(p->next==head){p->next=head;}p=p->next;}linkList* q=p->next;p->next=q->next;q=NULL;free(q);head->text.len--;return ;
}
main.c:
#include <stdio.h>
#include "./d221.h"int main(int argc, const char *argv[])
{linkList* head = loop_LinkList();insert_linkList(head,10);insert_linkList(head,20);insert_linkList(head,30);show_linkList(head);insertTail_linkList(head,3);insertTail_linkList(head,4);insertTail_linkList(head,5);insertTail_linkList(head,6);show_linkList(head);deleteHead_linkList(head);deleteHead_linkList(head);deleteHead_linkList(head);show_linkList(head);deleteTail_linkList(head);deleteTail_linkList(head);show_linkList(head);insertbyPos_linkList(head,66,1);insertbyPos_linkList(head,77,3);insertbyPos_linkList(head,88,4);show_linkList(head);deletebyPos_linkList(head,1);show_linkList(head);return 0;
}
.h函数:
#ifndef LINK_H_
#define LINK_H_
typedef int dataType;union msg{dataType data;int len;
};typedef struct node{union msg text;struct node* next;
}linkList;linkList* loop_LinkList();
dataType insert_linkList(linkList* head,int num);
void show_linkList(linkList* head);
void insertTail_linkList(linkList* head,int num);
dataType deleteHead_linkList(linkList* head);
void deleteTail_linkList(linkList* head);
void insertbyPos_linkList(linkList* head,int num,int pos);
void deletebyPos_linkList(linkList* head,int pos);#endif