首先,先建一个cpp文件,具体代码如下:
尾插法
LinkList List_TailInsert(LinkList &L) {
int x;
LNode *s, *r = L;
scanf_s("%d", &x);
while (x!=9999)
{
s = (LNode *)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf_s("%d", &x);
}
r->next = NULL;
return L;
}
头插法
LinkList List_HeadInsert(LinkList &L) {
int x;
LNode *s;
scanf_s("%d", &x);
while (x!=9999)
{
s = (LNode *)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
scanf_s("%d", &x);
}
return L;
}
按位查找,返回第i个元素
LNode * GetElem(LinkList L,int i) {
LNode *p = L->next;
int j = 1;
if (i==0)
{
return L;
}
if (i<1)
{
return NULL;
}
while (p!=NULL && j<i)
{
p = p->next;
j++;
}
return p;
}
在第i个位置插入新的结点
bool ListInsert(LinkList &L, int i, int e) {
if (i<1)
{
return false;
}
LNode *p = GetElem(L, i - 1);
if (p==NULL)
{
return false;
}
LNode *s = (LNode *)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
删除第i个位置的结点
bool ListDelete(LinkList &L, int i, int &e) {
if (i<1)
{
return false;
}
LNode *p = GetElem(L, i - 1);
if (p == NULL) {
return false;
}
if (p->next==NULL)
{
return false;
}
LNode *q = p->next;
e = q->data;
p->next = q->next;
free(q);
return true;
}
单链表的逆置
LinkList NiZhi(LinkList &L) {
LinkList p, q;
p = L->next;
L->next = NULL;
while (p!=NULL)
{
q = p;
p = p->next;
q->next = L->next;
L->next = q;
}
return L;
}
单链表的逆置
LinkList NiZhi(LinkList &L) {
LinkList p, q;
p = L->next;
L->next = NULL;
while (p!=NULL)
{
q = p;
p = p->next;
q->next = L->next;
L->next = q;
}
return L;
}
完整代码
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode {
int data;
struct LNode *next;
}LNode,*LinkList;
bool InitList(LinkList &L) {
L = (LNode *)malloc(sizeof(LNode));
if (L==NULL)
{
return false;
}
L->next = NULL;
return true;
}
LinkList List_TailInsert(LinkList &L) {
int x;
LNode *s, *r = L;
scanf_s("%d", &x);
while (x!=9999)
{
s = (LNode *)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf_s("%d", &x);
}
r->next = NULL;
return L;
}
LinkList List_HeadInsert(LinkList &L) {
int x;
LNode *s;
scanf_s("%d", &x);
while (x!=9999)
{
s = (LNode *)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
scanf_s("%d", &x);
}
return L;
}
LinkList NiZhi(LinkList &L) {
LinkList p, q;
p = L->next;
L->next = NULL;
while (p!=NULL)
{
q = p;
p = p->next;
q->next = L->next;
L->next = q;
}
return L;
}
LNode * GetElem(LinkList L,int i) {
LNode *p = L->next;
int j = 1;
if (i==0)
{
return L;
}
if (i<1)
{
return NULL;
}
while (p!=NULL && j<i)
{
p = p->next;
j++;
}
return p;
}
bool ListInsert(LinkList &L, int i, int e) {
if (i<1)
{
return false;
}
LNode *p = GetElem(L, i - 1);
if (p==NULL)
{
return false;
}
LNode *s = (LNode *)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
bool ListDelete(LinkList &L, int i, int &e) {
if (i<1)
{
return false;
}
LNode *p = GetElem(L, i - 1);
if (p == NULL) {
return false;
}
if (p->next==NULL)
{
return false;
}
LNode *q = p->next;
e = q->data;
p->next = q->next;
free(q);
return true;
}
void _printList(LinkList L) {
while (L != NULL)
{
L = L->next;
printf("%d ", L->data);
}
}
int main() {
LinkList L;
InitList(L);
List_TailInsert(L);
if (ListInsert(L,2,222))
{
printf("在2号位置插入222插入成功\n");
}
else
{
printf("插入失败\n");
}
int temp = 0;
if (ListDelete(L, 2, temp)) {
printf("%d已被删除\n",temp);
}
else
{
printf("删除失败\n");
}
_printList(L);
return 0;
}
|