20年833
20年8331. 给一个带头结点单链表,删除所有值为k的节点1234567891011121314151617181920struct ListNode{ int val; struct ListNode *next;};ListNode * removeElements(ListNode *head,int k){ if(head==NULL) return NULL; ListNode *p=head; ListNode *q; while(p->next!=NULL)//用p来遍历链表 { if(p->next->val==k){//p的后一个节点为要删除的结点 q=p->next; p->next=p->next->next; free(q);//删除值为k的结点,并释放内存 } else p=p-&g...
16-17年833
16-17年833
22真题
22真题5. (1)使用C语言给出双链表的数据结构(2)写出删除结点的C语言实现1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253struct node{ struct node *prev; int data; struct node *next;}void deletion_beginning(node *head){ struct node *ptr; if(head==NULL) { printf("UNDERFLOW\n"); }else if(head->next==NULL){ head=NULL; free(head); printf("not delete"); } else{ ...
19-20真题
19-20真题自操作表,指表中元素被find函数访问到,就自动移动到表头,并保持其他元素顺序不变 用数组存储结构写find功能 写链表存储结构的find功能 1234567891011121314151617181920212223242526272829303132void Find(char a[],int n,char e){ //n为数组长度,e为要查找的元素 for(int i=0;i<n;i++){ if(a[i]==e){ //找到元素之后 for(int j=i-1;j>=0;j--){ a[j+1]=a[j];//i-1位置之前的所有结点后移 } a[0]=e; } }}struct LNode{ char data;//数据域,保存结点的值 struct LNode *next;//指...
16-18真题
16-18真题1. 循环双链表,结点previous,data,next和访问频度域freq,初试为0,每当链表进行一次Locate(L,x)运算时,令x结点freq域的值加1,并使其链表结点频度按递减顺序排序,并实现Locate(L,x)。123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960//1. 找到指定结点 2. 访问频度+1 3. 进行排序#include<iostream>using namespace std;const int N=100010;struct DNode{ int data; int freq; struct DNode *next; struct DNode *prior;};DNode *h;void sort(DNode *h){//根据freq降序排列,写成一个函数 DNode *p,*q,*pre; ...
12-15真题
12-15真题求数列1-1/2+1/3-1/4+…1/n123456789101112131415161718192021222324/*1. 输入n,即为终止数列的数字2. 循环判定数字,分母为奇数时,系数为正,分母为偶数时,系数为负3. 最后在循环的过程中执行数字的累加,最后输出结果*/#include<iostream>using namespace std;int main(){ int n; double total; cin>>n; for(int i=0;i<n;i++){ double flag=0; if(i%2==0){ flag=1.0; }else{ flag=-1.0; } total+=(flag)/(i+1); } cout<<"total is"<<total...
数据结构第二次习题课
数据结构第二次习题课二叉树按二叉链表形式存储 建立完全二叉树的算法 12345678910111213141516171819typedef struct BTNode{ int data; struct BTNode *lchild,*rchild;}BTNode,*BiTree;//采用先序遍历构建二叉树BiTree CreateTree(){ int x; BiTree *bt; scanf("%d",&x); if(x==0) bt=NULL; else{ bt=(BiTree)malloc(sizeof(BTNode)); bt->data=x; bt->lchild=CreateTree(); bt->rchild=CreateTree(); } return bt;} 写一个判断给定的二叉树是否是完全二叉树的算法 1234567891011121...
数据结构第一次习题课
第一次习题课阶乘和123456789101112131415161718int cal(int x){ int cal=1; for(int i=2;i<=x;i++){ cal*=x; } return cal;}int fun(LinkList L){ LNode p=L->next; int ans=0; while(p){ ans+=cal(p->data); p=p->next; }} 数组在长度为N的数组arr中,将小于等于arr[0]的数放在数组的左半部分,大于arr[0]的放在右半部分, arr[0]介于中间,输出处理后的数组 123456789101112131415161718192021222324#include<iostream>using namespace std;const int N=100010;int arr[N];int main()...





