赞
踩
单链表的实现
#include<iostream> using namespace std; typedef struct LNode { int data; LNode* next; }ListNode; void CreatListF(ListNode*& L, int n, int a[]);//头插法建立链表 void CreatListR(ListNode*& L, int n, int a[]);//尾插法建立链表 void DestroyList(ListNode*& L);//销毁链表 int ListLength(ListNode* L);//求链表的长度 void DispList(ListNode* L);//输出线性表 bool GetElem(ListNode* L, int& e, int i);//按某个数据值查找 int LocateElem(ListNode*& L, int i, int e);//按元素值查找 bool ListInsert(ListNode*& L, int i, int e);//插入元素 bool ListDelete(ListNode*& L, int i, int& e);//删除数据元素 void UpOrder(ListNode*& L);//对链表进行递增排序 void Dorder(ListNode*& L);//对链表进行递减排序 int main() { int a[10] = { 1,3,2,4,6,5,8,9,7,0 }; ListNode* L = NULL; //ListNode* R; CreatListF(L, 10, a); //CreatListR(R,10,a); DispList(L); //DispList(R); UpOrder(L); DispList(L); Dorder(L); DispList(L); DestroyList(L); return 0; } void CreatListF(ListNode*& L, int n, int a[]) {//头插 ListNode* s; int i = 0; L = (ListNode*)malloc(sizeof(ListNode)); L->next = NULL; while (i < n) { s = (ListNode*)malloc(sizeof(ListNode)); s->data = a[i]; s->next = L->next; L->next = s; i++; } } void CreatListR(ListNode*& L, int n, int a[])//尾插法 { ListNode* s, * r; int i = 0; L = (ListNode*)malloc(sizeof(ListNode)); r = L; while (i < n) { s = (ListNode*)malloc(sizeof(ListNode)); s->data = a[i]; r->next = s; r = s; i++; } r->next = NULL; } void DestroyList(ListNode*& L)//销毁链表 { ListNode* pre, * p; pre = L, p = L->next; while (p != NULL) { free(pre); pre = p; p = p->next; } free(pre); } int ListLength(ListNode* L) {//统计链表的长度 ListNode* p; int n = 0; p = L->next; while (p != NULL) { n++; p = p->next; } return (n); } void DispList(ListNode* L) {//输出线性表 ListNode* p = L->next; while (p != NULL) { cout << p->data << " "; p = p->next; } cout << endl; } bool GetElem(ListNode* L, int& e, int i) {//按位置查找 int j = 0; ListNode* p = L; if (i <= 0) return false; while (p != NULL && j < i) { j++; p = p->next; } if (p == NULL) return false; else e = p->data; return true; } int LocateElem(ListNode*& L, int e)//按元素值查找 { ListNode* p = L->next; int i = 1; while (p->data != e && p != NULL) { i++; p = p->next; } if (p == NULL) return 0; else return i; } bool ListInsert(ListNode*& L, int i, int e)//插入元素 { int j = 0; ListNode* p = L, * s; if (i <= 0) return false; while (p != NULL && j < i - 1) { j++; p = p->next; } if (p == NULL) return false; else { s = (ListNode*)malloc(sizeof(ListNode)); s->data = e; s->next = p->next; p->next = s; return true; } } bool ListDelete(ListNode*& L, int i, int& e)//删除数据元素 { ListNode* p = L; int j = 0; if (i <= 0) return false; while (j < i - 1 && p != NULL) { j++; p = p->next; } if (p == NULL) return false; else { ListNode* q = p->next; if (q == NULL) return false; e = q->data; p->next = q->next; free(q); return true; } } void UpOrder(ListNode*& L) {//递增排序 ListNode* p,*q,*pre; p = L->next->next; L->next->next= NULL; while (p!=NULL) { q = p->next; pre = L; while(pre->next!=NULL&&pre->next->data<p->data) { pre = pre->next; } p->next = pre->next; pre->next = p; p = q; } } void Dorder(ListNode*& L) {//递减排序 ListNode* p, * pre, * q,*r; p=L->next->next; L->next->next = NULL; while (p != NULL ) { pre = L; q = p->next; while (pre->next!=NULL&&pre->next->data>p->data) { pre = pre->next; } p->next = pre->next; pre->next = p; p = q; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。