main函數部分:
#include <stdio.h>
#include "./23_doubleLinkList.h"
int main(int argc, const char *argv[])
{ doubleLinkList* head = create_doubleLinkList();insertHead_doubleLinkList(head,12);insertHead_doubleLinkList(head,21);insertHead_doubleLinkList(head,14);insertHead_doubleLinkList(head,16);insertTail_doubleLinkList(head,13);show_doubleLinkList(head);insertBypos_doubleLinkList(head,33,6);show_doubleLinkList(head);deleteHead_doubleLinkList(head);show_doubleLinkList(head);deleteTail_doubleLinkList(head);show_doubleLinkList(head);deleteBypos_doubleLinkList(head,2);show_doubleLinkList(head); return 0;
}
功能函數:
#include<stdio.h>
#include <stdlib.h>
#include "./23_doubleLinkList.h"
doubleLinkList* create_doubleLinkList()
{ doubleLinkList* head = (doubleLinkList*)malloc(sizeof(doubleLinkList)); if(NULL==head) { printf("頭結點創建失敗!\n"); return NULL; } head->text.len=0; head->next=head->prev=NULL; return head;
}
//判斷鏈表是否為空
int isEmpty(doubleLinkList* head)
{ return head->next==NULL?1:0;
}
//頭插
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=temp->prev=NULL; //插入操作 //需分情況考慮該鏈表是否有元素,以免出現段錯誤 if(head->next!=NULL) { temp->next = head->next; head->next=temp; temp->prev=head; temp->next->prev=temp; }else { temp->next = head->next; head->next=temp; temp->prev=head; } head->text.len++; return; }
//尾插
void insertTail_doubleLinkList(doubleLinkList* head,datatype num)
{ //定義一個新的結點 doubleLinkList* temp=(doubleLinkList*)malloc(sizeof(doubleLinkList)); if(NULL == temp) { printf("頭結點創建失敗!\n"); return; } temp->text.data=num; temp->next=temp->prev=NULL; //找到尾部的結點,p就是原雙向鏈表的尾部結點 doubleLinkList* p=head; while(p->next!=NULL) { p=p->next; } p->next=temp; temp->next=NULL; temp->prev=p; head->text.len++; return;
}
//按位置插入
void insertBypos_doubleLinkList(doubleLinkList* head,datatype num,int pos)
{ if(pos<1 || pos>head->text.len+1) { printf("輸入的位置%d不合法!\n",pos); } //定義一個新的結點 doubleLinkList* temp=(doubleLinkList*)malloc(sizeof(doubleLinkList)); if(NULL == temp) { printf("頭結點創建失敗!\n"); return; } temp->text.data=num; temp->next=temp->prev=NULL; //找到要插入位置的前一個`結點 doubleLinkList* p=head; int i; for(i=0;i<pos-1;i++) { p=p->next; } temp->next=p->next; p->next=temp; temp->prev=p; if(temp->next!=NULL) temp->next->prev=temp; head->text.len++; return;
}
//頭刪
void deleteHead_doubleLinkList(doubleLinkList* head)
{ if(head->next==NULL) { printf("鏈表為空!\n"); return; } //定義一個指針指向要刪除的結點方便后續釋放 doubleLinkList* temp; temp=head->next; head->next=temp->next; //需分情況考慮該結點是否為鏈表最后一個結點,以免出現段錯誤 if(temp->next!=NULL) { temp->next->prev=head; } free(temp); head->text.len--; return;
}
//尾刪
void deleteTail_doubleLinkList(doubleLinkList* head)
{ if(head->next==NULL) { printf("鏈表為空!\n"); return; } //定義一個指針指向要刪除的結點方便后續釋放 doubleLinkList* p=head; while(p->next!=NULL) { p=p->next; } p->prev->next=NULL; free(p); head->text.len--; return; }
//按位置刪除
void deleteBypos_doubleLinkList(doubleLinkList* head,int pos)
{ if(head->next==NULL) { printf("鏈表為空!\n"); return; } //判斷位置是否合法 if(pos<1 || pos>head->text.len) { printf("輸入的位置%d不合法!\n",pos); } //定義一個指針指向要刪除的結點方便后續釋放 doubleLinkList* p=head; int i; for(i=0;i<pos;i++) { p=p->next; } //p就是要刪除的結點 p->prev->next=p->next; //當結點不為最后一個節點時,需要將該結點的后一位的前驅指針指向上一個結點 //當結點為最后一個節點則不需要,若不分情況考慮則會出現段錯誤; if(p->next!=NULL) { p->next->prev=p->prev; } free(p); head->text.len--;
}
//遍歷
void show_doubleLinkList(doubleLinkList* head)
{ doubleLinkList* p=head; if(isEmpty(head)) { printf("鏈表為空!\n"); } while(p->next!=NULL) { p=p->next; printf("%d ",p->text.data); } printf("\n"); return;
}
頭文件:
#ifndef __doubleLinkList_H__
#define __doubleLinkList_H__
typedef int datatype;
union msg{ //若數據的類型也為int,則不需要這個聯合體
datatype data;
int len; //放頭結點,記錄鏈表長度
};
typedef struct node{union msg text;struct node* next; //指針,由于指針指向這一整個節點,所以類型為struct node*struct node* prev;
}doubleLinkList; doubleLinkList* create_doubleLinkList();void insertHead_doubleLinkList(doubleLinkList* head,datatype num);void insertTail_doubleLinkList(doubleLinkList* head,datatype num);void deleteBypos_doubleLinkList(doubleLinkList* head,int pos);
void deleteTail_doubleLinkList(doubleLinkList* head);
void deleteHead_doubleLinkList(doubleLinkList* head);
void insertBypos_doubleLinkList(doubleLinkList* head,datatype num,int pos);
void show_doubleLinkList(doubleLinkList* head);
#endif