[C语言链表操作]包括链表的创建删除添加和释放操作!! #include#includestruct node *create(); void print_list(struct node *head); struct node * insert_node(struct node *h,int x,int y...+阅读
#include#includestruct person { char name[10]; int number; person *next; person *last; }; person *p,*q,*start=NULL,*end; void insert(int num,char nam[10])//插入函数,按姓名的首字母顺序插入链表中的。 { q=new person; if (start==NULL) //判断start是否为空 若空则新建 { start=q; end=q; p=q; start->last=NULL; end->next=NULL; } else { if(strcmp(nam,start->name)last=q; q->next=start; q->last=NULL; start=q; } else if (strcmp(nam,end->name)>0) //插入表尾部 { end->next=q; q->last=end; end=q; q->next=NULL; } else if (strcmp(nam,end->name)name)>0)//插入链表中 { for (p=start;strcmp(nam,p->name)>0;p=p->next); q->next=p; p->last=q; for (p=start;p->next!=q->next;p=p->next); p->next=q; q->last=p; } } strcpy(q->name,nam); q->number=num; } void find(int num) //按编号查找 { if(start==NULL) { printf("无记录\n"); } else { for(p=start;p!=NULL&p->number!=num;p=p->next); if(p==NULL) { printf("不存在的编号!\n"); } else if (p->number==num) { printf("您查找的编号是:%d\n",num); printf("该生的姓名为:%s",p->name); } } } void del(int num) //按编号删除 { for(p=start;p->number!=num;p=p->next); if (p->number==num) { if (p->next==NULL) { if(p->last==NULL) { start=NULL; } else { (p->last)->next=NULL; } delete p; } else if (p->last==NULL) { (p->next)->last=NULL; start = p->next; delete p; } else if(p->last!=NULL&p->next!=NULL) { (p->last)->next=p->next; (p->next)->last=p->last; delete p; } } else { printf("不存在的编号!\n"); } } void show() { printf("学号\t姓名\n"); for (p=start;;p=p->next) { printf("%d\t%s\n",p->number,p->name); if (p->next==NULL) break; } } void main() { int i,num; char nam[10]; printf("输入三位学生信息(学号,姓名):"); for(i=0;i
c语言双向链
#include"stdio.h"#include"malloc.h"#define NULL 0struct node{ char name[20]; struct node *next,*prior; //定义结构结点指针};struct node *creat(int n){ struct node *h,*p,*s; int i; h=(struct node*)malloc(sizeof(struct node)); h->name[0]='\0'; h->prior=NULL; /*此处相当于建立一个空节点h,初始化双向链表头结点*/ h->next=NULL; p=h; for(i=0;inext=s; // 让指针p所指结点指向s结点,即h结点的下一结点是s printf("Input the %d student's name:",i+1); scanf("%s",s->name); s->prior=p;/*结点s与指针p所指结点建立连接,s的前一节点是指针p所指结点*/ s->next=NULL; //s指向空节点,即s是尾结点 p=s; //让指针p指向向结点s,此时指针p所指结点编出s } p->next=NULL; return h;}struct node *search(struct node *h,char*x){ struct node *p; char *y; p=h->next; while(p) { y=p->name; if(strcmp(y,x)==0)return p; else p=p->next; } printf("cannot find datd!\n");}void del(struct node *p){ /*删除指针p指向的结点*/ p->next->prior=p->prior;//指针p所指结点的下一结点的头结点,指向指针p所指结点的前一节点 p->prior->next=p->next; // 指针p所指结点的前一节点指向,指针p所指结点的后一节点 //这两步实现将p所指结点,排除到双向链表之外 free(p); //删除p所指结点}main(){ int number; char sname[20]; struct node *head,*sp; puts("Please in put the size of the list;"); scanf("%d",&number); head=creat(number); sp=head->next; printf("\nNow the double list is:\n"); while(sp) { printf(" %s",&*(sp->name)); sp=sp->next; } printf("\nPlease input the name which you want to find:\n"); scanf("%s",sname); sp=search(head,sname); printf("the name you want to find is: %s\n",*&sp->name); del(sp); sp=head->next; printf("\nNow the double list is:\n"); while(sp) { printf(" %s",&*(sp->name)); sp=sp->next; } printf("\n"); puts("\n Press any key to quit..."); getch();}
双向链表的排序用c语言编写程序
#include struct node{ int data; struct node* next; struct node* prev;};struct node* create_list(int n){ struct node *head = null; struct node *p = null, *q = null; int i = 0, data = 0; for (i = 0; inext = null; p->data = data; if (i == 0) { head = p; p->prev = null; } else { p->prev = q; q->next = p; } q = p; } return head;}void free_list(struct node* head){ struct node* p = head; struct node* q = null; while (p != null) { q = p; p = p->next; delete q; }}void display_list(struct node* head){ struct node* p = head; while (p != null) { printf("%d ", p->data); p = p->next; } printf("\n");}struct node* get_max_node(struct node* node){ struct node* max_node = node; while (node != null) { if (node->data >max_node->data) { max_node = node; } node = node->next; } return max_node;}void swap_node(struct node* node1, struct node* node2){ int temp; if (node1 == node2) { return; } temp = node1->data; node1->data = node2->data; node2->data = temp;}void sort_list(struct node* head){ struct node* max_node = null; struct node* current = null; current = head; while (current != null) { max_node = get_max_node(current); swap_node(current, max_node); current = current->next; }}int main(){ struct node *head = null; int n = 0; printf("请输入双向链表的结点个数:"); scanf("%d", &n); head = create_list(n); display_list(head); sort_list(head); display_list(head); free_list(head); return 0;}
帮忙做道C语言的题:双向链表的排序
#includetypedef struct Link/*双向链表结构体*/{ int data; struct Link *lift; struct Link *right;}linkx,*linky;linky Init();/*建立双向链表*/void PrLink(linky p);/*输出双向链表*/linky Sort(linky head);/*对双向链表排序*/linky Swap(linky head,linky one,linky two);/*任意交换双向链表两个结点的地址*/void main(void){ linky head; head=Init(); head=Sort(head); PrLink(head);}linky Init()/*建立链表*/{ linky p,q,head; int n=0; head=p=q=(linky)malloc(sizeof(linkx)); clrscr(); printf("please input 10 num: "); scanf("%d",&p->data);/*输入数据*/ head->lift=NULL; n++; while(n!=10)/*一直输入到规定的数字个数停止*/ { q=p; p=(linky)malloc(sizeof(linkx)); scanf("%d",&p->data);/*输入数据*/ q->right=p; p->lift=q; n++; } p->right=NULL; return(head);}linky Swap(linky head,linky one,linky two)/*任意交换两个结点*/{linky temp; if(one->lift==NULL&two->right==NULL)/*首和尾巴的交换*/ { if(one->right==two)/*只有两个结点的情况下*/ { two->right=one; two->lift=NULL; one->lift=two; one->right=NULL; head=two; } else/*有间隔的首尾交换*/ { one->right->lift=two; two->lift->right=one; two->right=one->right; one->lift=two->lift; two->lift=one->right=NULL; head=two;/*尾结点成为头结点*/ } } else if(two->right==NULL)/*尾和任意一个交换*/ { if(one->right==two)/*交换最后两个结点*/ { one->lift->right=two; two->lift=one->lift; two->right=one; one->lift=two; one->right=NULL; } else/*和前面其他结点交换*/ { temp=two->lift; temp->right=one; one->lift->right=two; one->right->lift=two; two->lift=one->lift; two->right=one->right; one->lift=temp; one->right=NULL; } } else if(one->lift==NULL)/*头和任意一个交换*/ { if(one->right==two)/*交换头两个结点*/ { two->right->lift=one; one->right=two->right; one->lift=two; two->right=one; two->lift=NULL; head=two; } else/*头结点和后面其他结点交换*/ { temp=one->right; temp->lift=two; one->lift=two->lift; one->right=two->right; two->lift->right=one; two->right->lift=one; two->right=temp; two->lift=NULL; head=two;/*交换的结点成为头结点*/ } } else/*当中的任意两个交换*/ { if(one->right==two)/*交换连在一起的两个结点*/ { temp=one->lift; one->lift->right=two; one->right->lift=two; one->lift=two; one->right=two->right; two->right->lift=one; two->right=one; two->lift=temp; } else/*交换隔开的两个结点*/ { one->lift->right=two; one->right->lift=two; one->lift=two->lift; temp=one->right; one->right=two->right; two->lift->right=one; two->right->lift=one; two->right=temp; two->lift=one->lift; } } return(head);}linky Sort(linky head)/*对链表排序*/{ linky i,j,t,p; int max; p=head; for(i=p;i->right!=NULL;i=i->right)/*用选择法的思想对这些结点排序*/ { max=i->data; for(j=i->right;j!=NULL;j=j->right) if(j->datadata; t=j; } if(max!=i->data)/*如果没有找到比i小的结点*/ { head=Swap(head,i,t);/*因为最终返回的是头结点,而头结点又有可能变化,所以每次头结点返回*/ i=t; } } return(head);}void PrLink(linky p)/*输出链表*/{ linky q; printf("Now the link: "); do { q=p; printf("%d ",p->data); p=p->right; free(q);/*释放输出结点*/ } while(p!=NULL); getch();}
以下为关联文档:
求助:c语言建立二叉链表问题出在参数传递上,在主函数中你将root传递到函数CreateBiTree中,形参bt等于root的值,在CreateBiTree中你给bt申请了空间,但是这只是将你申请的空间的指针赋给了形参,主函数中的...
帮忙做道C语言的题双向链表的排序#includetypedef struct Link/*双向链表结构体*/{ int data; struct Link *lift; struct Link *right;}linkx,*linky;linky Init();/*建立双向链表*/void PrLink(linky p);/*输...
求高手做个c语言设计一个双向链表的排序#includetypedef struct Link/*双向链表结构体*/ { int data; struct Link *lift; struct Link *right; }linkx,*linky; linky Init();/*建立双向链表*/ void PrLink(linky p)...
c语言课程设计双向链表排序#includetypedef struct Link/*双向链表结构体*/{ int data; struct Link *lift; struct Link *right;}linkx,*linky;linky Init();/*建立双向链表*/void PrLink(linky p);/*输...
关于C语言单向链表SLIST *creatlist(int *a){ SLIST *h,*p,*q; int i;h=p=(SLIST *)malloc(sizeof(SLIST));for(i=0; i<N; i++){ q=(SLIST *)malloc(sizeof(SLIST));q->data=a[i]; p->next=q;...
c语言创建单向链表函数void inputinfo(stu_info **head,int n) { int i=1; stu_info *loc_head=NULL,*tail; loc_head=(stu_info *)malloc(sizeof(stu_info)); tail=loc_head; for(i=1;i<=n;i++)...
C语言单向链表问题链表只要用head指针指示,是按从左到又读吗? head指针就是链表的头指针,只要有这个头指针,整个链表中的数据就可以访问; 但,只是单个方向的,因为结点中的next指针只保存下个结点的指...
链表的问题 C语言#include <stdio.h>;/*供参考*/ #include <malloc.h> #include <string.h> #define NULL 0 #define LEN sizeof(struct linkman) struct linkman //联系人结构体 { char na...
C语言链表问题我给你改,留个联系方式好给你。现在公司活比较忙。 给你改了一下,感觉你的链表好象设计的时候就有问题 你看看我给你改的吧. #include#include #define NULL 0 #define LEN si...