链表操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| #include <stdio.h> #include <stdlib.h>
#define ERROR 0 #define OK 1 #define Overflow 2 #define Underflow 3 #define Notpresent 4 #define Duplicate 5 typedef int Status; typedef int ElemType; typedef struct Node{ ElemType elem; struct Node *link; }Node;
typedef struct { struct Node* first; int n;
}SingleList;
SingleList list;
Status Init(SingleList *L){ L->first = NULL; L->n = 0; return OK; }
Status Find(SingleList L,int i,ElemType *x){ Node *p; int j; if (i<0 || i> L.n-1) return ERROR; p = L.first; for (j = 0; j < i; ++i) p=p->link; *x = p->elem; return OK; }
Status Insert(SingleList *L,int j,ElemType x){ Node *p,*q; int i; if(j<-1 || j> L->n) return ERROR; p = L->first; for(i=0;i<j;i++) p=p->link; q = (Node *)malloc(sizeof(Node)); q->elem = x; if (j>-1) { q->link = p->link; p->link = q ; }else { q->link = L->first; L->first = q; } L->n++; return OK; }
Status Delete(SingleList *L,int j){ int i; Node *p,*q; if(!L->n) return ERROR; if ( j<0 || j>L->n) return ERROR; q = L->first; p = L->first; for(i=0;i<j-1;i++) q = q->link; if (i==0) L->first = L->first->link; else{ p = q->link; q->link = p->link; } free(p); L->n -- ; return OK; }
Status Output(SingleList L){ Node *p; if(!L.n) return ERROR; p = L.first; while(p){ printf("%d ",p->elem ); p = p->link; } return OK; }
void Destory(SingleList *L){ Node *p; while(L->first){ p = L->first->link; free(L->first); L->first = p; } }
int main(){ int i,x;
Init(&list); for (i = 0; i < 9; ++i) Insert(&list,i-1,i); printf("the linked list is :"); Output(list);
Delete(&list,1); printf("\nafter deleting the list is:"); Output(list);
Find(list,0,&x); printf("\nthe value is %d\n",x );
Destory(&list);
system("pause"); return 0; }
|
带表头节点的单链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| #include <stdio.h> #include <stdlib.h>
#define ERROR 0 #define OK 1 #define Overflow 2 #define Underflow 3 #define Notpresent 4 #define Duplicate 5
typedef int ElemType; typedef int Status;
typedef struct Node{ ElemType elem; struct Node *link; }Node;
typedef struct { struct Node* head; int n; }Headlist;
Status Init(Headlist *L){ L->head = (Node*)malloc(sizeof(Node)); if(!L->head) return ERROR; L->head->link = NULL; L->n = 0; return OK; }
Status Sort(Headlist *L){ Node *p=L->head,*pre=NULL; Node *r=p->link; p->link = NULL; p=r; while(p != NULL){ r = p->link; pre = L->head; while(pre->link != NULL && pre->link->elem < p->elem) pre = pre->link; p->link = pre->link; pre->link = p; p=r; } return OK; }
Status deleleab(Headlist *L, int a,int b){ Node *q = L->head,*p=L->head->link; while( p ) if(p->elem >= a && p->elem <= b) { q->link = p->link; free(p); p = q->link; }else{ p = p->link; q = p->link; } return OK; }
Status Converse(Headlist *L){ Node *p = NULL,*cur= NULL; Node *q = L->head->link; if(L->head && L->head->link){ while( q != NULL ) { cur = q; q = q->link; L->head->link = cur; cur->link = p; p = cur; } }else return ERROR; return OK; }
Status Insert(Headlist *L,int j,ElemType x){ int i; Node *p=NULL,*q=NULL; if(j<-1 || j> L-> n-1) return ERROR; p = L->head; for(i=0;i<=j;i++) p=p->link; q = (Node *)malloc(sizeof(Node)); q->elem = x; q->link = p->link; p->link = q; L->n++; return OK; }
Status Output(Headlist L){ Node *p = L.head->link; if(!L.n) return ERROR; while(p){ printf("%d ",p->elem ); p = p->link; } return OK; }
Status Destory(Headlist *L){ Node *p=NULL; while(L->head){ p = L->head->link; free(L->head); L->head = p; } return OK; }
Status Delete(Headlist *L,int j){ Node *p = L->head,*q = L->head; int i; if(!L->n) return ERROR; if ( j<0 || j>L->n) return ERROR; for(i = 0 ;i<=j-1;i++) p = p->link; q = p; p = p->link; q->link = p->link; free(p); return OK; }
Status Find(Headlist *L,int j,ElemType *x){ Node *p= L->head; int i; if ( j<0 || j>L->n) return ERROR; for(i = 0 ;i<=j;i++) p = p->link; *x = p->elem; return OK; }
int main(){ int x; Headlist list; Init(&list); Insert(&list,-1,3); Insert(&list,0,2); Insert(&list,-1,5); Insert(&list,2,7); Insert(&list,-1,1); printf("the linked list is :"); Output(list); printf("\nAfter sorted:"); Sort(&list); Output(list); printf("\nAfter Conversed:"); Converse(&list); Output(list); printf("\nAfter delete index of 0,the list is:"); Delete(&list,0); Output(list);
Find(&list,2,&x); printf("\nthe index of 2:%d\n",x);
Destory(&list); system("pause"); return 0; }
|
带表头的链表和普通链表的区别在于:
- 带表头链表的头结点的数据域是不设置的,真正有用的结点是L->head->link指向的结点.而普通链表L->first指向的结点
- 这样的好处是不用特殊考虑是不是头结点.
代码实现细节:
1.插入的i,是ai后面再添加一项,所以for
条件为j=0;j<i
进行j次link
2.删除时,删除的是ai,for(j=0;j < i - 1;j++)
,为什么是 i-1
跟代码实现有关,先把q指向要删除的前一个结点,p=q->link,q->link = p->link
从而将p
即ai孤立出来
▲带表头的话,将<变为<=,因为要多推个link,跳过head结点
设计上最大的区别在于
▲多了个表头以后,就不用再考虑,删除和插入的时候去动List->first指针,带表头后,修改的都是L->head->link之后的结点Node
单链表实现多项式加减、相乘
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
| #include <stdio.h> #include <stdlib.h>
#define ERROR 0 #define OK 1 #define Overflow 2 #define Underflow 3 #define Notpresent 4 #define Duplicate 5
typedef int ElemType; typedef int Status;
typedef struct PNode { ElemType ceof; ElemType exp; struct PNode *link; }PNode;
typedef struct { struct PNode *head; }polynominal;
Status Init(polynominal *p){ p->head = (PNode *)malloc(sizeof(PNode)); p->head->exp = -1; p->head->link = NULL; return OK; }
Status Create(polynominal *p){ PNode *pn = NULL,*q=NULL,*pre=NULL; p->head = (PNode *)malloc(sizeof(PNode)); p->head->exp = -1; p->head->link = NULL; for (;;) { pn = (PNode *)malloc(sizeof(PNode)); printf("ceof:\n"); scanf("%d",&pn->ceof); printf("exp:\n"); scanf("%d",&pn->exp); if (pn->exp < 0) {printf("End the input\n"); break;} pre = p->head; q=p->head->link; while(q && q->exp > pn->exp){ pre = q; q = q->link; } pn->link = q; pre->link = pn; } return OK; }
Status Sort(polynominal *L){ PNode *p=L->head,*pre=NULL; PNode *r=p->link; p->link = NULL; p=r; while(p != NULL){ r = p->link; pre = L->head; while(pre->link != NULL && pre->link->exp < p->exp) pre = pre->link; p->link = pre->link; pre->link = p; p=r; } return OK; }
Status Add(polynominal *px,polynominal *qx){ PNode *q1=qx->head, *p=px->head->link; PNode *q=q1->link; PNode *temp = NULL; while( q && p){ while( p->exp < q->exp ){ q1 = q; q = q->link; } if (p->exp == q->exp ){ q->ceof = q->ceof + p->ceof; if (q->ceof == 0){ q1->link = q->link; free(q); q = q1->link; p = p->link; }else{ q1 = q; q = q->link; p = p->link; } }else{ temp = (PNode * )malloc(sizeof(PNode)); temp->ceof = p->ceof; temp->exp = p->exp; temp->link = q1->link; q1->link = temp; p = p->link; } } return OK;
}
void Output(polynominal *p){ PNode *q = p->head->link; int last = 0; while( q!=NULL ){ if(q->link == NULL) last =1; printf("%dx^%d", q->ceof,q->exp); if(!last) printf("+"); q = q->link; } printf("\n"); }
void Destory(polynominal *p){ PNode *q = NULL; while(p->head){ q = p->head->link; free(p->head); p->head = q; } }
Status unify(polynominal *t) { PNode *p=NULL; PNode *q=NULL; PNode *last=NULL; PNode *tmp; for(p=t->head->link;p!=NULL;p=p->link){ last = p; for(q=last->link; q!=NULL ; ){ if(q->exp == p->exp){ p->ceof += q->ceof; tmp = q->link; last->link = q->link; free(q); q= tmp; }else{ last= q; q=q->link;} } } return OK; }
polynominal Multiply(polynominal *px,polynominal *qx){ PNode *p = px->head; PNode *q = qx->head; PNode *x = NULL; PNode *tmp = NULL; polynominal newpoly; Init(&newpoly); x = newpoly.head; for (p=px->head->link; p!=NULL; p=p->link){ for (q=qx->head->link; q!=NULL; q=q->link){ tmp = (PNode*)malloc(sizeof(PNode)); tmp->ceof = p->ceof * q->ceof; tmp->exp = p->exp + q->exp; tmp->link = x->link; x->link = tmp; x = x->link; } } unify(&newpoly); Sort(&newpoly); return newpoly; }
int main() { polynominal p,q; polynominal mul; Create(&p); Output(&p); Create(&q); Output(&q);
printf("After Multiplied:\n"); mul = Multiply(&p,&q); Output(&mul);
Add(&p,&q); printf("After added:"); Output(&q); printf("\n"); system("pause"); return 0; }
|