#include
using namespace std;
struct List //定義一個結構體表示線性表
{
int num;
struct List *next;
};
int main()
{
List la,*p,*head,*p1,*pm; //定義線性表la
//把表a的首元素地址賦給p和head
int n=1,m,t;
cout<<"請輸入表a中的第"<<1<<"個元素"<<endl;
cin>>la.num;
p=&la;head=&la;p1=&la;
cout<<"是否繼續輸入,繼續就輸入1,不繼續就輸入0"<<endl;
cin>>t;
if(t!=1&&t!=0)
{
cout<<"輸入錯誤,請重新輸入"<<endl;
cout<<"是否繼續輸入,繼續就輸入1,不繼續就輸入0"<<endl;
cin>>t;
}
if(t)
do
{
p->next =new List;
p=p->next ;
cout<<"請輸入表a中的第"<<n+1<<"個元素"<<endl;
cin>>p->num;
cout<<"是否繼續輸入,繼續就輸入1,不繼續就輸入0"<<endl;
cin>>t;
if(t!=1&&t!=0)
{
cout<<"輸入錯誤,請重新輸入"<<endl;
cout<<"是否繼續輸入,繼續就輸入1,不繼續就輸入0"<<endl;
cin>>t;
}
n++;
}while(t);
p->next =new List;
p=head;
cout<<"總共有"<<n<<"個元素"<<endl;
cout<<"表中元素為"<<endl;
for(int i=1;i<=n;i++)
{
cout<<p->num <<" ";
p=p->next;
}
p=head;
cout<<"表a中的元素已經輸入完成,請輸入要插入新元素的位置數m"<<endl;
cin>>m;
pm=new List;
cout<<"輸入要插入的新元素"<<endl;
cin>>pm->num;
pm->next =p+m;
(p+m-1)->next =pm ;
cout<<p->num <<' '<<(p+m-1)->num <<' '<<pm->num<<endl;
for(int j=1;j<=n;j++)
{
cout<<p1->num<<' ';
p1=p1->next;
}
return 0;
}
1 #include <iostream>
2 using namespace std;
3 struct List //露簍?一賂枚鹿?鹵鉚??鹵鉚
4 {
5 int num;
6 struct List *next;
7 };
8 int main()
9 {
10 List la,*p,*head,*p1,*pm; //露簍???鹵鉚
11 //擄馴鉚??陋?碌?路賂魯賂酶ead
12 int n=1,m,t;
13 cout<<"input "<<1<<"item"<<endl;
14 cin>>la.num;
15 p=&la;head=&la;p1=&la;
16
17 cout<<"1 go on, 0 terminal"<<endl;
18 cin>>t;
19 if(t!=1&&t!=0)
20 {
21 cout<<"err, try again:"<<endl;
22 cout<<"1 go on, 0 terminal"<<endl;
23 cin>>t;
24 }
25
26 if(t)
27 {
28 do
29 {
30 p->next =new List;
31 p=p->next ;
32 cout<<"input"<<n+1<<"item"<<endl;
33 cin>>p->num;
34 cout<<"1 go on, 0 terminal"<<endl;
35 cin>>t;
36 if(t!=1&&t!=0)
37 {
38 cout<<"err, try again:"<<endl;
39 cout<<"1 go on, 0 terminal"<<endl;
40 cin>>t;
41 }
42 n++;
43 }while(t);
44
45 // p->next =new List;
46
47 p=head;
48 cout<<"total"<<n<<"item"<<endl;
49 cout<<"contents:"<<endl;
50 for(int i=1;i<=n;i++)
51 {
52 cout<<p->num <<" ";
53 p=p->next;
54 }
55 p=head;
56 cout<<"insert m"<<endl;
57 cin>>m;
58 pm=new List;
59 cout<<"insert num"<<endl;
60 cin>>pm->num;
61 // pm->next =p+m;
62 // (p+m-1)->next =pm ;
63 struct List *pre = p;
64 struct List *cur = p;
65 for( int i = 0; i <= m; i++ )
66 {
67 if( i == m )
68 {
69 pm->next = cur;
70 pre->next = pm;
71 }
72 pre = cur;
73 cur = cur->next;
74 }
75
76 n++; // ?要錄?
77 cout<<p->num <<' '<<(p+m-1)->num <<' '<<pm->num<<endl;
78
79 for(int j=1;j<=n;j++)
80 {
81 cout<<p1->num<<' ';
82 p1=p1->next;
83 }
84 }
85 return 0;
86 }