问题 1676: 算法2-8~2-11:链表的基本操作
题目链接:https://www.dotcpp.com/oj/problem1676.html
题目描述 链表是数据结构中一种最基本的数据结构,它是用链式存储结构实现的线性表。它较顺序表而言在插入和删除时不必移动其后的元素。现在给你一些整数,然后会频繁地插入和删除其中的某些元素,会在其中某些时候让你查找某个元素或者输出当前链表中所有的元素。 下面给你基本的算法描述:



3 3 2 1 21 show delete 1 show delete 2 show delete 1 show delete 2 insert 2 5 show insert 1 5 show insert 1 7 show insert 2 5 show insert 3 6 show insert 1 8 show get 2样例输出
1 2 3 delete OK 2 3 delete OK 2 delete OK Link list is empty delete fail insert fail Link list is empty insert OK 5 insert OK 7 5 insert OK 7 5 5 insert OK 7 5 6 5 insert OK 8 7 5 6 5 7
感觉第一次把链表搞懂。。。
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。1 #include <iostream> 2 #include <algorithm> 3 #include <string> 4 #include <cstring> 5 using namespace std; 6 struct node 7 { 8 int data; 9 node* next; 10 }; 11 int x,y,n,m; 12 string s; 13 //创建链表 14 node* create(int n) 15 { 16 node *head,*p; 17 head=new node; 18 head->next=NULL; 19 while(n--){ 20 cin>>x; 21 p=new node; 22 p->data=x; 23 p->next=head->next; 24 head->next=p; 25 } 26 return head; 27 } 28 //展示链表各元素的值 29 void show(node* head) 30 { 31 int flag=0; 32 node* p=head; 33 p=p->next; 34 if(p==NULL){ 35 cout<<"Link list is empty"<<endl; 36 return ; 37 } 38 while(p!=NULL){ 39 cout<<p->data<<" "; 40 p=p->next; 41 } 42 cout<<endl; 43 } 44 //删除第t个结点 45 int del(node* head,int t) 46 { 47 if(t<=0) return 0; 48 int flag=0; 49 node* p=head; 50 node* q; 51 int cnt=1; 52 while(p!=NULL){ 53 if(cnt==t){ 54 q=p->next; 55 p->next=q->next; 56 flag=1; 57 break; 58 }else{ 59 p=p->next; 60 cnt++; 61 } 62 } 63 return flag; 64 } 65 //在第pos个位置添加值为value的结点 66 int insert(node* head,int pos,int value) 67 { 68 if(pos<=0) return 0; 69 int flag=0; 70 node* p=head; 71 node* q; 72 int cnt=1; 73 while(p!=NULL){ 74 if(cnt==pos){ 75 q=new node; 76 q->data=value; 77 q->next=p->next; 78 p->next=q; 79 flag=1; 80 break; 81 }else{ 82 p=p->next; 83 cnt++; 84 } 85 } 86 return flag; 87 } 88 //获取第t个结点的值 89 int get(node* head,int t) 90 { 91 if(t<=0) return 0; 92 int flag=0; 93 node* p=head; 94 node* q; 95 p=p->next; 96 int cnt=1; 97 while(p!=NULL){ 98 if(cnt==t){ 99 return p->data; 100 }else{ 101 p=p->next; 102 cnt++; 103 } 104 } 105 return flag; 106 } 107 //这是我用来测试的可以忽略 108 void test() 109 { 110 //int a[5]={1,2,3,4,5}; 111 node* head=create(5); 112 show(head); 113 cout<<del(head,3)<<endl; 114 show(head); 115 cout<<insert(head,1,1000)<<endl; 116 show(head); 117 cout<<insert(head,2,555)<<endl; 118 show(head); 119 cout<<get(head,1)<<endl; 120 show(head); 121 } 122 int main() 123 { 124 while(cin>>n){ 125 node* head=create(n); 126 cin>>m; 127 while(m--){ 128 cin>>s; 129 if(s[0]=='s'){ 130 show(head); 131 }else if(s[0]=='d'){ 132 cin>>x; 133 if(del(head,x)){ 134 cout<<"delete OK"<<endl; 135 }else{ 136 cout<<"delete fail"<<endl; 137 } 138 }else if(s[0]=='i'){ 139 cin>>x>>y; 140 if(insert(head,x,y)){ 141 cout<<"insert OK"<<endl; 142 }else{ 143 cout<<"insert fail"<<endl; 144 } 145 }else if(s[0]=='g'){ 146 cin>>x; 147 if(get(head,x)){ 148 cout<<get(head,x)<<endl; 149 }else{ 150 cout<<"get fail"<<endl; 151 } 152 } 153 } 154 } 155 return 0; 156 }

更多精彩