
C++编程,不会写,麻烦有时间的达人帮写一下,星期三就要了→_←,题目要求如下,就这么多……
1.初始化链表S2.判断链表S是否非空3.依次进链栈元素a,b,c,d,e4.判断链栈S是否非空5.输出链栈长度6.输出从栈顶到栈底元素7.输出出链栈序列8.判断链栈S是...
1.初始化链表S
2.判断链表S是否非空
3.依次进链栈元素a,b,c,d,e
4.判断链栈S是否非空
5.输出链栈长度
6.输出从栈顶到栈底元素
7.输出出链栈序列
8.判断链栈S是否非空
9.释放链栈
特殊要求:1.合并两链栈。2.查找某元素位置。3.输出数组元素。4.保存地址。5.转化为指针(可不做) 展开
2.判断链表S是否非空
3.依次进链栈元素a,b,c,d,e
4.判断链栈S是否非空
5.输出链栈长度
6.输出从栈顶到栈底元素
7.输出出链栈序列
8.判断链栈S是否非空
9.释放链栈
特殊要求:1.合并两链栈。2.查找某元素位置。3.输出数组元素。4.保存地址。5.转化为指针(可不做) 展开
2011-12-26
展开全部
//链表功能大全
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct node
{
int nDate;
struct node *pstnext;
}Node;
//链表输出
void output(Node *head)
{
Node *p = head->pstnext;
while(NULL != p)
{
cout<<p->nDate;
p = p->pstnext;
}
cout<<"\r"<<endl;
}
//链表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
cout<<"分配内存失败\r"<<endl;
return NULL;
}
head->pstnext = NULL;
p = head;
while(cycle)
{
cout<<"请输入数据且当输入数据为0时结束输入\r"<<endl;
cin>>Date;
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
cout<<"分配内存失败\r"<<endl;
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}
//单链表测长
void length(Node *head)
{
Node *p = head->pstnext;
int j=0;
while(NULL != p)
{
p = p->pstnext;
j++;
}
cout<<j<<"\r"<<endl;
}
//链表按值查找
void research_Date(Node *head, int date)
{
Node *p;
int n=1;
p = head->pstnext;
while(NULL != p && date != p->nDate)
{
p = p->pstnext;
++n;
}
if(NULL == p)
{
cout<<"链表中没有找到该值";
}else if(date == p->nDate)
{
cout<<"要查找的值"<<date<<"在链表中第"<<n<<"个位置\r"<<endl;
}
return;
}
//按序号查找
void research_Number(Node *head, int Num)
{
Node *p=head;
int i = 0;
while(NULL != p && i < Num)
{
p = p->pstnext;
i++;
}
if(p == NULL)
{
cout<<"查找位置不合法\r"<<endl;
}else if(i == 0)
{
cout<<"查找位置为头结点\r"<<endl;
}else if(i == Num)
{
cout<<"第"<<i<<"个位置数据为"<<p->nDate<<"\r"<<endl;
}
}
//在指定元素之前插入新结点
void insert_1(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
{
pre = pre->pstnext;
j++;
}
if(NULL == pre || j > i-1)
{
cout<<"插入位置不存在\r"<<endl;
}else
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
cout<<"分配内存失败\r"<<endl;
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}
}
//在指定元素之后插入新结点
void insert_2(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre->pstnext && j < i)
{
pre = pre->pstnext;
j++;
}
if(j == i)
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
cout<<"分配内存失败\r"<<endl;
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}else
{
cout<<"插入位置不存在\r"<<endl;
}
}
//删除指定结点
void Delete_1(Node *head, int i3)
{
Node *p = head, *pre = NULL;
int j = 0;
while(NULL != p && j < i3)
{
pre = p;
p = p->pstnext;
j++;
}
if(NULL == p)
{
cout<<"删除位置不存在\r"<<endl;
}else
{
pre->pstnext = p->pstnext;
free(p);
}
}
//指定删除单链表中某个数据,并统计删除此数据的个数
int Delete_2(Node *head, int Delete_date)
{
int count = 0;
Node *p = head, *q;
while(NULL != p->pstnext)
{
q = p->pstnext;
if(q->nDate == Delete_date)
{
p->pstnext = q->pstnext;
free(q);
++count;
}
else
{
p = q;
}
}
return count;
}
//链表逆置
void Reverse_list(Node *head)
{
Node *q, *s;
if(NULL == head->pstnext || NULL == head->pstnext->pstnext)
{
return;
}
q = head->pstnext->pstnext;
head->pstnext->pstnext = NULL;
while(NULL != q)
{
s = q->pstnext;
q->pstnext = head->pstnext;
head->pstnext = q;
q = s;
}
}
//单链表的连接
void connect_list(Node *head, Node *head_New)
{
Node *p = head;
while(NULL != p->pstnext)
{
p = p->pstnext;
}
p->pstnext = head_New->pstnext;
}
//单链表销毁
void destroy_list(Node* head)
{
while (NULL != head)
{
Node* temp = head;
head = head->pstnext;
free(temp);
}
}
void main()
{
int date, num; //待查找数据
int i3; //指定删除元素的位置
int i1, i2, Newdate_1, Newdate_2; //待插入的新数据
int Delete_date, k; //待删除的数据与其个数
Node *Head = NULL; //定义头结点
Node *Head_New = NULL;
//链表建立
Head = creat();
cout<<"输出建立的单链表\r"<<endl;
output(Head);
//单链表测长
cout<<"单链表长度为\r"<<endl;
length(Head);
//链表按值查找
cout<<"请输入待查找的数据\r"<<endl;
scanf("%d", &date);
research_Date(Head, date);
//链表按序号查找
cout<<"请输入待查找序号\r"<<endl;
scanf("%d", &num);
research_Number(Head, num);
//在指定第i1个元素之前插入新元素Newdate
cout<<"在指定第i个元素之前插入新元素Newdate";
cout<<"请输入i与元素且以逗号间隔\r"<<endl;
scanf("%d,%d", &i1, &Newdate_1);
insert_1(Head, i1, Newdate_1);
cout<<"插入后新链表\r"<<endl;
output(Head);
//在指定第i2个元素之后插入新元素Newdate
cout<<"在指定第i个元素之后插入新元素Newdate";
cout<<"请输入i与元素且以逗号间隔\r"<<endl;
scanf("%d,%d", &i2, &Newdate_2);
insert_2(Head, i2, Newdate_2);
cout<<"插入后新链表\r"<<endl;
output(Head);
//指定删除i3元素
cout<<"删除元素的位置\r"<<endl;
scanf("%d", &i3);
Delete_1(Head, i3);
cout<<"删除后新链表\r"<<endl;
output(Head);
//指定删除单链表中某个数据,并统计删除此数据的个数
cout<<"请输入待删除的元素\r"<<endl;
scanf("%d", &Delete_date);
k = Delete_2(Head, Delete_date);
cout<<"删除后新链表\r"<<endl;
output(Head);
cout<<"删除指定元素在链表中的个数为:"<<endl;
cout<<k<<"\r"<<endl;
//单链表逆置
Reverse_list(Head);
cout<<"逆置后输出\r"<<endl;
output(Head);
//单链表的连接
cout<<"建立一个新链表\r"<<endl;
Head_New = creat();
cout<<"输出新链表"<<endl;
output(Head);
cout<<"将新链表连接到原来链表的尾部并输出\r"<<endl;
connect_list(Head, Head_New);
output(Head);
destroy_list(Head);
}
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct node
{
int nDate;
struct node *pstnext;
}Node;
//链表输出
void output(Node *head)
{
Node *p = head->pstnext;
while(NULL != p)
{
cout<<p->nDate;
p = p->pstnext;
}
cout<<"\r"<<endl;
}
//链表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
cout<<"分配内存失败\r"<<endl;
return NULL;
}
head->pstnext = NULL;
p = head;
while(cycle)
{
cout<<"请输入数据且当输入数据为0时结束输入\r"<<endl;
cin>>Date;
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
cout<<"分配内存失败\r"<<endl;
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}
//单链表测长
void length(Node *head)
{
Node *p = head->pstnext;
int j=0;
while(NULL != p)
{
p = p->pstnext;
j++;
}
cout<<j<<"\r"<<endl;
}
//链表按值查找
void research_Date(Node *head, int date)
{
Node *p;
int n=1;
p = head->pstnext;
while(NULL != p && date != p->nDate)
{
p = p->pstnext;
++n;
}
if(NULL == p)
{
cout<<"链表中没有找到该值";
}else if(date == p->nDate)
{
cout<<"要查找的值"<<date<<"在链表中第"<<n<<"个位置\r"<<endl;
}
return;
}
//按序号查找
void research_Number(Node *head, int Num)
{
Node *p=head;
int i = 0;
while(NULL != p && i < Num)
{
p = p->pstnext;
i++;
}
if(p == NULL)
{
cout<<"查找位置不合法\r"<<endl;
}else if(i == 0)
{
cout<<"查找位置为头结点\r"<<endl;
}else if(i == Num)
{
cout<<"第"<<i<<"个位置数据为"<<p->nDate<<"\r"<<endl;
}
}
//在指定元素之前插入新结点
void insert_1(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
{
pre = pre->pstnext;
j++;
}
if(NULL == pre || j > i-1)
{
cout<<"插入位置不存在\r"<<endl;
}else
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
cout<<"分配内存失败\r"<<endl;
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}
}
//在指定元素之后插入新结点
void insert_2(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre->pstnext && j < i)
{
pre = pre->pstnext;
j++;
}
if(j == i)
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
cout<<"分配内存失败\r"<<endl;
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}else
{
cout<<"插入位置不存在\r"<<endl;
}
}
//删除指定结点
void Delete_1(Node *head, int i3)
{
Node *p = head, *pre = NULL;
int j = 0;
while(NULL != p && j < i3)
{
pre = p;
p = p->pstnext;
j++;
}
if(NULL == p)
{
cout<<"删除位置不存在\r"<<endl;
}else
{
pre->pstnext = p->pstnext;
free(p);
}
}
//指定删除单链表中某个数据,并统计删除此数据的个数
int Delete_2(Node *head, int Delete_date)
{
int count = 0;
Node *p = head, *q;
while(NULL != p->pstnext)
{
q = p->pstnext;
if(q->nDate == Delete_date)
{
p->pstnext = q->pstnext;
free(q);
++count;
}
else
{
p = q;
}
}
return count;
}
//链表逆置
void Reverse_list(Node *head)
{
Node *q, *s;
if(NULL == head->pstnext || NULL == head->pstnext->pstnext)
{
return;
}
q = head->pstnext->pstnext;
head->pstnext->pstnext = NULL;
while(NULL != q)
{
s = q->pstnext;
q->pstnext = head->pstnext;
head->pstnext = q;
q = s;
}
}
//单链表的连接
void connect_list(Node *head, Node *head_New)
{
Node *p = head;
while(NULL != p->pstnext)
{
p = p->pstnext;
}
p->pstnext = head_New->pstnext;
}
//单链表销毁
void destroy_list(Node* head)
{
while (NULL != head)
{
Node* temp = head;
head = head->pstnext;
free(temp);
}
}
void main()
{
int date, num; //待查找数据
int i3; //指定删除元素的位置
int i1, i2, Newdate_1, Newdate_2; //待插入的新数据
int Delete_date, k; //待删除的数据与其个数
Node *Head = NULL; //定义头结点
Node *Head_New = NULL;
//链表建立
Head = creat();
cout<<"输出建立的单链表\r"<<endl;
output(Head);
//单链表测长
cout<<"单链表长度为\r"<<endl;
length(Head);
//链表按值查找
cout<<"请输入待查找的数据\r"<<endl;
scanf("%d", &date);
research_Date(Head, date);
//链表按序号查找
cout<<"请输入待查找序号\r"<<endl;
scanf("%d", &num);
research_Number(Head, num);
//在指定第i1个元素之前插入新元素Newdate
cout<<"在指定第i个元素之前插入新元素Newdate";
cout<<"请输入i与元素且以逗号间隔\r"<<endl;
scanf("%d,%d", &i1, &Newdate_1);
insert_1(Head, i1, Newdate_1);
cout<<"插入后新链表\r"<<endl;
output(Head);
//在指定第i2个元素之后插入新元素Newdate
cout<<"在指定第i个元素之后插入新元素Newdate";
cout<<"请输入i与元素且以逗号间隔\r"<<endl;
scanf("%d,%d", &i2, &Newdate_2);
insert_2(Head, i2, Newdate_2);
cout<<"插入后新链表\r"<<endl;
output(Head);
//指定删除i3元素
cout<<"删除元素的位置\r"<<endl;
scanf("%d", &i3);
Delete_1(Head, i3);
cout<<"删除后新链表\r"<<endl;
output(Head);
//指定删除单链表中某个数据,并统计删除此数据的个数
cout<<"请输入待删除的元素\r"<<endl;
scanf("%d", &Delete_date);
k = Delete_2(Head, Delete_date);
cout<<"删除后新链表\r"<<endl;
output(Head);
cout<<"删除指定元素在链表中的个数为:"<<endl;
cout<<k<<"\r"<<endl;
//单链表逆置
Reverse_list(Head);
cout<<"逆置后输出\r"<<endl;
output(Head);
//单链表的连接
cout<<"建立一个新链表\r"<<endl;
Head_New = creat();
cout<<"输出新链表"<<endl;
output(Head);
cout<<"将新链表连接到原来链表的尾部并输出\r"<<endl;
connect_list(Head, Head_New);
output(Head);
destroy_list(Head);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询