C语言实现链表
/************************************************************************* > File Name: slink.c > Author: > Mail: > Created Time: Thu 25 Oct 2018 06:24:28 PM CST ************************************************************************/ #include<stdio.h> typedef int ElemType; typedef struct node { ElemType data; struct node *next; }slink; slink *creslink(int n) { slink *head,*p,*s; int i; p = head = (slink *)malloc(sizeof(slink)); for(i=1;i<=n;i++) { s = (slink *)malloc(sizeof(slink)); scanf("%d",&s->data); p->next = s; p = s; } p->next = NULL; return head; } int getlen(slink *head) { slink *p; int n = 0; p = head->next; while(p!=NULL) { n++; p = p->next; } return n; } int getelem(slink *head,int i,ElemType *e) { slink *p; int j; if(i<1) return 0; p = head->next; j = 1; while(p!=NULL && j<i) { p = p->next; j++; } if(p == NULL) return 0; *e = p->data; return 1; } int locate(slink *head,ElemType x) { int i = 1; slink *p = head->next; while(p!=NULL && x!=p->data) { p = p->next; i++; } if(p) return i; else return 0; } int delete(slink *head,int i,ElemType *e) { slink *p ,*q; int j = 0; p = head; if(i<1)return 0; while(p!=NULL && j<i-1) { p = p->next; j++; } if(p==NULL)return 0; q=p->next; p->next = q->next; *e = q->data; free(q); return 1; } int insert(slink *head,int i,ElemType x) { slink *p,*q; int j = 0; if(i<1) return 0; p = head; while(p!=NULL && j<i-1) { p = p->next; j++; } if(p==NULL) return 0; q = (slink *)malloc(sizeof(slink)); q->data = x; q->next = p->next; p->next = q; return 1; } void list(slink *head) { slink *p; p = head->next; while(p!=NULL) { printf("%4d",p->data); p = p->next; } printf("\n"); } void link(slink *a,slink *b) { slink *p; for(p=a;p->next!=NULL;p=p->next); p->next = b->next; free(b); } void merge(slink *la,slink *lb) { slink *pa,*pb,*pc; pa = la->next; pb = lb->next; pc = pa; while(pa!=NULL && pb!=NULL) { if(pa->data <= pb->data) { pc->next = pa; pa = pa->next; }else{ pc->next = pb; pb = pb->next; } pc = pc->next; } if(pa!=NULL) pc->next = pa; else pc->next = pb; free(lb); } /*************************************************************/ void bring(slink *la,slink *lb) { slink *l,*p,*q; l = lb->next; while(l!=NULL) { p = la; q = la->next; while(q!=NULL && q->data!=l->data) { p = q; q = q->next; } if(q!=NULL) { p->next = q->next; free(q); } l = l->next; } for(l=la;l->next!=NULL;l=l->next); l->next=lb->next; free(lb); } /********************************************************/ void turn(slink *head) { slink *p,*q; p = head->next; head->next = NULL; while(p!=NULL) { q = p->next; p->next = head->next; head->next = p; p = q; } } slink *divide(slink *head) { slink *odd,*r,*p,*q; odd = (slink *)malloc(sizeof(slink)); r = head->next; p = head; q = odd; while(r!=NULL) { if(r->data%2!=0) { p->next = r; p = r; }else{ q->next = r; q = q->next; } } q->next = p->next = NULL; return odd; } /****************************多项式**********************/ #include <stdio.h> typedef struct node { int c; int e; struct node *next; }dxs; dxs *creat(int m) { dxs *head,*p,*q; int i; p = head = (dxs *)malloc(sizeof(dxs)); //创建头结点 for(i=1;i<=m;i++) { q = (dxs *)malloc(sizeof(dxs)); scanf("%d%d",&q->c,&q->e); p->next = q; p = q; } p->next = NULL; return head; } plus(dxs *h1,dxs *h2) { dxs *h,*p1,*p2,*q; h = h1; p1 = h1->next; p2 = h2->next. while(p1!=NULL && p2!=NULL) if(p1->e > p2->e) { h->next = p1; h = p1; p1 = p1->next; }else if(p2->e > p1->e){ h->next = p2; h = p2; p2 = p2->next; }else if(p1->c + p2->c != 0){ p1->c = p1->c + p2->c; h->next = p1; h = p1; p1 = p1->next; q = p2; p2 = p2->next; free(q); }else{ q = p1; p1=p1->next;free(q); q = p2; p1=p1->next;free(q); } if(p1!=NULL)h->next = p1; else h->next = p2; free(h2); } void list(dxs *head) { dxs *p = head->next; while(p!=NULL) { printf("(%4d,%4d) ",p->c,p->e) p = p->next; } printf("\n"); }