线性表:1.有限的序列⒉.序列中的每一个元素都有唯一的前驱和后继,除了开头和结尾两个节点
顺序表:分配一块连续的内存去存放这些元素,例如编程语言中的数组
链表:内存是不连续的,元素会各自被分配一块内存,内存和内存之间用指针进行相连
1、单链表
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h> //开辟空间typedef struct Node {int data;struct Node* next;
}Node;//带头结点的初始化单链表
Node* initList() {Node* list = (Node*)malloc(sizeof(Node));list->data = 0;list->next = NULL;return list;
}//头插法
void headInsert(Node* list, int data) {Node* node = (Node*)malloc(sizeof(Node));node->data = data;node->next = list->next;list->next = node;list->data++;
}//尾插法
void tailInsert(Node* list, int data) {Node* head = list;Node* node = (Node*)malloc(sizeof(Node));node->data = data;node->next = NULL;list = list->next;while (list->next) {list = list->next;} //找到最后的结点list->next = node;head->data++;
}//删除
void delete(Node* list, int data) {Node* pre = list;Node* current = list->next;while (current) {if (current->data == data) {pre->next = current->next;free(current);break;}pre = current;current = current->next;}list->data--;
}void printList(Node* list) {list = list->next;while (list) {printf("%d ", list->data);list = list->next;}printf("\n");
}int main() {Node* list = initList();headInsert(list, 1);headInsert(list, 2);headInsert(list, 3);printList(list);tailInsert(list, 4);tailInsert(list, 5);tailInsert(list, 6);printList(list);delete(list, 3);printList(list);return 0;
}
这里说一下,头插尾插的时候,都当作有很多的时候插入,最后处理开始会好很多
2、单循环链表
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h> //开辟空间typedef struct Node {int data;struct Node* next;
}Node;Node* initList() {Node* L = (Node*)malloc(sizeof(Node));L->data = 0;L->next = L;return L;
}//头插法
void headInsert(Node* L, int data) {Node* node = (Node*)malloc(sizeof(Node));node->data = data;node->next = L->next;L->next = node;L->data++;
}//尾插法
void tailInsert(Node* L, int data) {Node* n = L;Node* node = (Node*)malloc(sizeof(Node));node->data = data;while (n->next != L) {n = n->next;}node->next = L;n->next = node;L->data++;
}//删除
int delete(Node* L, int data) {Node* pre = L;Node* node = L->next;while (node != L) {if (node->data == data) {pre->next = node->next;free(node);L->data--;return 1;}pre = node;node = node->next;}return 0;
}void printList(Node* L) {Node* node = L->next;while (node != L) {printf("%d ", node->data);node = node->next;}printf("NULL\n");
}int main() {Node* L = initList();headInsert(L, 1);headInsert(L, 2);headInsert(L, 3);printList(L);tailInsert(L, 4);tailInsert(L, 5);tailInsert(L, 6);printList(L);delete(L, 3);printList(L);return 0;
}
3、双链表
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h> //开辟空间typedef struct Node {int data;struct Node* pre;struct Node* next;
}Node;//带头结点
Node* initList() {Node* L = (Node*)malloc(sizeof(Node));L->data = 0;L->pre = NULL;L->next = NULL;return L;
}void headInsert(Node* L, int data) {Node* node = (Node*)malloc(sizeof(Node));node->data = data;if (L->data == 0) {//链表为空node->next = L->next; //NULLnode->pre = L;L->next = node;L->data++;}else {node->pre = L;node->next = L->next;L->next->pre = node;L->next = node;L->data++;}
}void tailInsert(Node* L, int data) {Node* node = L;Node* n = (Node*)malloc(sizeof(Node));n->data = data;while (node->next) {node = node->next;} //找到最后一个n->next = node->next; //NULLnode->next = n;n->pre = node;L->data++;
}int delete(Node* L, int data) {Node* node = L->next;while (node) {if (node->data == data && node->next != NULL) {node->pre->next = node->next;node->next->pre = node->pre;free(node);L->data--;return 1;}if (node->data == data && node->next == NULL) {node->pre->next = node->next;free(node);L->data--;return 1;}node = node->next;}return 0;
}void printList(Node* L) {Node* node = L->next;while (node) {printf("%d ", node->data);node = node->next;}printf("NULL\n");
}int main() {Node* L = initList();headInsert(L, 1);headInsert(L, 2);headInsert(L, 3);printList(L);tailInsert(L, 4);tailInsert(L, 5);tailInsert(L, 6);printList(L);delete(L, 1);printList(L);delete(L, 6);printList(L);return 0;
}
4、双循环链表
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h> //开辟空间typedef struct Node {int data;struct Node* pre;struct Node* next;
}Node;Node* initList() {Node* L = (Node*)malloc(sizeof(Node));L->data = 0;L->pre = L;L->next = L;return L;
}void headInsert(Node* L, int data) {Node* node = (Node*)malloc(sizeof(Node));node->data = data;if (L->data == 0) {//链表为空node->pre = L;node->next = L->next;L->next = node;L->pre = node;L->data++;}else {//链表不为空node->pre = L;node->next = L->next;L->next->pre = node;L->next = node;L->data++;}
}void tailInsert(Node* L, int data) {Node* node = L->pre;Node* n = (Node*)malloc(sizeof(Node));n->data = data;n->pre = node;n->next = L;node->next = n;L->pre = n;L->data++;
}int delete(Node* L, int data) {Node* node = L->next;while (node != L) {if (node->data == data) {node->pre->next = node->next;node->next->pre = node->pre;L->data--;return 1;}node = node->next;}return 0;
}void printList(Node* L) {Node* node = L->next;while (node != L) {printf("%d ", node->data);node = node->next;}printf("NULL\n");
}int main() {Node* L = initList();headInsert(L, 1);headInsert(L, 2);headInsert(L, 3);printList(L);tailInsert(L, 4);tailInsert(L, 5);tailInsert(L, 6);printList(L);delete(L, 1);printList(L);delete(L, 6);printList(L);return 0;
}
五、多项式相加
【问题描述】编写一个程序用单链表存储多项式,并实现两个一元多项式A与B相加的函数。A,B刚开始是无序的,A与B之和按降序排列。例如:
多项式A: 1.2X^0 2.5X^1 3.2X^3 -2.5X^5
多项式B: -1.2X^0 2.5X^1 3.2X^3 2.5X^5 5.4X^10
多项式A与B之和:5.4X^10 6.4X^3 5X^1
【输入形式】第一行第一个数据n代表多项式的总项数,后面的2*n个数据,每一对代表对应的某一项的系数和指数,第二行类似,第三行的数据x要查询第几项
【输出形式】多项式中某一项的系数与指数,系数保留一位小数
【输入样例】
4 1.2 0 2.5 1 3.2 3 -2.5 5
5 -1.2 0 2.5 1 3.2 3 2.5 5 5.4 10
2
【输出样例】
6.4 3
【样例说明】
第一个多项式的系数与指数对,以空格隔开
第二个多项式的系数与指数对,以空格隔开
输出第2项的系数与指数,系数与指数间用空格隔开,系数保留一位小数
【评分标准】必须用链表实现
#include<stdio.h>
#include<stdlib.h>
typedef struct node* list;
struct node {float coef;int expon;list next;
};
list create()//最基本的构建其实是最好的方法 我也不多说了
{list q, r, head;int i;head = (list)malloc(sizeof(struct node));r = head;int n;scanf("%d", &n);for (i = 0; i < n; i++){q = (list)malloc(sizeof(struct node));scanf("%f %d", &q->coef, &q->expon);r->next = q;r = q;}r->next = NULL;return head;
}
list sort(list head)//这是一个排序的函数
{list p = head->next, q, r;if (p != NULL){r = p->next;p->next = NULL;p = r;while (p != NULL){r = p->next;q = head;while (q->next != NULL && q->next->expon > p->expon)q = q->next;p->next = q->next;q->next = p;p = r;}}return head;}
list add(list a, list b)//排好序的多项式相加
{list p, q, r, c, s;p = a->next;q = b->next;c = (list)malloc(sizeof(struct node));r = c;while (p && q) {if (p->expon < q->expon) {s = (list)malloc(sizeof(struct node));s->expon = q->expon;s->coef = q->coef;r->next = s;r = s;q = q->next;}else if (p->expon > q->expon) {s = (list)malloc(sizeof(struct node));s->expon = p->expon;s->coef = p->coef;r->next = s;r = s;p = p->next;}else {double sum = p->coef + q->coef;if (sum != 0) {s = (list)malloc(sizeof(struct node));s->coef = sum;s->expon = p->expon;r->next = s;r = s;p = p->next;q = q->next;}else {p = p->next;q = q->next;}}}if (p == NULL && q == NULL)r->next = NULL;if (p != NULL) {r->next = p;}if (q != NULL) {r->next = q;}return c;
}
void show(list l)//输出函数
{int n;list p = l;scanf("%d", &n);while (n--) {p = p->next;}printf("%.1f %d", p->coef, p->expon);//这个格式十分重要啊printf("\n");
}
int main()
{list a, b, c, d, e;a = create();b = create();d = sort(a);e = sort(b);c = add(d, e);show(c);return 0;}