标签:null index c++ 注意 highlight tin stat err using
1.线性链表:用任意的存储单元存储线性表的数据元素(这组存储单元可以是连续的也可以是不连续)
#include <bits/stdc++.h> using namespace std; #define ElemType int #define Status int #define ERROR -1 #define OK 1 typedef struct LNode { ElemType data; struct LNode *next; }LNode ,*LinkList; LinkList CreateListHead_L(int n)///* 头插入 { LinkList L = (LinkList)malloc(sizeof(LNode)); L->next = NULL; while (n--) { LinkList newnode = (LinkList)malloc(sizeof(LNode)); scanf("%d",&newnode->data); newnode->next = L->next; L->next = newnode; } return L; } LinkList CreateListTail_L(int n)///尾插入 { LinkList L = (LinkList)malloc(sizeof(LNode)); LinkList last = (LinkList)malloc(sizeof(LNode)); L->next = NULL; last = L; while (n--) { ElemType e; scanf("%d",&e); LinkList newnode = (LinkList)malloc(sizeof(LNode)); newnode->data = e; last->next = newnode; newnode->next = NULL; last = newnode; } return L; } Status GetElem_L(LinkList &L,int i,ElemType &e) { LinkList index = L->next; int ct = 1; while (index && ct < i) { index = index->next; ++ct; } if (!index || ct > i) return ERROR; e = index->data; return OK; } Status ListInsert_L(LinkList &L,int i,ElemType e) { LinkList index = L->next; int ct = 1; while (index && ct < (i-1)) { index = index->next; ++ct; } if (!index || ct > i -1) return ERROR; LinkList newnode = (LinkList)malloc(sizeof(LNode)); newnode->data = e; newnode->next = index->next; index->next = newnode; return OK; } Status ListDeteleByPos_L(LinkList &L,int i,ElemType &e) { LinkList index = L->next; int ct = 1; while (index && ct < i-1) { index = index->next; ++ct; } if (!(index->next) || ct > i-1) return ERROR; LinkList tmp = index->next; index->next = tmp->next; e = tmp->data; free(tmp); return OK; } Status ListDeteleByVal_L(LinkList &L,ElemType e) { LinkList index = L->next,pri = L; while (index) /// 注意:判断的如果是 pri pri 是最后一个元素的时候 index 已经超出链表 { if (index->data == e) { pri->next = index->next; free(index); index = pri->next; } else { pri = pri->next; index = index->next; } } return OK; } int GetLength(LinkList &L) { int len = 0; LinkList index = L->next; while (index) { index = index->next; len++; } return len; } LinkList ListReverse(LinkList &L) { LinkList a = L->next; LinkList b = a->next; LinkList c = b->next; a->next = NULL; int len = GetLength(L); while (len--) { b->next = a; c = b; b = a; if(!a) { L->next=a; } else c=c->next; } return L; } void TrverseLinklist(LinkList &L)///* 注意:这里是带头指针的链表 { LinkList index = L->next; for (;index!= NULL; index = index->next) { cout<<index->data<<" "; } cout<<endl; }
标签:null index c++ 注意 highlight tin stat err using
原文地址:http://www.cnblogs.com/sxy-798013203/p/7768144.html