数据结构之链表操作

来源:互联网 发布:今日头条数据缓存策略 编辑:程序博客网 时间:2024/06/05 14:24


原理如下:


代码如下,如有疑问可加我Q 804705550  ,欢迎与大家交流讨论


#include<stdio.h>#include<stdlib.h>#include<malloc.h>#include<stdbool.h>struct inode{int val;struct inode *point;};struct inode *crate_link(void);bool insert_to_link(struct inode *head);bool add_to_link(struct inode *head);bool delete_from_link(struct inode *head);void traver_link(struct inode *head);void sort_link(struct inode *head);bool is_empty(struct inode *head);int length_for_link(struct inode *head);int main(){int num;struct inode *head = NULL;while(1){printf("************** link_list operate *******************\n\n");printf("1.create the link_list!\n");printf("2.intsert the date into link_list!\n");printf("3.add the inode to the last link_list!\n");printf("4.delete the inode from link_list!\n");printf("5.traversing the link_list!\n");printf("6.sort the link_list!\n");printf("7.quit the soft!\n\n");printf("please input operate number: ");scanf("%d",&num);printf("\n");switch(num){case 1:head = crate_link();break;case 2:insert_to_link(head);break;case 3: add_to_link(head);                                break;case 4:delete_from_link(head);break;case 5:traver_link(head);break;case 6:sort_link(head);break;case 7: exit(0);break;}}}struct inode *crate_link(void){struct inode *head,*end;int len,i,value;head = (struct inode *)malloc(sizeof(struct inode *));end = (struct inode *)malloc(sizeof(struct inode *));if(head == NULL){printf("Memory allocation failure!\n");exit(1);}end = head;end->point = NULL;printf("please input the length for link: ");scanf("%d",&len);for(i=0;i<len;i++){printf("please input the value to link: ");        scanf("%d",&value);struct inode *new_inode;new_inode = (struct inode *)malloc(sizeof(struct inode *));new_inode->val = value;new_inode->point = NULL;end->point = new_inode;end = new_inode; }printf("***creat link succeed! \n\n");return head;}bool insert_to_link(struct inode *head){int pos,i,len,value;struct inode *p=NULL;traver_link(head);len = length_for_link(head);printf("input the addr: ");scanf("%d",&pos);if(pos > len){printf("the addr over range!\n");return false;}printf("input the value: ");        scanf("%d",&value);for(i=0,p=head->point;i<pos-2;i++,p=p->point);struct inode *new=(struct inode *)malloc(sizeof(struct inode *));new->val = value;struct inode *temp;temp =p->point;p->point = new;new->point = temp;return true;}bool add_to_link(struct inode *head){int value,len,i;struct inode *p=NULL;struct inode *new=NULL;new = (struct inode *)malloc(sizeof(struct inode *));len = length_for_link(head);for(i=0,p=head->point;i<len-1;i++,p=p->point);printf("input the value: ");scanf("%d",&value);new->val = value;p->point = new;new->point = NULL;return true;}bool delete_from_link(struct inode *head){int pos,i,len;        struct inode *p=NULL;        len = length_for_link(head);        printf("input the addr: ");        scanf("%d",&pos);         if(pos > len)        {                printf("the addr over range!\n");                return false;        }         for(i=0,p=head->point;i<pos-2;i++,p=p->point);        struct inode *temp;        temp =p->point;        p->point = p->point->point;        free(temp);return true;}void traver_link(struct inode *head){struct inode *temp;int i;for(i=1,temp=head->point;temp!=NULL;i++,temp=temp->point)        {printf("link %d date: %d\n",i,temp->val);        }}void sort_link(struct inode *head){struct inode *p,*q;int len=length_for_link(head);int i,j,temp;for(i=0,p=head->point;i<len-1;i++,p=p->point){for(j=i+1,q=p->point;j<len;j++,q=q->point){if(p->val > q->val){temp = p->val;p->val = q->val;q->val = temp;}}}}bool is_empty(struct inode *head){if(head->point == NULL)return true;elsereturn false;}int length_for_link(struct inode *head){int length=0;struct inode *temp;for(temp=head->point;temp!=NULL;temp=temp->point){length++;}return length;}



0 0
原创粉丝点击