/* * 时间:2015年7月28日07:54:10 * 项目:单链表(头插法和尾插法) */ # include <stdio.h> typedef int ElemType; typedef struct Node{ Node *next; ElemType data; }LinkList; /*头插法,拥有头指针*/ void InitLinkListHead(LinkList *headList) { headList->next = NULL; headList->data = 0; /*代表线性表的长度*/ } //每次从头部进行插入,插入的时间复杂度o(1) void InsertHead(LinkList *headList,ElemType value) { Node *newnode = new Node; newnode->data = value; newnode->next = headList->next; headList->next = newnode; headList->data++; } //头指针删除元素 ElemType DeleteElem(LinkList *headList) { if(headList->data <= 0) { printf("链表中没有元素所以你不能删除\n"); return -1; } else { ElemType value = headList->next->data; headList->next = headList->next->next; headList->data--; return value; } } /*尾插入法,尾指针*/ //初始化表尾微针 void InitEndList(Node *end) { end->data = 0;//表示链表的长度 end->next = NULL; } //每次从尾部进行插入,插入时间为o(1) /*这里必须使用二级指针,原因是因为*endList = newnode,因为当链表长度为空时,链表必须先指向新的第一个元素 *如果不使用二级指针会使得原本的地址没有了指向,不理解建议看看二级指针的用法和意义。 */ void InsertEnd(LinkList **endList,ElemType value,Node *end) { Node *newnode = new Node; newnode->data = value; newnode->next = NULL; if(end->data == 0) { *endList = newnode; //让没有任何元素的链表先指向一个新的元素 end->next = newnode; //每次尾指针都是指向新的元素 } else { end->next->next = newnode; end->next = newnode; } end->data++; //插入一个元素就让其进行自加 } //尾插法删除数据 ElemType DeleteElemEnd(LinkList **endList,Node *end) { if(end->data == 0) { printf("链表中没有任何元素,无法进行插入"); return -1; } else { ElemType value; if(end->data == 1) { value = (*endList)->data; endList = NULL; } else { value = (*endList)->data; (*endList) = (*endList)->next; } end->data--; return value; } } /*可以自由选择位置进行插入,插入的时间复杂度o(n)*/ void InsertLocation(LinkList *headList,ElemType value,int location) { if(location > headList->data+1 || location <= 0) printf("很抱歉,你想要插入的位置是(%d),不符合实际情况\n",location); else { Node *newnode = new Node; newnode->data = value; Node *temp = headList; for(int i = 1;i < location;i++) temp = temp->next; newnode->next = temp->next; temp->next = newnode; headList->data++; } } //可以任意选择位置进行删除 void DeleteLocation(LinkList *headList,ElemType &value,int location) { if(location > headList->data+1 || location <= 0) printf("很抱歉,你想要插入的位置(%d)不符合实际情况\n",location); else { Node *newnode = headList; for(int i = 1;i < location;i++) newnode = newnode->next; value = newnode->next->data; newnode->next = newnode->next->next; headList->data--; } } /*头指针和位置插入法输出函数*/ void printList(LinkList *headList) { Node *temp = headList->next; int i = 0; while(temp) { if(i%10 == 0) printf("\n"); printf("%4d",temp->data); i++; temp = temp->next; } printf("\n"); } /*尾指针输出法*/ void printfEnd(LinkList *list,Node *end) { if(end->data == 0) printf("链表中没有任何元素\n"); else { LinkList *temp = list; for(int i = 0;i < end->data;i++) { if(i%10 == 0) printf("\n"); printf("%4d",temp->data); temp = temp->next; } } printf("\n"); } int main() { printf("头指针插入法:\n"); LinkList list; InitLinkListHead(&list); for(int i = 0;i < 50;i++) InsertHead(&list,i); printList(&list); printf("尾指针插入法:\n"); LinkList *linklistend; Node end; InitEndList(&end); for(int j = 0;j < 50;j++) InsertEnd(&linklistend,j,&end); printfEnd(linklistend,&end); printf("按照位置插入法:\n"); LinkList listLocation; InitLinkListHead(&listLocation); for(int z = 0;z < 50;z++) InsertLocation(&listLocation,z,z); printList(&listLocation); printf("进行删除操作:\n\n"); printf("1.头指针删除法:\n"); DeleteElem(&list); printList(&list); /*和尾插入法一样,必须巧用二级指针*/ printf("2.尾指针删除法:\n"); DeleteElemEnd(&linklistend,&end); printfEnd(linklistend,&end); printf("3.按照位置删除法:\n"); int value; DeleteLocation(&listLocation,value,4); printList(&listLocation); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-12 15:46:54