#include typedef struct node{ int data; struct node * next; }linklistnode; typedef struct { linklistnode *rear;//头指针 int length; }linklist; //创建表 linklist * createlist(linklist *); //初始化表 linklist * initlist(linklist *); //求表长 int getlength(linklist *); //插入 linklist * insertlist(linklist *,int ,int ); //前插 linklist * insertlistatfirst(linklist *,int ); //按值删除 linklist * deletelistbyvalue(linklist *,int ); //按序号删除 linklist * deletelistbyindex(linklist *,int ); //删除头节点 linklist * deletefirstnode(linklist *); //按值查找 linklistnode * searchbyvalue(linklist *,int ); //按值查找的节点的前一个节点 linklistnode * searchbyvalue2(linklist *,int ); //按序号查找 linklistnode * searchbyindex(linklist *,int ); //按序号查找2 linklistnode * searchbyindex2(linklist *list,int pos); //去重 linklistnode * deleterepeatvalue(linklist *,int ); linklistnode * deleterepeatvalue2(linklist *,int); //创建表函数 linklist * createlist(linklist *list){ list = (linklist *)malloc(sizeof(linklist)); printf("n创建表成功"); return list; } //初始化表函数 linklist * initlist(linklist *list){ if(list == null){ printf("n表未创建,请先创建再初始化!"); return null; } list->head = null; list->rear = null; list->length = 0; printf("n初始化表成功"); return list; } //后插函数 linklist * insertlistatlast(linklist *list,int value){ linklistnode *temp; if(list == null){ printf("n表不存在,请先创建并初始化"); return null; } temp = (linklistnode *)malloc(sizeof(linklistnode)); if(list->length == 0){ temp->data = value; list->rear = temp; temp->next = list->rear; }else{ temp->data = value; temp->next = list->rear->next; list->rear->next = temp; list->rear = temp; } list->length++; printf("n插入成功"); return list; } //随意插入函数 linklist *insertlist(linklist *list,int pos,int value){ linklistnode *temp = null,*p = null; linklistnode *newnode; if(list == null){ printf("n表不存在,请先创建并初始化"); return null; } if( pos < 1 || pos > list->length+1){ printf("n插入位置不正确"); return list; } if(pos == list->length+1){ list = insertlistatlast(list,value); return list; } temp = searchbyindex(list,pos); if(temp){ newnode = (linklistnode *)malloc(sizeof(linklistnode)); newnode->data = value; newnode->next = temp->next; temp->next = newnode; list->length++; } printf("n插入成功"); return list; } //按序号查找 linklistnode * searchbyindex(linklist *list,int pos){ if(list == null){ printf("n表不存在,请先创建并初始化"); return null; } if( pos < 1 || pos > list->length){ printf("n查找的位置不正确"); return null; } int cnt = 1; linklistnode *p; p = list->rear; while(cnt!=pos){ p=p->next; cnt++; } return p; } //按序号查找2 (返回前一个节点) linklistnode * searchbyindex2(linklist *list,int pos){ if(list == null){ printf("n表不存在,请先创建并初始化"); return null; } if( pos < 1 || pos > list->length){ printf("n查找的位置不正确"); return null; } int cnt = 0; linklistnode *p; p = list->rear; while(cnt!=pos){ p=p->next; cnt++; } return p; } //输出 void displaylist(linklist *list){ linklistnode *p; if(list == null){ printf("n表不存在,请先创建并初始化"); return ; } p = list->rear->next; int i = 0; while(ilength){ printf("t%d",p->data); p = p->next; i++; } return ; } //按值查找 linklistnode * searchbyvalue(linklist *list,int value){ linklistnode *p = null; if(list == null){ printf("n表不存在,请先创建并初始化"); return null; } p = list->rear; int cnt = 0; while(cnt < list->length){ if(p->data == value){ return p; }else{ p = p->next; cnt++; } } return null; } //删除链表第一个节点 linklist * deletefirstnode(linklist *list){ linklistnode *p = null,*q = null; if(list == null){ printf("n表不存在,请先创建并初始化"); return null; } q = list->rear->next; list->rear->next = q->next; free(q); list->length--; printf("n删除成功"); return list; } //按序号删除 linklist * deletelistbyindex(linklist *list,int pos){ linklistnode *p = null,*q = null; if(list == null){ printf("n表不存在,请先创建并初始化"); return null; } if(pos < 1 || pos > list->length){ printf("n删除位置不合法"); return list; } if(pos == 1){ q = list->rear->next; list->rear->next = q->next; free(q); list->length--; printf("n删除成功"); return list; } p = searchbyindex2(list,pos-1); if(p != null){ q = p->next; p->next = q->next; free(q); list->length--; printf("n删除成功"); } return list; } //按找值为value的节点的前一个节点 linklistnode * searchbyvalue2(linklist *list,int value){ linklistnode *p = null; if(list == null){ printf("n表不存在,请先创建并初始化"); return null; } p = list->rear; int cnt = 0; while(cntlength){ if(p->next->data == value){ return p; }else{ cnt++; p = p->next; } } return null; } //按值删除 linklist * deletelistbyvalue(linklist *list,int value){ linklistnode *p = null,*q = null;; if(list == null){ printf("n表不存在,请先创建并初始化"); return null; } p = list->rear; if(p->next->data == value){ list = deletefirstnode(list); return list; } p = searchbyvalue(list,value); if(p != null){ q = p->next; p->next = q->next; free(q); list->length--; printf("n删除成功"); return list; } return list; } //去重 linklistnode * deleterepeatvalue(linklist *list,int value){ linklistnode *p = null,*q = null;; if(list == null){ printf("n表不存在,请先创建并初始化"); return null; } p = list->rear->next; if(p->data == value){ list = deletefirstnode(list); return list; } p = searchbyvalue2(list,value); if(p != null){ q = p->next; p->next = q->next; free(q); list->length--; return list; } return list; } int main(int argc, char *argv[]) { linklistnode *w = null; int flag = 1; int choice; int value; int pos; linklistnode * tempnode = null; linklist * list = null; while(1){ printf("n------链表---------n"); printf("n------1.创建表-----n"); printf("n------2.初始化-----n"); printf("n------3.求表长-----n"); printf("n------4.尾部插入-------n"); printf("n------5.随意插入-------n"); printf("n------6.按值删除---n"); printf("n------7.按序号删除-n"); printf("n------8.按值查找---n"); printf("n------9.按序号查找-n"); printf("n------10.输出表-----n"); printf("n------11.去重-----n"); printf("n------0.退出-------n"); printf("n您的选择是:"); scanf("%d",&choice); switch(choice){ case 1: list = createlist(list); break; case 2: list = initlist(list); break; case 3: printf("表长为:%d",list->length); break; case 4: printf("n请输入要插入的值:"); scanf("%d",&value); list = insertlistatlast(list,value); break; case 5: printf("n请输入要插入的位置:"); scanf("%d",&pos); printf("n请输入要插入的值:"); scanf("%d",&value); list = insertlist(list,pos,value); break; case 6: printf("n请输入要删除的值:"); scanf("%d",&value); list = deletelistbyvalue(list,value); break; case 7: printf("n请输入要删除元素的位置:"); scanf("%d",&pos); list = deletelistbyindex(list,pos); break; case 8: printf("n请输入要查找的值:"); scanf("%d",&value); tempnode = searchbyvalue(list,value); if(tempnode == null){ printf("n查找失败"); }else{ printf("n该节点地址:%x",tempnode); printf("n该节点的值:%d",tempnode->data); } break; case 9: printf("n请输入要查找的位置:"); scanf("%d",&pos); tempnode = searchbyindex2(list,pos); if(tempnode == null){ printf("n查找不成功"); }else{ printf("n结点的值:%d",tempnode->data); printf("n结点的地址:%x",tempnode); } break; case 10: displaylist(list); break; case 11: printf("n请输入要去重的值:"); scanf("%d",&value); w = searchbyvalue(list,value); w->data = value+1; while(searchbyvalue(list,value) != null){ list = deleterepeatvalue(list,value); } w->data = value; printf("n去重成功!"); break; case 0: return 0; break; default: printf("n您的选择有误,请重新选择"); } } return 0; }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/607004.html