#include<bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef int ElemType;
typedef struct pNode{
ElemType data;
struct pNode *next;
}LinkList;
//头插法建立单链表
LinkList *Creat_LinkList()
{
ElemType x;
LinkList *head,*p;
head = (LinkList*)malloc(sizeof(LinkList));
if(head == NULL)
return head;
head -> next = NULL;
cout<<"请输入要录入的数以0结束"<<endl;
cin >> x;
while(x != 0)
{
p = (LinkList*) malloc(sizeof(LinkList));
if(p == NULL)
return head;
p -> data = x;
p -> next = head -> next;
head -> next = p;
cin >> x;
}
return head;
}
//尾插法建立单链表
LinkList * Creat_LinkList_R()
{
ElemType x;
LinkList *head,*p,*tail;//tail是尾指针
head = (LinkList*)malloc(sizeof(LinkList));
if(head == NULL)
return head;
head -> next = NULL;
tail = head; //一开始尾指针指向头指针的位置
cout<<"请输入要录入的数以0结束"<<endl;
cin >> x;
while(x != 0)
{
p = (LinkList*) malloc(sizeof(LinkList));
if(p == NULL)
return head;
p -> data = x;
tail -> next = p; //将p插入到尾节点的后面
tail = p; //修改尾节点的指向
tail -> next = NULL; //将尾节点的指针域修改为空
cin >> x;
}
return head;
}
//单链表的遍历
int Print_LinkList(LinkList *head)
{
LinkList* p = head -> next;
if(p == NULL)
return 0;
while(p != NULL)
{
cout << p -> data << endl;
p = p -> next;
}
return 1;
}
//单链表求长度
int LinkList_Length(LinkList* head)
{
ElemType num = 0;
LinkList * p = head;
while(p -> next != NULL)
{
num++;
p = p -> next;
}
return num;
}
//单链表的查找(返回指定位置的元素)
LinkList * GetDara_LinkList(LinkList * head,int i){
LinkList * p = head;
int num = 0; //计数器,每次加1,p的指向向后面改变一次
if(i < 0)
return NULL;
while((p -> next != NULL )&&(num < i))
{
num++;
p = p -> next;
}
if(num == i)
cout<< p -> data <<endl;
else
return NULL;
}
//单链表的查找(按照值来查找)
LinkList * GetDara_LinkList_value(LinkList *head,int value)
{
LinkList *p = head -> next;
while(p != NULL)
{
if(p -> data != value)
p = p -> next;
else
break;
}
cout << "查询到" << p -> data <<endl;
}
//单链表的插入(后插)
void InsertAfter_LinkList(LinkList * p,LinkList * s)
{
s -> next = p -> next;
p -> next = s;
}
//单链表的插入(前插)
void InsertBefore_LinkList(LinkList *head,LinkList *p,LinkList *s)
{
LinkList *q = head;
while(q -> next != p) //搜索P的前驱节点
q = q -> next;
s -> next = p;
q -> next = s;
}
//在链表中指定位置前插入新节点s
int InsertIndex_LinkList(LinkList * head,LinkList *s,int i)
{
LinkList *p;
if(i <= 0) p = NULL;
else if(i == 1) p = head;
else
p = GetDara_LinkList(head,i-1);
if(p == NULL)
return 0;
else{
InsertAfter_LinkList(p,s);
return 1;
}
}
//删除P的后继节点
int DeleteAfter_LinkList(LinkList *p)
{
LinkList * r;
if(!p) return 0;
r = p -> next;
if(!r)
return 0;
p -> next = r -> next;
free(r);
return 1;
}
int main()
{
LinkList *L;
//L = Creat_LinkList(); //头插法建立单链表
L = Creat_LinkList_R(); //尾插法建立单链表
Print_LinkList(L); //遍历单链表
int L_Length = LinkList_Length(L); //单链表长度
cout << "单链表的长度为:" << L_Length <<endl;
GetDara_LinkList(L,1); //返回第一个位置的元素
GetDara_LinkList_value(L,1); //按照链表中的值(1)来查找
// InsertAfter_LinkList(LinkList * p,LinkList * s) //单链表的插入(后插)
// InsertBefore_LinkList(LinkList *head,LinkList *p,LinkList *s) //在链表中指定位置前插入新节点s
// InsertIndex_LinkList(LinkList * head,LinkList *s,int i) //在链表中指定位置前插入新节点s
// int DeleteAfter_LinkList(p); //删除P的后继节点
return 0;
}
【数据结构】C/C++ 单链表的 创建、初始化、增、删、改、查、遍历等基本操作
最新推荐文章于 2024-09-18 22:30:23 发布