数据结构(一)单链表的基本操作(不带头节点)

#include <iostream>


using namespace std;


struct Node{
int data;
struct Node *next;
};
//头插法创建链表 
struct Node * create(int n)
{
struct Node *p1,*p2,*head;
int data;
p1 = p2 = head = NULL;
p1 = new Node();
cin>>data;
p1->data = data;
p1->next = NULL;
head = p1;
for(int i=1;i<n;i++)
{
p2 = new Node();
cin >> data;
p2->data = data;
p2->next = NULL;
p1->next = p2;
p1 = p2; 
}
return head;
}
//尾插法创建链表
struct Node *create_tail(int n)
{
struct Node *p1,*p2,*head;
int data;
p1 = p2 = head = NULL;
p1 = new Node();
cin>>data;
p1->data = data;
p1->next = NULL;
for(int i=1;i<n;i++)
{
p2 = new Node();
cin >> data;
p2 ->data = data;
p2->next = p1;
p1 = p2;
}
head = p2;
return head;

//链表的输出 
void output(struct Node *head)
{
struct Node *p1;
p1 = head;
while(p1!=NULL)
{
cout<<" "<<p1->data;
p1 = p1->next;
}
}
//链表的查询
void search(struct Node *head)
{
//按序号查询 
struct Node *p1;
int n;
p1 = head;
cout << "请输入查询的序号:";
cin>>n;
int i=1;
while (p1!=NULL)
{
if(i==n)
{
cout<<"此序号的数据为: "<<p1->data;
break;
}
p1 = p1->next;
i++;
}
//按值查询
cout<<"请输入你要查询的值";
cin>>n;
int flag=0;
while(p1!=NULL) 
{
if(p1->data==n)
{
cout<<"你要查询的数据为: "<<p1->data;
exit(0);
flag=1; 
}
p1 = p1->next;
}
if(flag=0)
{
cout<<"没有此数据";
}


//链表的插入
void insert(struct Node *head)
{
struct Node *p1,*p2;
p1 = head;
p2 = NULL;
//插入在一个数据的前面
int n,data;
cout<<"请输入你要插入在哪个数据的前面:";
cin>>n;
while(p1->next->data!=n)
{
p1=p1->next;
if(p1->next==NULL)
{
cout<<"没有找到此数据";
exit(0);
}



p2 = new Node();
cout<<"请输入你要插入的数据:";
cin>>data;
p2->data = data;
p2->next = NULL;
p2->next = p1->next;
p1->next = p2;

//链表删除 
void deleteData(struct Node *head)
{
struct Node *p1,*s;
p1 = head;
int n;
cout<<"请输入你要删除的数据:";
cin>>n;
while(p1->next->data!=n)
{
p1 = p1->next;
if(p1->next!=NULL)
{
cout<<"你要删除的数据不存在";
exit(0);
}
}
s = p1->next;
p1->next=p1->next->next;
delete(s);
}
//链表逆序
struct Node *invert(struct Node *head,int n)
{
struct Node *p1,*invert_head,*s,*p2,*p3,*p4;
p1 = head;
invert_head = new Node();
invert_head->next = NULL;
for(int i=0;i<n;i++)
{
while(p1->next!=NULL)
{
p3 = p1;
p1 = p1->next;
}
if(i==0)
{
s = p1;
invert_head->data = p1->data;
invert_head->next = NULL;
p3->next = NULL;
p4 = invert_head;
delete(s);
}
else
{
s = p1;
p2 = new Node();
p2->data = p1->data;
p4->next = p2;
p2->next = NULL;
p4= p2;
p3->next = NULL;
delete(s);
}
p1 = head;

return invert_head;

struct Node *invert(struct Node *head)
{
struct Node *p1,*p2;
p1 = NULL;
p2 = head;
while(p2!=NULL)
{
head = head->next;
p2->next = p1;
p1 = p2;
p2 = head;
}
head = p1;
}
 
int main()
{
int n,n_2;
cout<<"请输入你要创建的个数\n";
cin>>n;
struct Node *head=NULL,*head_2=NULL,*result;
head=create(n);
head=invert(head);
// cout<<"请输入你要创建的个数\n";
// cin>>n_2;
// head_2 = create(n_2);
// result=combine(head,head_2);
// head = create_tail(n);
// output(head);
//search(head);
// insert(head);
// deleteData(head);
// head=invert(head,n);
output(head);
// output(head);
return 0;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值