队列_顺序队列
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 50
#define ElemType int
#define Status int
typedef struct
{
ElemType data[MaxSize];
int front,rear;
}Queueptr;
Status InitQueue(Queueptr &Q)
{
Q.front=Q.rear=0;
}
Status QueueEmpty(Queueptr Q)
{
if(Q.front==Q.rear)
{
return NULL;
}
}
Status QueueLength(Queueptr Q)
{
return Q.rear-Q.front;
}
Status GetHead(Queueptr Q,ElemType &e)
{
e=Q.data[Q.front];
return e;
}
Status EnQueue(Queueptr &Q,ElemType e)
{
printf("Queue-->%d\n",e);
if(Q.rear>=MaxSize)
{
return -1;
}
Q.data[Q.rear++]=e;
return 0;
}
Status DeQueue(Queueptr &Q,ElemType &e)
{
if(Q.rear==Q.front)
{
return -1;
}
e=Q.data[Q.front++];
printf("Queue<--%d\n",e);
return e;
}
Status ClearQueue(Queueptr &Q)
{
}
Status DestoryQueue(Queueptr &Q)
{
}
int main()
{
Queueptr Q;
int e;
InitQueue(Q);
for(int i=1;i<10;i++)
{
EnQueue(Q,i);
}
printf("Queue_length->%d\n",QueueLength(Q));
printf("Queue_header->%d\n",GetHead(Q,e) );
while(QueueEmpty(Q)!=NULL)
{
DeQueue(Q,e);
}
return 0;
}
队列_顺序循环队列
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 50
#define ElemType int
#define Status int
typedef struct
{
ElemType data[MaxSize];
int front,rear;
}Queueptr;
Status InitQueue(Queueptr &Q)
{
Q.front=Q.rear=0;
}
Status QueueEmpty(Queueptr Q)
{
if(Q.front==Q.rear)
{
return NULL;
}
}
Status QueueLength(Queueptr Q)
{
return (Q.rear-Q.front+MaxSize)%MaxSize;
}
Status GetHead(Queueptr Q,ElemType &e)
{
e=Q.data[Q.front];
return e;
}
Status EnQueue(Queueptr &Q,ElemType e)
{
if(Q.front==(Q.rear+1)%MaxSize)
{
return -1;
}
Q.data[Q.rear]=e;
printf("Queue-->%d\n",e);
Q.rear=(Q.rear+1)%MaxSize;
return 0;
}
Status DeQueue(Queueptr &Q,ElemType &e)
{
if(Q.rear==Q.front)
{
return -1;
}
e=Q.data[Q.front];
Q.front=(Q.front+1)%MaxSize;
printf("Queue<--%d\n",e);
return e;
}
Status ClearQueue(Queueptr &Q)
{
}
Status DestoryQueue(Queueptr &Q)
{
}
int main()
{
Queueptr Q;
int e;
InitQueue(Q);
for(int i=1;i<30;i++)
{
EnQueue(Q,i);
}
printf("Queue_length->%d\n",QueueLength(Q));
printf("Queue_header->%d\n",GetHead(Q,e) );
while(QueueEmpty(Q)!=NULL)
{
DeQueue(Q,e);
}
return 0;
}
队列_链队列
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 50
#define ElemType int
#define Status int
typedef struct QNode
{
ElemType data;
struct QNode *next;
}QNode,*Queueptr;
typedef struct
{
Queueptr front;
Queueptr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(QNode*)malloc(sizeof(QNode));
if(!Q.front)
{
exit(-1);
}
Q.front->next=NULL;
return 0;
}
Status QueueEmpty(LinkQueue Q)
{
if(Q.front==Q.rear)
{
return NULL;
}
}
Status QueueLength(LinkQueue Q)
{
int i;
while(Q.front->next!=NULL)
{
i++;
Q.front=Q.front->next;
}
return i;
}
Status GetHead(LinkQueue Q,ElemType &e)
{
e=Q.front->next->data;
return e;
}
Status EnQueue(LinkQueue &Q,ElemType e)
{
Queueptr p;
p=(QNode*)malloc(sizeof(QNode));
p->data=e;
printf("----->%d\n",e);
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
}
Status DeQueue(LinkQueue &Q,ElemType &e)
{
if(Q.front==Q.rear)
{
return -1;
}
Queueptr p;
p=Q.front->next;
e=p->data;
printf("<-----%d\n",e);
Q.front->next=p->next;
if(Q.rear==p)
{
Q.rear=Q.front;
}
free(p);
}
Status ClearQueue(LinkQueue &Q)
{
if (Q.front == Q.rear)
{
return -1;
}
QNode *node = Q.front->next;
while (node)
{
Q.front->next = node->next;
if (Q.rear == node)
{
Q.rear = Q.front;
}
free(node);
node = Q.front->next;
}
return 0;
}
Status DestoryQueue(LinkQueue &Q)
{
}
int main()
{
LinkQueue Q;
int e;
InitQueue(Q) ;
for(int i=0;i<10;i++)
{
EnQueue(Q,i) ;
}
printf("Queue_head->%d\n",GetHead(Q,e) );
printf("Queue_len->%d\n",QueueLength(Q) );
while(QueueEmpty(Q)!=NULL)
{
DeQueue(Q,e);
}
return 0;
}
队列_循环队列
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 50
#define ElemType int
#define Status int
typedef struct
{
ElemType *base;
int front,rear;
}Queueptr;
Status InitQueue(Queueptr &Q)
{
Q.base=(ElemType*)malloc(MaxSize*sizeof(ElemType));
if(!Q.base)
{
exit(-1);
}
Q.front=Q.rear=0;
}
Status QueueEmpty(Queueptr Q)
{
if(Q.front==Q.rear)
{
return NULL;
}
}
Status QueueLength(Queueptr Q)
{
return (Q.rear-Q.front+MaxSize)%MaxSize;
}
Status GetHead(Queueptr Q,ElemType &e)
{
e=Q.base[Q.front];
return e;
}
Status EnQueue(Queueptr &Q,ElemType e)
{
if((Q.rear+1)%MaxSize==Q.front)
{
return -1;
}
Q.base[Q.rear]=e;
printf("Queue-->%d\n",Q.base[Q.rear]);
Q.rear=(Q.rear+1)%MaxSize;
return 0;
}
Status DeQueue(Queueptr &Q,ElemType &e)
{
if(Q.rear==Q.front)
{
return -1;
}
e=Q.base[Q.front];
Q.front=(Q.front+1)%MaxSize;
printf("Queue<--%d\n",e);
return e;
}
Status ClearQueue(Queueptr &Q)
{
}
Status DestoryQueue(Queueptr &Q)
{
}
int main()
{
Queueptr Q;
int e;
InitQueue(Q) ;
for(int i=1;i<20;i++)
{
EnQueue(Q,i);
}
printf("Queue_length->%d\n",QueueLength(Q));
printf("Queue_header->%d\n",GetHead(Q,e) );
while(QueueEmpty(Q)!=NULL)
{
DeQueue(Q,e);
}
return 0;
}
线性表应用_集合操作1
#include<stdio.h>
#include<stdlib.h>
#define typeElem int
typedef struct Node
{
struct Node *next;
typeElem data;
}SetNode,*LinkSet;
void InitList(LinkSet &s)
{
s=(SetNode*)malloc(sizeof(SetNode));
s->next=NULL;
}
void InsertElem(LinkSet &s,typeElem e)
{
SetNode *p,*t=s,*tail;
p=(SetNode*)malloc(sizeof(SetNode));
p->data=e;
p->next=NULL;
while(t->next!=NULL)
{
if(t->next->data==e)
{
return;
}
t=t->next;
}
t->next=p;
}
void PrintSet(LinkSet s)
{
while(s->next!=NULL)
{
printf("%d \n",s->next->data);
s->next=s->next->next;
}
}
void MergeSet(LinkSet la,LinkSet lb,LinkSet &lc)
{
while(la->next!=NULL)
{
InsertElem(lc,la->next->data);
la->next=la->next->next;
}
while(lb->next!=NULL)
{
InsertElem(lc,lb->next->data);
lb->next=lb->next->next;
}
}
int main()
{
LinkSet la,lb,lc;
InitList(la);
InitList(lb);
InitList(lc);
InsertElem(la,3);
InsertElem(la,2);
InsertElem(la,4);
InsertElem(la,7);
InsertElem(la,2);
InsertElem(lb,3);
InsertElem(lb,2);
InsertElem(lb,1);
InsertElem(lb,6);
InsertElem(lb,8);
printf("\n");
MergeSet(la,lb,lc);
printf("****************\n");
PrintSet(lc);
printf("****************\n");
}
线性表应用_集合操作2
#include<stdio.h>
#include<stdlib.h>
#define typeElem int
typedef struct Node
{
struct Node *next;
typeElem data;
}SetNode;
typedef struct{
SetNode *first;
}LinkSet;
void InitList(LinkSet &s)
{
s.first=NULL;
}
int numSet(LinkSet s,typeElem e)
{
while(s.first!=NULL)
{
if(s.first->data==e)
{
return 0;
}
s.first=s.first->next;
}
return 1;
}
void InsertElem(LinkSet &s,typeElem e)
{
if(numSet(s,e))
{
SetNode *p,*tail;
LinkSet t;
p=(SetNode*)malloc(sizeof(SetNode));
p->data=e;
p->next=NULL;
if(s.first==NULL)
{
s.first=p;
}
else
{
tail->next=p;
}
tail=p;
}
}
void PrintSet(LinkSet s)
{
while(s.first!=NULL)
{
printf("%d \n",s.first->data);
s.first=s.first->next;
}
}
void MergeSet(LinkSet la,LinkSet lb,LinkSet &lc)
{
while(la.first!=NULL)
{
InsertElem(lc,la.first->data);
la.first=la.first->next;
}
while(lb.first!=NULL)
{
InsertElem(lc,lb.first->data);
lb.first=lb.first->next;
}
}
int main()
{
LinkSet la,lb,lc;
InitList(la);
InitList(lb);
InitList(lc);
InsertElem(la,3);
InsertElem(la,2);
InsertElem(la,4);
InsertElem(la,7);
InsertElem(la,5);
InsertElem(lb,3);
InsertElem(lb,2);
InsertElem(lb,1);
InsertElem(lb,6);
InsertElem(lb,8);
MergeSet(la,lb,lc);
printf("\n");
PrintSet(lc);
}
线性表应用_一元多项式
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define LEN sizeof(Poly)
typedef struct term{
float coef;
int expn;
struct term *next;
}Poly,*Link;
int LocateElem(Link p, Link s, Link &q);
void CreatePolyn(Link &p,int m);
void PrintPolyn(Link p);
int cmp(Link a, Link b);
Link AddPolyn(Link pa, Link pb);
Link SubPolyn(Link pa, Link pb);
Link Reverse(Link p);
Link MultiplyPolyn(Link A,Link B);
void Calculate(Link p,float x);
int main()
{
Link P1,P2,P3;
int L1,L2;
printf(" --------------------------------------------------------------------\n");
printf(" |================== 一元多项式的运算 =================|\n");
printf(" |================== 1.相加(+) =================|\n");
printf(" |================== 2.相减(-) =================|\n");
printf(" |================== 3.相乘(*) =================|\n");
printf(" --------------------------------------------------------------------\n\n");
printf("请输入第一个多项式的项数:");
scanf("%d",&L1);
CreatePolyn(P1,L1);
printf("第一个多项式为:");
printf("P1(X)=");
PrintPolyn(P1);
printf("请输入第二个多项式的项数:");
scanf("%d",&L2);
CreatePolyn(P2,L2);
printf("第二个多项式为:");
printf("P2(X)=");
PrintPolyn(P2);
printf("\n");
printf("请输入要选择的运算(+ , - , *): ");
char ch1;
getchar();
scanf("%c",&ch1);
getchar();
switch(ch1)
{
case '+':
{
printf("两个一元多项式相加: ");
printf("P1(X)+P2(X)=");
P3=AddPolyn(P1, P2);
PrintPolyn(P3);
}
break;
case '-':
{
printf("两个一元多项式相减: ");
printf("P1(X)-P2(X)=");
P3=SubPolyn(P1, P2);
PrintPolyn(P3);
}
break;
case '*':
{
printf("两个一元多项式相乘: ");
printf("P1(X)*P2(X)=");
P3=MultiplyPolyn(P1, P2);
PrintPolyn(P3);
}
break;
default:
{
printf("您输入了错误指令 %c !",ch1);
}
}
char ch2;
printf("\n是否代入X进行求值?(Y/N): ");
ch2=getchar();
getchar();
switch(ch2)
{
case 'Y':
{
float x;
printf("\n请输入多项式中X的值:");
scanf("%f",&x);
Calculate(P3,x);
break;
}
case 'N':
{
break;
}
default:
{
printf("你输入了错误指令 %c !",ch2);
}
}
return 0;
}
int LocateElem(Link p, Link s, Link &q)
{
Link p1 = p->next;
Link p2 = p;
while(p1)
{
if(s->expn > p1->expn)
{
p1 = p1->next;
p2 = p2->next;
}
else if(s->expn == p1->expn)
{
q = p1;
return 1;
}
else
{
q = p2;
return 0;
}
}
if(!p1)
{
q = p2;
return 0;
}
}
void CreatePolyn(Link &p,int m)
{
Link s,q;
int i;
p=(Link)malloc(LEN);
p->next=NULL;
for(i=0;i<m;i++)
{
s=(Link)malloc(LEN);
printf("输入系数和指数(以空格隔开):");
scanf("%f %d", &s->coef, &s->expn);
if(!LocateElem(p, s, q))
{
s->next = q->next;
q->next = s;
}
else
{
q->coef+=s->coef;
}
}
}
void PrintPolyn(Link p)
{
Link s;
s = p->next;
while(s)
{
printf(" %.2f X^%d", s->coef, s->expn);
s = s->next;
if(s!=NULL)
{
if(s->coef>=0)
{
printf(" +");
}
}
}
printf("\n");
}
int cmp(Link a, Link b)
{
if (a->expn<b->expn)
{
return -1;
}
else if(a->expn == b->expn)
{
return 0;
}
else
{
return 1;
}
}
Link AddPolyn(Link pa, Link pb)
{
Link newp, p, q, s, pc;
float sum;
p = pa->next;
q = pb->next;
newp=(Link)malloc(LEN);
pc = newp;
while(p&&q)
{
switch(cmp(p, q))
{
case -1://
s = (Link)malloc(LEN);
s->coef = p->coef;
s->expn = p->expn;
pc->next = s;
pc = s;
p = p->next;
break;
case 0://若比较两项的指数相等,则将两项系数相加后得到的项放入头结点为newp的链表中 ,且p,q同时向后遍历
sum = p->coef+q->coef;
if(sum!=0.0)
{
s = (Link)malloc(LEN);
s->coef = sum;
s->expn = p->expn;
pc->next = s;
pc = s;
}
p = p->next;
q = q->next;
break;
case 1://若指数:q<p,则将q所指结点链入头结点为newp的链表中,且q向后遍历
s = (Link)malloc(LEN);
s->coef = q->coef;
s->expn = q->expn;
pc->next = s;
pc = s;
q = q->next;
break;
}
}
pc->next=p?p:q;
return newp;
}
Link SubPolyn(Link pa, Link pb)
{
Link newp, p, q, s, pc;
float sum;
newp=(Link)malloc(LEN);
pc = newp;
p = pa->next;
q = pb->next;
while(q)
{
q->coef=0-q->coef;
q=q->next;
}
q=pb->next;
while(p&&q)
{
switch(cmp(p, q))
{
case -1:
s = (Link)malloc(LEN);
s->coef = p->coef;
s->expn = p->expn;
pc->next = s;
pc = s;
p = p->next;
break;
case 0:
sum = p->coef-q->coef;
if(sum!=0.0)
{
s = (Link)malloc(LEN);
s->coef = sum;
s->expn = p->expn;
pc->next = s;
pc = s;
}
p = p->next;
q = q->next;
break;
case 1:
s = (Link)malloc(LEN);
s->coef = q->coef;
s->expn = q->expn;
pc->next = s;
pc = s;
q = q->next;
break;
}
}
pc->next=p?p:q;
return newp;
}
Link Reverse(Link p)
{
Link head=p;
Link q1,q2;
q2=head->next;
head->next=NULL;
while(q2)
{
q1=q2;
q2=q2->next;
q1->next=head->next;
head->next=q1;
}
return head;
}
Link MultiplyPolyn(Link A,Link B)
{
Link pa,pb,pc,s,head;
int k,maxExpn,minExpn;
float coef;
head=(Link)malloc(LEN);
head->next=NULL;
if(A->next!=NULL&&B->next!=NULL)
{
minExpn=A->next->expn+B->next->expn;
A=Reverse(A);
B=Reverse(B);
maxExpn=A->next->expn+B->next->expn;
}
else
{
return head;
}
pc=head;
B=Reverse(B);
for(k = maxExpn;k>=minExpn;k--)
{
pa = A->next;
while(pa !=NULL&&pa->expn>k)
{
pa = pa->next;
}
pb = B->next;
while(pb!=NULL&&pa!=NULL&&pa->expn+pb->expn<k)
{
pb = pb->next;
}
coef=0.0;
while(pa!=NULL&&pb!=NULL)
{
if(pa->expn+pb->expn==k)
{
coef+=pa->coef*pb->coef;
pa=pa->next;
pb=pb->next;
}
else if(pa->expn+pb->expn>k)
{
pa = pa->next;
}
else
{
pb = pb->next;
}
}
if(coef!=0.0)
{
s=(Link)malloc(LEN);
s->coef=coef;
s->expn=k;
s->next=pc->next;
pc->next=s;
pc=s;
}
}
B = Reverse(B);
head=Reverse(head);
return head;
}
void Calculate(Link p,float x)
{
Link q=p->next;
float sum;
float result=0;
while(q)
{
sum=1.0;
for(int i=1;i<=q->expn;i++)
{
sum=sum*x;
}
result+=sum*q->coef;
q=q->next;
}
printf("将X的值代入多项式中计算的结果为:%.5f\n",result);
}
线性表_顺序表
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
#define Max 100
typedef struct
{
int *elem;
int length;
}sqlist;
int init_list(sqlist &l)
{
l.elem=(int *)malloc(Max*sizeof(int));
l.length=0;
return 0;
}
int create_list(sqlist &l,int i,int e)
{
int *newbase;
if(i<1||i>l.length+1)
{
return -1;
}
if(l.length>=Max-1)
{
newbase=(int *)realloc(l.elem,Max*sizeof(int));
if(!newbase)
{
return -1;
}
l.elem=newbase;
}
for(int j=l.length-1;j>=i-1;j--)
{
l.elem[j+1]=l.elem[j];
}
l.elem[i-1]=e;
l.length++;
return 0;
}
int print_list(sqlist &l)
{
for(int i=1;i<l.length+1;i++)
{
cout<<l.elem[i-1]<<endl;
}
return 0;
}
int find_list(sqlist &l)
{
int c,t=0;
printf("input selct a num:\n");
cin>>c;
for(int i=1;i<l.length+1;i++)
{
if(c==l.elem[i-1])
{
cout<<l.elem[i-1]<<endl;
t=1;
break;
}
}
if(c==0)
{
printf("connot fint it\n");
}
return 0;
}
int del_list(sqlist &l)
{
int c,t=0;
printf("input selct a num:\n");
cin>>c;
for(int i=1;i<l.length+1;i++)
{
if(c==l.elem[i-1])
{
l.elem[i-1]=l.elem[i];
t=1;
}
if(t==1)
{
l.elem[i-1]=l.elem[i];
}
}
l.length--;
if(c==0)
{
printf("connot find it to del\n");
}
return 0;
}
int mermg(sqlist &l,sqlist &l1,sqlist &m)
{
if (l.length==0|| l1.length==0)
{
return -1;
}
init_list(m);
for(int i=1;i<l.length+1;i++)
{
create_list(m,i,l.elem[i-1]);
}
for(int j=l.length+1;j<l.length+l1.length+1;j++)
{
create_list(m,j,l1.elem[j-l.length-1]);
}
return 0;
}
int main()
{
sqlist p1,p2,p3;
init_list(p1);
init_list(p2);
init_list(p3);
for(int i=1;i<10;i++)
{
create_list(p1,i,i+1);
}
for(int j=1;j<4;j++)
{
create_list(p2,j,j+2);
}
create_list(p1,3,1);
print_list(p1);
printf("*****************\n");
print_list(p2);
printf("*****************\n");
mermg(p1,p2,p3);
print_list(p3);
return 0;
}
线性表_顺序表(数组)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
char name[30];
char id[30];
float score;
struct student *next;
}*Stu,stu;
Stu creatList()
{
int n,i;
stu *p,*head,*tail;
printf("请输入学生人数:\n");
scanf("%d",&n);
head=NULL;
for(i=0;i<n;i++)
{
printf("第%d个学生:\n",i+1);
p=(stu *)malloc(sizeof(stu));
printf("请输入学生的姓名,学号,分数:\n");
scanf("%s %s %f",&p->name,&p->id,&p->score);
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
tail->next=p;
}
tail=p;
}
printf("创建成功!\n");
return head;
}
Stu printList(stu *p )
{
if(p==NULL)
{
printf("链表为空!\n");
}
while(p!=NULL)
{
printf("学生的姓名,学号,分数为:\n");
printf("%s %s %.2f\n",p->name,p->id,p->score);
p=p->next;
}
}
stu select_data(stu *head)
{
char n[40];
int m;
if(head==NULL)
{
printf("data no exists:\n");
}
printf("intput student name:\n");
scanf("%s",n);
while(head!=NULL)
{
m=strcmp(n,head->name);
if(m==0)
{
printf("%s,%s,%.2f \n",head->name,head->id,head->score);
}
head=head->next;
}
}
int main()
{
stu *p;
p=creatList();
printList(p);
select_data(p);
return 0;
}
线性表_链表
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
char name[30];
char id[30];
float score;
int len;
struct student *next;
}*Stu,stu;
void creatList(stu *&head)
{
int n,i;
stu *p,*tail;
printf("请输入学生人数:\n");
scanf("%d",&n);
head=NULL;
for(i=0;i<n;i++)
{
printf("第%d个学生:\n",i+1);
p=(stu *)malloc(sizeof(stu));
printf("请输入学生的姓名,学号,分数:\n");
scanf("%s %s %f",&p->name,&p->id,&p->score);
p->next=NULL;
if(head==NULL)
{
head=p;
head->len=1;
}
else
{
tail->next=p;
head->len=head->len+1;
}
tail=p;
}
tail->next=head;
printf("创建成功!\n");
}
void printList(stu *p )
{
if(p==NULL)
{
printf("链表为空!\n");
}
int i=0;
printf("len-->%d\n",p->len);
while(p!=NULL)
{
printf("学生的姓名,学号,分数为:\n");
printf("%s %s %.2f\n",p->name,p->id,p->score);
p=p->next;
i++;
if(i==p->len)
{
break;
}
}
}
void select_data(stu *head)
{
char n[40];
int m;
if(head==NULL)
{
printf("data no exists:\n");
}
printf("intput student name:\n");
scanf("%s",n);
while(head!=NULL)
{
m=strcmp(n,head->name);
if(m==0)
{
printf("%s,%s,%.2f \n",head->name,head->id,head->score);
break;
}
head=head->next;
}
}
int main()
{
Stu head;
creatList(head);
printList(head);
select_data(head);
return 0;
}
线性表_循环链表
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
char name[30];
char id[30];
float score;
int len;
struct student *next;
}*Stu,stu;
void creatList(stu *&head)
{
int n,i;
stu *p,*tail;
printf("请输入学生人数:\n");
scanf("%d",&n);
head=NULL;
for(i=0;i<n;i++)
{
printf("第%d个学生:\n",i+1);
p=(stu *)malloc(sizeof(stu));
printf("请输入学生的姓名,学号,分数:\n");
scanf("%s %s %f",&p->name,&p->id,&p->score);
p->next=NULL;
if(head==NULL)
{
head=p;
head->len=1;
}
else
{
tail->next=p;
head->len=head->len+1;
}
tail=p;
}
tail->next=head;
printf("创建成功!\n");
}
void printList(stu *p )
{
if(p==NULL)
{
printf("链表为空!\n");
}
int i=0;
printf("len-->%d\n",p->len);
while(p!=NULL)
{
printf("学生的姓名,学号,分数为:\n");
printf("%s %s %.2f\n",p->name,p->id,p->score);
p=p->next;
i++;
if(i==p->len)
{
break;
}
}
}
void select_data(stu *head)
{
char n[40];
int m;
if(head==NULL)
{
printf("data no exists:\n");
}
printf("intput student name:\n");
scanf("%s",n);
while(head!=NULL)
{
m=strcmp(n,head->name);
if(m==0)
{
printf("%s,%s,%.2f \n",head->name,head->id,head->score);
break;
}
head=head->next;
}
}
int main()
{
Stu head;
creatList(head);
printList(head);
select_data(head);
return 0;
}
线性表_双向链表
#include<stdio.h>
#include<stdlib.h>
typedef struct datanum
{
int num;
struct datanum *right;
struct datanum *left;
}dat,*dap;
struct datanum* create()
{
int i,j,n;
dat *head,*s,*p;
printf("请输入次数:\n");
scanf("%d",&n);
head=(datanum*)malloc(sizeof(datanum));
p=head;
head->right=NULL;
head->left=NULL;
for(i=n;i>0;i--)
{
printf("请输入第%d个数字:\n",n-i+1);
s=(datanum*)malloc(sizeof(datanum));
scanf("%d",&s->num);
p->right=s;
s->left=p;
p=s;
}
s->right=NULL;
printf("链表创建完成!\n");
return head;
}
int print1(struct datanum *p)
{
printf("链表信息显示:\n");
while(p->right!=NULL)
{
printf("%d<--->",p->right->num);
p=p->right;
}
printf("over!\n");
}
struct datanum* w_insert(struct datanum *p)
{
int n;
dat *t,*head;
printf("请输入插入的数字:\n");
t=(datanum*)malloc(sizeof(datanum));
scanf("%d",&t->num);
head=p;
while(p->right!=NULL)
{
p=p->right;
}
p->right=t;
t->right=NULL;
t->left=p;
return head;
}
struct datanum* t_insert(struct datanum *p)
{
int n;
dat *t,*head;
printf("请输入插入的数字:\n");
t=(datanum*)malloc(sizeof(datanum));
scanf("%d",&t->num);
head=p;
t->right=p->right;
p->right->left=t;
p->right=t;
t->left=p;
return head;
}
int main()
{
dat *p,*q;
p=create();
print1(p);
q=w_insert(p);
print1(q);
q=t_insert(p);
print1(q);
return 0;
}
线性表_单链表
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
char name[30];
char id[30];
float score;
struct student *next;
}*Stu,stu;
Stu creatList()
{
int n,i;
stu *p,*head,*tail;
printf("请输入学生人数:\n");
scanf("%d",&n);
head=NULL;
for(i=0;i<n;i++)
{
printf("第%d个学生:\n",i+1);
p=(stu *)malloc(sizeof(stu));
printf("请输入学生的姓名,学号,分数:\n");
scanf("%s %s %f",&p->name,&p->id,&p->score);
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
tail->next=p;
}
tail=p;
}
printf("创建成功!\n");
return head;
}
Stu printList(stu *p )
{
if(p==NULL)
{
printf("链表为空!\n");
}
while(p!=NULL)
{
printf("学生的姓名,学号,分数为:\n");
printf("%s %s %.2f\n",p->name,p->id,p->score);
p=p->next;
}
}
stu select_data(stu *head)
{
char n[40];
int m;
if(head==NULL)
{
printf("data no exists:\n");
}
printf("intput student name:\n");
scanf("%s",n);
while(head!=NULL)
{
m=strcmp(n,head->name);
if(m==0)
{
printf("%s,%s,%.2f \n",head->name,head->id,head->score);
}
head=head->next;
}
}
int main()
{
stu *p;
p=creatList();
printList(p);
select_data(p);
return 0;
}