代码
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode, *LinkList;
LinkList createList_first(LinkList &L,int n);
LinkList createList_last(LinkList &L, int n);
void print(LinkList &head);
bool isEmpty(LinkList L);
void print(LinkList &head);
bool isEmpty(LinkList L);
int GetLength(LinkList &head);
bool insertList_L(LinkList &L, int position, int data);
bool deleteList_L(LinkList &L,int position);
LinkList createList_first(LinkList &L,int n) {
L = new LNode;
L->data = 2013211553;
L->next = NULL;
printf(" ***********前插法创建单链表,请输入%d个数****************\n",n);
for(int i = n; i > 0; i--) {
LNode* p = new LNode;
cin>>p->data;
p->next = L->next;
L->next = p;
}
print(L);
return L;
}
LinkList createList_last(LinkList &L, int n) {
L = new LNode;
L->data = 2013211553;
L->next = NULL;
printf(" **********尾插法创建单链表,请输入%d个数**********\n",n);
LNode* endNode = L;
for(int i = 0; i < n; i++) {
LNode* p = new LNode;
cin>>p->data;
p->next = NULL;
endNode->next = p;
endNode = p;
}
print(L);
return L;
}
void print(LinkList &head) {
if(head == NULL)
return;
LNode* node = head->next;
printf(" **********遍历输出链表所有元素**********\n");
printf(" 头结点里面的数据信息: %d\n",head->data);
printf("链表中的实际元素(除头结点外)为");
for(int i = 1; node != NULL; i++) {
printf(" %d ",node->data);
node = node->next;
}
}
bool isEmpty(LinkList L) {
if(L->next == NULL)
return false;
else
return true;
}
bool GetElem_L(LinkList L,int i) {
printf(" **********在带头结点的单链表查找元素**********\n");
bool flag = false;
LNode* p = L->next;
int j = 1;
while(p && j < i) {
p = p->next;
j++;
}
if(!p ||j>i){
printf("没有第%d个位置的元素\n",i);
} else {
printf(" 第%d个位置的元素是 %d\n",i,p->data);
}
return flag;
}
int GetLength(LinkList &head) {
int length = 0;
LNode* node = head->next;
for(length ; node != NULL;length++) {
node = node->next;
}
return length;
}
bool insertList_L(LinkList &L, int position, int data){
bool flag = false;
LNode* p = L;
int j = 0;
while(p&&j<position-1){
p = p->next;
j++;
}
if(!p||j>position-1)
flag = false;
LNode* node = new LNode;
node->data = data;
node->next = p->next;
p->next = node;
flag = true;
print(L);
return flag;
}
bool deleteList_L(LinkList &L,int position) {
bool flag = false;
LNode* p = L;
int j = 0;
while((p->next)&&j<position-1) {
p = p->next;
j++;
}
if(!(p->next)||j>position-1)
flag = false;
LNode* node = p->next;
printf("\n\t删除的元素是 %d\n",node->data);
p->next = node->next;
delete(node);
print(L);
return flag;
}
void main() {
LNode* head = new LNode;
int length = 0;
printf("\t\t请输入链表的长度:");
scanf("%d",&length);
getchar();
LinkList L = createList_last(head,length);
int position = 0, data = 0;
printf("\n\n\t\t请输入插入元素的位置和值:");
scanf("%d,%d",&position,&data);
getchar();
insertList_L(L,position,data);
int getelem = 0;
printf("\n\n\t\t请输入要查找的元素位置");
scanf("%d",&getelem);
getchar();
GetElem_L(L,getelem);
int deletep = 0;
printf("\n\n\t\t请输入要删除的元素位置");
scanf("%d",&deletep);
getchar();
deleteList_L(L,deletep);
int getlength = 0;
getlength = GetLength(L);
printf("\n\n\t\t链表的长度(除头结点)为:%d\n",getlength);
}
运行结果截图