双链表增删改查
# include <stdio.h>
# include <stdlib.h>
typedef struct DNode { int data; struct DNode * prev; struct DNode * next;
} DNode;
DNode * createDoublyLinkedList ( ) { int n, i; printf ( "请输入双链表的长度:" ) ; scanf ( "%d" , & n) ; if ( n <= 0 ) { printf ( "输入的长度无效!\n" ) ; return NULL ; } DNode * head = NULL ; DNode * tail = NULL ; for ( i = 0 ; i < n; i++ ) { int data; printf ( "请输入第 %d 个结点的数据:" , i+ 1 ) ; scanf ( "%d" , & data) ; DNode * newNode = ( DNode* ) malloc ( sizeof ( DNode) ) ; newNode-> data = data; newNode-> prev = NULL ; newNode-> next = NULL ; if ( head == NULL ) { head = newNode; tail = newNode; } else { tail-> next = newNode; newNode-> prev = tail; tail = newNode; } } return head;
}
void traverseDoublyLinkedList ( DNode * head) { if ( head == NULL ) { printf ( "双链表为空!\n" ) ; return ; } DNode * current = head; printf ( "双链表的数据为:" ) ; while ( current != NULL ) { printf ( "%d " , current-> data) ; current = current-> next; } printf ( "\n" ) ;
}
void insertNode ( DNode * * head, int position, int data) { if ( * head == NULL ) { printf ( "双链表为空!\n" ) ; return ; } DNode * current = * head; int currentPosition = 1 ; while ( currentPosition < position && current-> next != NULL ) { current = current-> next; currentPosition++ ; } if ( currentPosition < position) { printf ( "插入位置无效!\n" ) ; return ; } DNode * newNode = ( DNode* ) malloc ( sizeof ( DNode) ) ; newNode-> data = data; newNode-> prev = NULL ; newNode-> next = NULL ; if ( currentPosition == 1 ) { newNode-> next = * head; ( * head) -> prev = newNode; * head = newNode; } else { newNode-> next = current; newNode-> prev = current-> prev; current-> prev-> next = newNode; current-> prev = newNode; } printf ( "成功插入结点!\n" ) ;
}
void deleteNode ( DNode * * head, int position) { if ( * head == NULL ) { printf ( "双链表为空!\n" ) ; return ; } DNode * current = * head; int currentPosition = 1 ; while ( currentPosition < position && current != NULL ) { current = current-> next; currentPosition++ ; } if ( current == NULL ) { printf ( "删除位置无效!\n" ) ; return ; } if ( currentPosition == 1 ) { * head = ( * head) -> next; if ( * head != NULL ) { ( * head) -> prev = NULL ; } } else { current-> prev-> next = current-> next; if ( current-> next != NULL ) { current-> next-> prev = current-> prev; } } free ( current) ; printf ( "成功删除结点!\n" ) ;
}
DNode * searchNode ( DNode * head, int data) { if ( head == NULL ) { printf ( "双链表为空!\n" ) ; return NULL ; } DNode * current = head; while ( current != NULL ) { if ( current-> data == data) { return current; } current = current-> next; } printf ( "未找到指定结点!\n" ) ; return NULL ;
}
int get_node_value ( DNode* head, int pos) { if ( head == NULL ) { printf ( "链表为空,无法查找!\n" ) ; return - 1 ; } int count = 0 ; DNode* current = head; while ( current != NULL ) { if ( count == pos) { return current-> data; } count++ ; current = current-> next; } printf ( "指定位置不存在节点!\n" ) ; return - 1 ;
}
int get_node_count ( DNode* head) { int count = 0 ; DNode* current = head; while ( current != NULL ) { count++ ; current = current-> next; } return count;
}
void print_list ( DNode* head) { DNode* current = head; while ( current != NULL ) { printf ( "%d " , current-> data) ; current = current-> next; } printf ( "\n" ) ;
}
DNode* create_node ( int data) { DNode* newNode = ( DNode* ) malloc ( sizeof ( DNode) ) ; newNode-> data = data; newNode-> next = NULL ; return newNode;
}
void append_node ( DNode* * head, int data) { DNode* newNode = create_node ( data) ; if ( * head == NULL ) { * head = newNode; } else { DNode* current = * head; while ( current-> next != NULL ) { current = current-> next; } current-> next = newNode; }
}
void freeDoublyLinkedList ( DNode * * head) { DNode * current = * head; while ( current != NULL ) { DNode * temp = current; current = current-> next; free ( temp) ; } * head = NULL ; printf ( "成功释放双链表的内存!\n" ) ;
} int main ( ) { DNode * head = NULL ; head = createDoublyLinkedList ( ) ; traverseDoublyLinkedList ( head) ; int insertPosition, insertData; printf ( "请输入要插入的位置和数据(以空格分隔):" ) ; scanf ( "%d %d" , & insertPosition, & insertData) ; insertNode ( & head, insertPosition, insertData) ; traverseDoublyLinkedList ( head) ; int deletePosition; printf ( "请输入要删除的位置:" ) ; scanf ( "%d" , & deletePosition) ; deleteNode ( & head, deletePosition) ; traverseDoublyLinkedList ( head) ; int searchData; printf ( "请输入要查找的值:" ) ; scanf ( "%d" , & searchData) ; DNode * searchResult = searchNode ( head, searchData) ; if ( searchResult != NULL ) { printf ( "成功找到结点!" ) ; } int position; printf ( "请输入要查找的位置:" ) ; scanf ( "%d" , & position) ; int value = get_node_value ( head, position) ; if ( value == - 1 ) { printf ( "位置超出链表范围!\n" ) ; } else { printf ( "位置 %d 的值为:%d\n" , position, value) ; } int count = get_node_count ( head) ; printf ( "链表中共有 %d 个数据节点\n" , count) ; append_node ( & head, 30 ) ; print_list ( head) ; freeDoublyLinkedList ( & head) ; return 0 ;
}
链表带功能函数
# include <stdio.h>
# include <stdlib.h> typedef struct DNode { int data; void ( * Function) ( const char * ) ; struct DNode * prev; struct DNode * next;
} DNode;
DNode* createNode ( int data, void ( * function) ( const char * ) ) { DNode* node = ( DNode* ) malloc ( sizeof ( DNode) ) ; node-> data = data; node-> Function = function; node-> prev = NULL ; node-> next = NULL ; return node;
}
void insertNode ( DNode* * head, DNode* node) { if ( * head == NULL ) { * head = node; } else { DNode* current = * head; while ( current-> next != NULL ) { current = current-> next; } current-> next = node; node-> prev = current; }
}
void executeListFunction ( DNode* head, const char * param) { DNode* current = head; while ( current != NULL ) { if ( current-> Function != NULL ) { current-> Function ( param) ; } current = current-> next; }
} void executeListFunctions ( DNode* head, int searchData, const char * param) { DNode* current = head; while ( current != NULL ) { if ( current-> data == searchData && current-> Function != NULL ) { current-> Function ( param) ; } current = current-> next; }
}
void function1 ( const char * param) { printf ( "执行功能函数1,参数:%s\n" , param) ;
} void function2 ( const char * param) { printf ( "执行功能函数2,参数:%s\n" , param) ;
} int main ( ) { DNode* node1 = createNode ( 1 , function1) ; DNode* node2 = createNode ( 2 , function2) ; DNode* head = NULL ; insertNode ( & head, node1) ; insertNode ( & head, node2) ; executeListFunctions ( head, 1 , "参数" ) ; free ( node1) ; free ( node2) ; return 0 ;
}