标签:blog io for div log ad ef amp as
#define _CRT_SECURE_NO_DEPRECATE /*取消scanf,printf不安全之类的错误提示*/ #include <stdio.h> #include <stdlib.h> typedef struct node { int value; struct node* next; }listnode; listnode* Creat_List1(int nodenum, int *data); //最先进去的元素在最后面 listnode* Creat_List2(int nodenum, int *data); //最先进去的元素在最前面 int Get_Link_Element(listnode* head, int i); //取得头指针为head的链表中的第i个元素的值(包括第0个元素) void Insert_List(listnode* head, int a, int i); void Delet_List(listnode* head, int i); //删除第i个元素 listnode* Merge_TWO_Linklist(listnode *list1, listnode *list2);//合并两个有序链表 int main() { int data; listnode *linka, *linkb, *linkc, *linkd, *linke; linka = (listnode*)malloc(sizeof(listnode)); linkb = (listnode*)malloc(sizeof(listnode)); linkc = (listnode*)malloc(sizeof(listnode)); linkd = (listnode*)malloc(sizeof(listnode)); linke = (listnode*)malloc(sizeof(listnode)); int a[5] = { 1, 3, 5, 7, 9 }; int b[3] = { 2,2, 4 }; linka = Creat_List2(5, a); linkb = Creat_List2(3, b); Insert_List(linkb, 10, 1); Delet_List(linkb, 1); linkc=Merge_TWO_Linklist(linka, linkb); data = Get_Link_Element(linkb, 1); printf("%d\n", data); } //新元素总是插入到头指针后面,结果就是原先的元素一直往后移 listnode* Creat_List1(int nodenum,int *data) { listnode *pc; //保存新节点 listnode *head; head = (listnode*)malloc(sizeof(listnode)); head->next = NULL; //先建立一个带头结点的单链表 /*开始插入元素*/ for (int i = 0; i < nodenum; i++) { pc = (listnode*)malloc(sizeof(listnode)); pc->value = data[i]; pc->next = head->next; head->next = pc; } return head; } //新元素插入到头指针后面,头指针一直往后移 listnode* Creat_List2(int nodenum, int *data) { listnode *pc; //保存新节点 listnode *head; listnode *r;//保存产生的链表 head = (listnode*)malloc(sizeof(listnode)); head->next = NULL; //先建立一个带头结点的单链表 r = head; /*开始插入元素*/ for (int i = 0; i < nodenum; i++) { pc = (listnode*)malloc(sizeof(listnode)); pc->value = data[i]; head->next = pc; pc->next = NULL; head = pc; } return r; } //取得头指针为head的链表中的第i个元素的值(包括第0个元素) int Get_Link_Element(listnode* head, int i) { /*创建一个扫描指针,让它指向第一个元素*/ listnode* pc; int j = 0; //计数 pc = (listnode*)malloc(sizeof(listnode)); pc = head->next; while (pc != NULL && j < i) //遍历 { pc = pc->next; j++; } if (pc == NULL || j > i) exit(-1); return pc->value; } /* *合并两个有序链表,把list2合并到list1 */ listnode* Merge_TWO_Linklist(listnode *list1, listnode *list2) { listnode *pc1, *pc2, *pc3,*list3; pc1 = (listnode*)malloc(sizeof(listnode)); pc2 = (listnode*)malloc(sizeof(listnode)); pc3 = (listnode*)malloc(sizeof(listnode)); pc1 = list1->next; pc2 = list2->next; pc3 = list1; list3 = pc3; while (pc1 != NULL && pc2 != NULL) { if (pc1->value <= pc2->value) { pc3->next = pc1; pc3 = pc3->next; pc1 = pc1->next; } else{ pc3->next = pc2; pc3 = pc3->next; pc2 = pc2->next; } } if (pc1 == NULL) pc3->next = pc2; if (pc2 == NULL) pc3->next = pc1; free(list2); return list3; } void Insert_List(listnode* head, int a, int i) { listnode *pc; listnode *s; pc = head->next;//令pc指向第一个元素 int j = 1; while (pc != NULL && j < i) { pc = pc->next; //pc后移动 j++; } if (pc == NULL) { printf("error\n"); exit(-1); } s = (listnode*)malloc(sizeof(listnode)); s->value = a; s->next = pc->next; pc->next = s; } //删除第i个元素 void Delet_List(listnode* head, int i) { listnode *temp = NULL; int j = 1; //计数 head = head->next; while (head != NULL && j < i) { head = head->next; j++; } if (head == NULL) { printf("error\n"); exit(-1); } temp = head->next; head->next = temp->next; //head = head->next->next; free(temp); }
标签:blog io for div log ad ef amp as
原文地址:http://www.cnblogs.com/mrethan/p/4149534.html