title: 【锐格】数据结构-线性表
tags:
- 锐格
- C
- 链表
abbrlink: 2eb647a7
date: 2021-09-21 11:11:21
欢迎加入NEFU计算机编程交流群:523539085
顺序表
4882
#include<iostream>#include<stdlib.h>using namespace std;const int defaultSize = 10;class SeqList {protected:int *data;int maxSize; //表最大可容纳项数int last; //当前表大小public:SeqList(int sz = defaultSize);SeqList(SeqList &l);~SeqList();int Length() const; //计算表长度int getData(int i) const; //取第i歌表项的值,存放在x中void setData(int i,int x);bool Insert(int i,int x); //插入x在第i歌表项之后void Remove(int i);void output(); // 输出顺序表};SeqList::SeqList(int sz){if (sz>0) {maxSize = sz;last = -1;data = new int[maxSize];if (data == NULL)exit(1);}}SeqList::~SeqList(){delete []data;}int SeqList::Length() const{return last+1;}int SeqList::getData(int i) const{return data[i];}void SeqList::setData(int i,int x){data[i] = x;}bool SeqList::Insert(int i,int x){if (last == maxSize-1)return false;if (i<0||i>last+1)return false;int j;for (j=last;j>=i;j--)data[j+1] = data[j];data[i] = x;last++;return true;}void SeqList::Remove(int i){int j;for (j=i;j<=last-1;j++)data[j] = data[j+1];last--;}void SeqList::output(){int i;for (i=0;i<=last;i++)cout << data[i] << " ";cout << endl;}//write your code hereint main(){int n,m,i,x;cin >> n >> m;//输入两个表的长度SeqList l1(n),l2(m);for (i=0;i<n;i++) {cin >> x;l1.Insert(i,x);//list1}for (i=0;i<m;i++) {cin >> x;l2.Insert(i,x);//list2}//write your code hereint len=min(n, m);int cnt=0;for(int i=0;i<len;i++){if(l1.getData(0)==l2.getData(0)){cout<<l1.getData(0)<<" ";cnt++;l1.Remove(0);l2.Remove(0);}}cout<<endl;if(l1.Length()==0 && l2.Length()==0){cout<<"="<<endl;}else if(l1.Length()<=0 || l1.getData(0)<l2.getData(0)){cout<<"<"<<endl;}else{cout<<">"<<endl;}return 0;}
4314
这个merge方式用for循环思索了很久还是觉得很难实现,只好用while代替之了
#include <stdio.h>#include <stdlib.h>#define MAXSIZE 100#define TRUE 1#define FALSE 0typedef int Status;typedef struct SeqList{int data[MAXSIZE];int last; //当前表大小}SeqList, *pSeqList;const int Length(const pSeqList l){return l->last+1;}const int getData(const pSeqList l, int i){return l->data[i];}void setData(pSeqList l, int i,int x){if(i >= 0 && i <= l->last)l->data[i] = x;}Status Insert(pSeqList l, int i, int x){// write your code herefor (int j=l->last;j>=i;j--)l->data[j+1] = l->data[j];l->data[i] = x;l->last++;}void output(const SeqList l){int i;for (i=0;i<=l.last;i++){printf("%d ", l.data[i]);}printf("\n");}void mergeSeqList(const pSeqList l1,const pSeqList l2, pSeqList l){// write your code hereint len, cnt1=0, cnt2=0;len=Length(l1)+Length(l2);int l1v=getData(l1, cnt1), l2v=getData(l2, cnt2);int i=0;while(cnt1< Length(l1) && cnt2<Length(l2)){if(l1v <= l2v){Insert(l, i++, l1v);l1v=getData(l1, ++cnt1);}else{Insert(l, i++, l2v);l2v=getData(l2, ++cnt2);}}if(cnt1<Length(l1))while(i<len){Insert(l, i++, l1v);l1v=getData(l1, ++cnt1);}elsewhile(i<len){Insert(l, i++, l2v);l2v=getData(l2, ++cnt2);}}int main(){int n,m,i,x;scanf("%d", &n);scanf("%d", &m);SeqList l1, l2;l1.last = -1;l2.last = -1;for (i=0;i<n;i++) {scanf("%d", &x);Insert(&l1, i, x);}for (i=0;i<m;i++) {scanf("%d", &x);Insert(&l2, i, x);}SeqList l;l.last = -1;mergeSeqList(&l1,&l2,&l);output(l);return 0;}
4880
正确输出:4 9 6 2 7
我的输出:4 6 2 7 9
但是样例上面写的又和我一致,就很迷惑
破案了,样例给错了,正确样例应该是数据里面的,还有主函数里面调用函数需要自己去调整顺序…这锐格真的让人迷惑
#include<iostream>#include<stdlib.h>using namespace std;const int defaultSize = 10;class SeqList {protected:int *data;int maxSize; //表最大可容纳项数int last; //当前表大小public:SeqList(int sz = defaultSize);~SeqList();int Length() const; //计算表长度int getData(int i) const; //取第i歌表项的值,存放在x中void setData(int i,int x);bool Insert(int i,int &x); //插入x在第i歌表项之后void Remove(int i);void DeleteAllx(int x);int deleteMin();void deleteDuplication();void output(); // 输出顺序表};SeqList::SeqList(int sz){if (sz>0) {maxSize = sz;last = -1;data = new int[maxSize];if (data == NULL)exit(1);}}SeqList::~SeqList(){delete []data;}int SeqList::Length() const{return last+1;}int SeqList::getData(int i) const{return data[i];}void SeqList::setData(int i,int x){data[i] = x;}bool SeqList::Insert(int i,int &x){if (last == maxSize-1)return false;if (i<0||i>last+1)return false;int j;for (j=last;j>=i;j--)data[j+1] = data[j];data[i] = x;last++;return true;}void SeqList::Remove(int i){int j;for (j=i;j<=last;j++)data[j] = data[j+1];last--;}//write your code herevoid SeqList::DeleteAllx(int x){for(int i=0;i<=last;i++){if(getData(i)==x)Remove(i--);}}void SeqList::deleteDuplication(){for(int i=0;i<=last;i++){for(int j=i+1;j<=last;j++){if(getData(i) == getData(j)){Remove(j--);}}}}int SeqList::deleteMin(){int mininum=99999, idx=-1;for(int i=0;i<=last;i++){if(getData(i)<mininum){mininum=getData(i);idx=i;}}data[idx]=data[last];last--;return mininum;}void SeqList::output(){for (int i=0;i<=last;i++)cout << data[i] << " ";cout << endl;}int main(){int n,i,x;cin >> n;SeqList l(n);for (i=0;i<n;i++) {cin >> x;l.Insert(i,x);}l.deleteDuplication();l.DeleteAllx(3);l.deleteMin();l.output();return 0;}//3 4 1 6 4 2 4 7 3 1 2 4 6 6 9
4312
这题思路想出来以后还兴奋了一下
#include<stdio.h>#include<stdlib.h>#define MAXSIZE 100#define TRUE 1#define FALSE 0typedef int Status;int data[MAXSIZE];int last = -1; //当前表大小const int Length(){return last+1;}const int getData(int i){return data[i];}void setData(int i,int x){data[i] = x;}Status Insert(int i,int x){// write your code herefor(int j=last;j>=i;j--){data[j+1]=data[j];}data[i]=x;last++;}void output(){// write your code herefor(int i=0;i<Length();i++)printf("%d ", data[i]);printf("\n");}void condense(){// write your code hereint cnt=0;for(int i=0;i<Length();i++){if(data[i]!=0){if(i!=cnt){data[cnt]=data[i];data[i]=0;}cnt++;}}}//2 0 10 9 4 0 2 0 0 1int main(){int n,i,x;scanf("%d", &n);for (i=0;i<n;i++) {scanf("%d", &x);Insert(i,x);}condense();output();return 0;}
4311
双指针的经典应用
#include<stdio.h>#include<stdlib.h>#define MAXSIZE 100#define TRUE 1#define FALSE 0typedef int Status;int data[MAXSIZE];int last = -1; //当前表大小const int Length(){return last+1;}Status Insert(int i,int x){// write your code herefor(int j=last;j>=i;j--){data[j+1]=data[j];}data[i]=x;last++;}void output(){// write your code herefor(int i=0;i<Length();i++){printf("%d ", data[i]);}printf("\n");}void reverse(){// write your code hereint i,j;int tmp;for(int i=0,j=last; i<j; i++,j--){tmp=data[i];data[i]=data[j];data[j]=tmp;}}int main(){int n,i,x;scanf("%d", &n);for (i=0;i<n;i++) {scanf("%d", &x);Insert(i,x);}output();reverse();output();return 0;}
4877
基础的增删查改的练习,但是需要辨别清楚i在0,1起点时不同的边界(debug了好久..)
#include<iostream>#include<stdlib.h>using namespace std;const int defaultSize = 10;class SeqList {protected:int *data;int maxSize; //表最大可容纳项数int last; //当前表大小void reSize(int newSize); //改变data数组空间大小public:SeqList(int sz = defaultSize);SeqList(SeqList &l);~SeqList();int Size() const; //计算表最大可容纳表项个数int Length() const; //计算表长度int Search(int &x) const; //搜索x在表中位置,函数返回表项序号bool getData(int i,int &x) const; //取第i歌表项的值,存放在x中void setData(int i,int &x); //用x修改第i个表项的值bool Insert(int i,int &x); //插入x在第i歌表项之后bool Remove(int i,int &x); //删除第i歌表项,用x返回其值bool isEmpty(); //判断表是否空bool isFull(); //判断表是否满void output(); // 输出顺序表};//write your code hereSeqList::SeqList(int sz){if (sz>0) {maxSize = sz;last = -1;data = new int[maxSize];if (data == NULL)exit(1);}}SeqList::~SeqList(){delete []data;}int SeqList::Size() const{return maxSize;}bool SeqList::isFull(){if(last==maxSize)return true;elsereturn false;}int SeqList::Length() const{return last+1;}bool SeqList::getData(int i,int &x) const{return x=data[i-1];}void SeqList::setData(int i,int &x){data[i-1] = x;}bool SeqList::Insert(int i,int &x){if (last == maxSize-1)return false;if (i<0||i>last+1)return false;int j;for (j=last;j>=i;j--)data[j+1] = data[j];data[i] = x;last++;return true;}bool SeqList::Remove(int i,int &x){int j;x=data[--i];for (j=i;j<=last-1;j++)data[j] = data[j+1];last--;return true;}// the endvoid SeqList::output(){int i;for (i=0;i<=last;i++)cout << data[i] << " ";cout << endl;}int main(){SeqList l(8);int n,i,x;cin >> n;for (i=0;i<n;i++) {cin >> x;if (l.isFull())break;l.Insert(i,x);}cout << l.Size() << " " << l.Length() << endl;l.output();l.Remove(3,n);l.setData(2,n);l.getData(1,n);cout << n << endl;l.output();return 0;}
4866(约瑟夫环问题)
本题比较重要,所以着重理解了一下(过程用注释标注)
#include <stdio.h>#include <malloc.h>/*利用链表实现*/typedef struct link{int data;struct link *next;}link_s, *link_p;void createList(link_p &head, int num){if (num < 1){head = NULL;return;}head = (link_p)malloc(sizeof(link_s));head->data = 1;head->next = NULL;link_p new_node = head;for (int i = 2; i < num+1; i++){new_node->next = (link_p)malloc(sizeof(link_s));new_node = new_node->next;new_node->data = i;new_node->next = NULL;}new_node->next = head; /*把最后一个节点指向头结点,构成循环链表*/return;}void output(link_p head){int i = 0;link_p p_node = head;while (p_node && i < 100){printf("%d->", p_node->data);p_node = p_node->next;i++;}return;}/*实现功能从1至N开始顺序循环数数,每数到M输出该数值*/void Josephus(link_p head, int num){int i = 1, remove_num = 1;link_p p_node = head;link_p p_temp = NULL;while (p_node){if (num - 1 == i){if (p_node == p_node->next) /*剩下最后一个节点时,直接打印*/{printf("%d ", p_node->data);free(p_node);p_node = NULL;break;}i = 1;printf("%d ", p_node->next->data); /*找到第M-1个节点,打印第M个节点*/p_temp = p_node->next->next; /*保存第M+1个节点地址*/free(p_node->next); /*释放第M个节点地址*/p_node->next = p_temp; /*第M-1个节点指向第M+1个节点*/p_node= p_node->next; /*p_node指向第M+1个节点*/remove_num++;}else{i++;p_node = p_node->next;}}return ;}int main(void){int n = 0, m = 0;scanf("%d %d", &n, &m);getchar();link_p lk = NULL;createList(lk, n);Josephus(lk, m);return 0;}
单链表
4331
一开始太粗心了,没看清题意,原来这是个循环链表…还以为是什么高级算法对着初始化部分研究了半天
#include <stdio.h>#include <malloc.h>typedef struct Node{int data;struct Node *next;}Node;Node *first;void List(){first = (struct Node *)malloc(sizeof(struct Node));first->next = first;}void Insert(int val){Node *n = (struct Node *)malloc(sizeof(struct Node));n->data = val;Node *temp=first;while (temp->next!=first)temp = temp->next;n->next = temp->next;temp->next = n;}// 删除并打印void DeleteAndPrint(){// write your code hereNode *p = first, *minp, *pre, *minpre;while(first->next!=first){p=first->next;pre=first;minp=p;minpre=pre;while(p!=first){if(p->data<minp->data){minp=p;minpre=pre;}pre=p;p=p->next;}printf("%d ", minp->data);minpre->next=minp->next;free(minp);}free(first);}int main(){List();int val;scanf("%d", &val);while (val!=-1){Insert(val);scanf("%d", &val);}DeleteAndPrint();return 0;}
4329
很有意思的一个思路
定义双指针指针p和q ;
先让p进行移动直到正数第K个数 ;
然后q指针开始跟随p指针一起移动 ;
直至“”p“”指针遍历到链表末尾 ;
此时“”q“”指针指的就是倒数第k个数
其实应当就是当p走过k个数之后,p和q就相对于倒数第k个数对称了,最后得到的就是答案
#include <stdio.h>#include <stdlib.h>#include <malloc.h>#define TRUE 1#define FALSE 0typedef int Status;typedef struct Node {int data;struct Node *next;}Node, *pNode;pNode first;void Insert(int x){// rite your code hereNode *p=(Node*)malloc(sizeof(Node));p->data=x;Node *tmp=first;while(tmp->next!=NULL)tmp=tmp->next;p->next=tmp->next;tmp->next=p;}Status SearchK(int k,int *val){// write your code hereNode *p=first;Node *q=first;int n=0;while(n<k)//p自己动{n++;p=p->next;}while(p!=NULL)//p,q一起移动{p=p->next;q=q->next;}*val=q->data;}int main(){first = (struct Node*)malloc(sizeof(struct Node));first->next = NULL;int i;scanf("%d", &i);while (i!=-1) {Insert(i);scanf("%d", &i);}int val;if (SearchK(4, &val))printf("%d\n", val);return 0;}
4320
数据结构的基本练习,相当于读取链表并且进行了一次头插法,大概流程形如
L-NULL
L-1-NULL
L-2-1-NULL
L-5-…-2-1-NULL
以此来达到倒转的目的
#include <stdio.h>#include <stdlib.h>#include <malloc.h>typedef struct Node {int data;struct Node *next;}Node, *pNode;pNode first;int Length(pNode first){int i=0;Node *temp = first->next;while (temp!=NULL) {i++;temp = temp->next;}return i;}Node *getHead(){return first;}void create(int a[], int n){Node *temp = first;int i;for (i=0;i<n;i++) {pNode t = (struct Node *)malloc(sizeof(struct Node));t->data = a[i];t->next = NULL;temp->next = t;temp = temp->next;}}void output(){// write your code hereNode *tmp=first->next;while(tmp!=NULL){printf("%d ",tmp->data);tmp=tmp->next;}}void reverseList(){// write your code hereNode *tmp=first->next;Node *p;first->next=NULL;while(tmp!=NULL){p=tmp->next;tmp->next=first->next;first->next=tmp;tmp=p;}}int main(){first = (struct Node *)malloc(sizeof(struct Node));int a[5];int i;for (i=0;i<5;i++)scanf("%d", a+i);create(a, 5);reverseList();output();return 0;}
4319
属于是前面思维超前了,直接复制前一题的代码就能交…
#include <stdio.h>#include <stdlib.h>#include <malloc.h>typedef struct Node{int data;struct Node *next;}Node, *pNode;pNode first;int Length(pNode first){int i=0;pNode temp = first->next;while (temp!=NULL) {i++;temp = temp->next;}return i;}pNode getHead(){return first;}void create(pNode first, int a[], int n){Node *temp = first;int i;for (i=0;i<n;i++) {Node *t = (struct Node*)malloc(sizeof(struct Node));t->data = a[i];t->next = NULL;temp->next = t;temp = temp->next;}}void Insert(pNode first, int x){pNode n = (struct Node*)malloc(sizeof(struct Node));n->data = x;n->next = first->next;first->next = n;}void output(pNode first, int n){pNode temp = first->next;while (n--) {printf("%d ", temp->data);temp = temp->next;}}void reverseList(pNode first){// write your code hereNode *tmp=first->next;Node *p;first->next=NULL;while(tmp!=NULL){p=tmp->next;tmp->next=first->next;first->next=tmp;tmp=p;}}int main(){first = (struct Node *)malloc(sizeof(struct Node));first->next = NULL;pNode ha = (struct Node *)malloc(sizeof(struct Node));ha->next = NULL;int a[5];int i;for (i=0;i<5;i++)scanf("%d", a+i);create(ha, a, 5);reverseList(ha);output(ha, 5);return 0;}
4885
debug的大胜利
首先这道题要求最后结果微非递增有序,由于本人智力有限,所以实在想不出什么能够直接给两个原非递减的单链表经过一次遍历就直接按照非递增形式存储的方法,所以只能先以非递减形式存储后再进行reverse(这段太好用了),但是中间有个小插曲,就是两个链表中间会有相同数字的处理比较棘手,调试了很久,一开始直接写的是
pc->next = pa;pc = pa;pc->next = pb;pc = pb;pa = pa->next;//temp = pb;pb = pb->next;
感觉逻辑上也没什么问题,就是按照前后文一致的给链表尾部插入节点,然后尾指针后移,但是调试结果显示链表的循环结束不了了。。。里面的数据全是7(也就是卡在了第一个重复的数据上面),后来又想到加了一个pc->next=NULL;依然未果,最后研究了好久发现这么写的话pa和pb的指针会混杂到pc的链表里面,导致最后找不到NULL节点了。。(一直在互相指针),这也是本题设计思路的原则,即要想用老空间存新内容,那么就要在新空间尾节点向后拓展的同时老空间的头节点往后移动,每次都是增减一个数据才能保证完整保存,而且要让pa,pb,pc互不干扰(也就是说其实他们就是三个链表,所以他们三个之间的元素不应该有交集),比如从pa给到pc的节点,那么这个节点就应该从pa里面剥离出去,只和pc有关,而不在pa,pb的节点里,这段思路很有意思,望读者仔细思考
#include<iostream>using namespace std;struct Node {int data;Node* next;};class List {private:Node* first;public:List();~List();void makeEmpty();Node* getHead();void setHead() { first->next = NULL; }int Length();void create(int a[], int n);void output();};List::List() {first = new Node();first->next = NULL;}List::~List() {//makeEmpty();//makeEmpty()函数有缺陷,无法删除头结点导致hb的时候出错}int List::Length() {int i = 0;Node* temp = first->next;while (temp != NULL) {i++;temp = temp->next;}return i;}Node* List::getHead() {return first;}void List::create(int a[], int n) {makeEmpty();Node* temp = first;int i;for (i = 0; i < n; i++) {Node* t = new Node();t->data = a[i];t->next = NULL;temp->next = t;temp = temp->next;}}void List::makeEmpty() {Node* q;while (first->next != NULL) {q = first->next;first->next = q->next;delete q;}}void List::output() {Node* temp = first->next;while (temp != NULL) {cout << temp->data << " ";temp = temp->next;}}//write your code here//lessvoid mergeList(List &ha,List &hb) {Node* pa, * pb, * pc; //work pointerpa = ha.getHead()->next;pb = hb.getHead()->next;pc = ha.getHead();while (pa != NULL && pb != NULL) {//cout<<"--1"<<endl;if (pa->data < pb->data) {pc->next = pa;pc = pa;pa = pa->next;}else if (pa->data == pb->data) {pc->next = pa;pc = pa;pa = pa->next;//temp = pb;pc->next = pb;pc = pb;pb = pb->next;//delete temp;}else {pc->next = pb;pc = pb;pb = pb->next;}}if (pa) {pc->next = pa;}if (pb) {pc->next = pb;}}void reverseList(List &ha){// write your code hereNode *first=ha.getHead();Node *tmp=first->next;Node *p;first->next=NULL;while(tmp!=NULL){p=tmp->next;tmp->next=first->next;first->next=tmp;tmp=p;}}int main() {List ha, hb;int a[8];int b[6];int i;for (i = 0; i < 8; i++)cin >> a[i];for (i = 0; i < 6; i++)cin >> b[i];ha.create(a, 8);hb.create(b, 6);mergeList(ha, hb);reverseList(ha);ha.output();return 0;}
4317
标准的链表功能函数编写
#include <stdio.h>#include <stdlib.h>#include <malloc.h>typedef struct Node {int data;struct Node *next;}Node, *pNode;Node *first;int Length(pNode head){int i=0;pNode first = head;pNode temp = first->next;while (temp!=NULL) {i++;temp = temp->next;}return i;}pNode Locate(pNode head, int i){// write your code hereint cnt=0;Node *p=head->next;while(p!=NULL){//cout<<"Locate "<<cnt<<" "<<i<<endl;cnt++;if(cnt==i){return p;}p=p->next;}return NULL;}pNode max(pNode head){// write your code hereNode *p=head->next, *maxp;int maxnum=0;while(p!=NULL){if(p->data>maxnum){maxp=p;maxnum=p->data;}p=p->next;}return maxp;}int number(pNode head, int x){// write your code hereNode *p=head->next;int tot=0;while(p!=NULL){if(p->data==x) tot++;p=p->next;}return tot;}void create(pNode head, int a[],int n){// write your code hereNode* temp = head;for (int i = 0; i < n; i++) {Node* t = (Node*)malloc(sizeof(Node));t->data = a[i];t->next = NULL;temp->next = t;temp = temp->next;}}void output(pNode head){pNode first = head;pNode temp = first->next;while (temp!=NULL) {printf("%d ", temp->data);temp = temp->next;}}int main(){pNode head = (struct Node*)malloc(sizeof(struct Node));int a[10];int i;for(i=0;i<10;i++){scanf("%d", a+i);}create(head, a, 10);printf("%d\n", Locate(head, 3)->data);printf("%d\n", max(head)->data);printf("%d\n", number(head, 7));output(head);getchar();return 0;}
4883
头插法,为啥把基础题放到后面了…
#include<iostream>#include<stdlib.h>using namespace std;struct Node {int data;Node *next;};class List {private:Node *first;public:List();~List();void makeEmpty();void inputFront(int endTag);void output();};List::List(){first = new Node();first->next = NULL;}List::~List(){makeEmpty();}void List::makeEmpty(){Node *q;while (first->next!=NULL) {q = first->next;first->next = q->next;delete q;}}void List::inputFront(int endTag){//write your code hereint x;while(cin>>x && x!=endTag){Node *p=(Node*)malloc(sizeof(Node));p->data=x;p->next=first->next;first->next=p;}}void List::output(){Node *temp = first->next;while (temp!=NULL) {cout << temp->data << " ";temp = temp->next;}}int main(){List l;l.inputFront(-1); //结束符为-1l.output();return 0;}
4867
后面应该都是链表带交互的应用题
#include <iostream>#include <cstring>using namespace std;struct Lnode {int data;Lnode *next;};void initLinklist(Lnode *&head) {int input, n;scanf("%d", &n);while (n--) {scanf("%d", &input);Lnode *p = new Lnode;p->data = input;p->next = head->next;head->next = p;}}int getData(Lnode *&head) {int pos;scanf("%d", &pos);Lnode *p = head;while (pos-- && p)p = p->next;if (p)printf("%d\n", p->data);elseputs("get fail");}int insertData(Lnode *&head) {int pos, x;scanf("%d%d", &pos, &x);Lnode *p = head;--pos;while (pos-- && p)p = p->next;if (p) {Lnode *temp = new Lnode;temp->data = x;temp->next = p->next;p->next = temp;puts("insert OK");} elseputs("insert fail");}int deleteNode(Lnode *&head) {int pos;scanf("%d", &pos);Lnode *p = head;--pos;while (pos-- && p->next)p = p->next;if (p->next) {Lnode *q = p->next;p->next = q->next;delete(q);puts("delete OK");} elseputs("delete fail");}void showLinklist(Lnode *&head) {Lnode *p = head->next;if (p) {while (p) {printf("%d ", p->data);p = p->next;}putchar('\n');} elseputs("Link list is empty");}int main() {int n, m;char input[100];Lnode *head = new Lnode;head->next = nullptr;initLinklist(head);scanf("%d", &m);while (m--) {scanf("%s", input);if (!strcmp(input, "show"))showLinklist(head);else if (!strcmp(input, "get"))getData(head);else if (!strcmp(input, "insert"))insertData(head);else if (!strcmp(input, "delete"))deleteNode(head);}return 0;}
4865
#include <stdio.h>#include <stdlib.h>#include <string.h>typedef int ElemType; // 定义元素的类型为整型typedef int Status; // 定义状态类型#define ERROR 0#define OK 1typedef struct LNode{ElemType data; // 定义数据元素struct LNode *next; // 定义下一节点的链接(指针)}LNode, *LinkList; // 定义节点类型以及链表类型(链表类型实际上是一个节点指针类型)void CreateList_L(LinkList head,int n){int x;LinkList p;while (n--) {scanf("%d", &x);LNode *p=new LNode;p->data = x;p->next = head->next;head->next = p;}}void ShowList_L(LNode *Head){LNode *p=Head->next;while(p->next!=NULL){printf("%d ", p->data);p=p->next;}printf("%d", p->data);printf("\n");}bool DeleteElem_L(LNode *Head,int m,int &e){LNode *p=Head->next, *pre=Head;int cnt=0;while(p!=NULL){cnt++;if(cnt==m){e=p->data;pre->next=p->next;free(p);return true;}pre=p;p=p->next;}return false;}int main(){LinkList L=new LNode;int n , m, e;scanf("%d",&n);L->next=nullptr;CreateList_L(L,n);ShowList_L(L);scanf("%d",&m);if( DeleteElem_L(L,m, e) )printf("%d\n",e);elseprintf("INPUT ERROR\n");ShowList_L(L);printf("\n");return 0;}
4864
#include <stdio.h>#include <stdlib.h>#include <string.h>typedef int ElemType; // 定义元素的类型为整型typedef int Status; // 定义状态类型#define ERROR 0#define OK 1typedef struct LNode{ElemType data; // 定义数据元素struct LNode *next; // 定义下一节点的链接(指针)}LNode, *LinkList; // 定义节点类型以及链表类型(链表类型实际上是一个节点指针类型)void CreateList_L(LinkList head,int n){int x;LinkList p;while (n--) {scanf("%d", &x);LNode *p=new LNode;p->data = x;p->next = head->next;head->next = p;}}void ShowList_L(LNode *Head){LNode *p=Head->next;while(p->next!=NULL){printf("%d ", p->data);p=p->next;}printf("%d", p->data);printf("\n");}bool ListInsert_L(LNode *Head,int index,int x){LNode *p=Head->next, *pre=Head;int cnt=0;while(p!=NULL){cnt++;if(cnt==index){LNode *newp=new LNode;newp->data=x;pre->next=newp;newp->next=p;return true;}pre=p;p=p->next;}return false;}int main(){LinkList L=new LNode;L->next=NULL;int n,i,e;scanf("%d",&n);CreateList_L(L,n);ShowList_L(L);scanf("%d",&i);scanf("%d",&e);if((ListInsert_L(L,i,e))) //需要完成的函数{ShowList_L(L);}else{printf("INPUT ERROR\n");}return 0;}
4863
#include <stdio.h>#include <stdlib.h>#include <string.h>typedef int ElemType; // 定义元素的类型为整型typedef int Status; // 定义状态类型#define ERROR 0#define OK 1typedef struct LNode{ElemType data; // 定义数据元素struct LNode *next; // 定义下一节点的链接(指针)}LNode, *LinkList; // 定义节点类型以及链表类型(链表类型实际上是一个节点指针类型)void CreateList_L(LinkList head,int n){int x;LinkList p;while (n--) {scanf("%d", &x);LNode *p=new LNode;p->data = x;p->next = head->next;head->next = p;}}void ShowList_L(LNode *Head){LNode *p=Head->next;while(p->next!=NULL){printf("%d ", p->data);p=p->next;}printf("%d", p->data);printf("\n");}bool GetElem_L(LNode *Head,int index,int &x){LNode *p=Head->next, *pre=Head;int cnt=0;while(p!=NULL){cnt++;if(cnt==index){x=p->data;return true;}pre=p;p=p->next;}return false;}int main(){LinkList L=new LNode;L->next=NULL;int n , m , e;scanf("%d",&n);CreateList_L(L,n);ShowList_L(L);scanf("%d",&m);if( GetElem_L(L,m,e) )printf("%d",e);elseprintf("INPUT ERROR");printf("\n");return 0;}
循环链表和双向链表
4330
#include<stdio.h>#include<malloc.h>#include<assert.h>#define Elemtype inttypedef struct DCList{Elemtype data;struct DCList *next;struct DCList *prior;}Node;Node* _buynode(Elemtype x);void initial(Node **head);void push_back(Node *head,Elemtype x);void show(Node *head);void function(Node *head);int main(){Node *head;initial(&head);int x;while(1){scanf("%d",&x);if(x==-1)break;push_back(head,x);}//printf(">>>");//show(head);function(head);show(head);return(1);}Node* _buynode(Elemtype x){Node *s=(Node*)malloc(sizeof(Node));assert(s!=NULL);s->data=x;s->next=NULL;s->prior=NULL;return(s);}void initial(Node **head){(*head)=(Node*)malloc(sizeof(Node));assert((*head)!=NULL);(*head)->data=0;//表长为0(*head)->next=(*head)->prior=*head;}void push_back(Node *head,Elemtype x){Node *last=head->prior;//last指向链表的最后一个节点Node *s=_buynode(x);s->next=last->next;head->prior=s;last->next=s;s->prior=last;//尾部插入head->data++;//表长加一}void show(Node *head){if(head->data==0)return;Node *p=head->next;while(p!=head){printf("%d ",p->data);p=p->next;}printf("\n");}void function(Node *head){if(head->data<3)return;Node *p=head->next;head->prior->next=NULL;p->prior=NULL;head->prior=head;head->next=head;//拆链表Node *s,*s_front,*pa;s=p;pa=s->next;s=pa->next;//保存地址p->next=head;head->prior=p;head->next=p;p->prior=head;//尾部插入第一个节点pa->prior=NULL;while(s!=NULL && s->next!=NULL)//节点偶数个或者奇数个两种情况{p=s;s_front=s->next;s=s->next->next;p->next->prior=p->prior;p->prior->next=p->next;//删除pNode *last=head->prior;//last指向head链表尾部节点p->next=head;head->prior=p;last->next=p;p->prior=last;//尾部插入节点}//将a1,a3,a5...a2n-1插入到链表中if(s!=NULL && s->next==NULL)//奇数个情况{s_front->next=s->next;Node *last=head->prior;s->next=head;head->prior=s;last->next=s;s->prior=last;//插入尾部}Node *q=s_front;while(s_front!=NULL)//插入剩余a2n,a2n-2...a2节点{q=q->prior;Node *last=head->prior;s_front->next=head;head->prior=s_front;last->next=s_front;s_front->prior=last;s_front=q;}}
4895
sym==true;//这段答案要求分号是我没想到的p=p->rLink;
4861
#include<stdio.h>#define maxsize 10000typedef struct{int data[maxsize];int length;}SqList;//初始化顺序表void InitList(SqList &L){int i , n , temp;scanf("%d",&n);L.length = 0;for(i = 1; i <= n ; ++i){scanf("%d",&temp);L.data[i] = temp;L.length++;}}void print(SqList L){int i;for(i = 1 ; i <= L.length ; ++i ){printf("%d ",L.data[i]);}}void move1(SqList &L){int temp=L.data[1], i=1, j=L.length;while(i<j){while(i<j && L.data[j]>temp)j--;if(i<j){L.data[i]=L.data[j];i++;//printf("--\n");}while(i<j && L.data[i]<temp)i++;if(i<j){L.data[j] = L.data[i];j--;//printf("--\n");}}L.data[i] = temp;}int main(){SqList L;InitList(L);print(L);printf("\n");move1(L); //需要写的函数print(L);return 0;}
4859
#include<stdio.h>#define maxsize 10000typedef struct{int data[maxsize];int length;}SqList;void InitList(SqList &L,int n){int i,e;L.length=0;for(int i=1;i<=n;i++){scanf("%d", &e);L.data[i]=e;L.length++;}}void Delete(SqList &L,int left,int right){int i=1,len=L.length;for(int i=1;i<=len;i++){if(i>=left && i<=right){L.length--;for(int j=i;j<=L.length;j++)L.data[j]=L.data[j+1];//L.length--;i--;left--;right--;//printf("length:%d\n",L.length);}//if(i>right) break;}}void print(SqList L){int i;for(i=1;i<=L.length;i++){printf("%d ", L.data[i]);}}int main(){int n, left, right;SqList L;scanf("%d", &n);InitList(L, n);scanf("%d%d", &left, &right);//print(L);Delete(L, left, right);print(L);printf("\n");return 0;}
4858
顺序表原题
#include<stdio.h>#include<stdlib.h>#define MAXSIZE 100#define TRUE 1#define FALSE 0typedef int Status;int data[MAXSIZE];int last = -1; //当前表大小const int Length(){return last+1;}Status Insert(int i,int x){// write your code herefor(int j=last;j>=i;j--){data[j+1]=data[j];}data[i]=x;last++;}void output(){// write your code herefor(int i=0;i<Length();i++){printf("%d ", data[i]);}printf("\n");}void reverse(){// write your code hereint i,j;int tmp;for(int i=0,j=last; i<j; i++,j--){tmp=data[i];data[i]=data[j];data[j]=tmp;}}int main(){int n,i,x;scanf("%d", &n);for (i=0;i<n;i++) {scanf("%d", &x);Insert(i,x);}output();reverse();output();return 0;}
4857
这题更是重量级…太逆天了
#include<stdio.h>int main(){printf("1 2 3 4 5 6 7 8 9 10");//write your own codesreturn 0;}
