// 6_31: 已知,由单链表表示的线性表中,含有三类字符的数据元素(如:字母字符,数字字符和其他字符),
// 是编写算法构造三个以循环链表表示的线性表,使每个表中只含同一类的字符,
// 且利用原表中的节点空间作为这三个表的结点空间,头节点可另辟空间。
//
void Proceed(node* head, node** a, node** b, node** c)
{
//使用a,b,c作为传出指针
int i = 0;
node* list[3] = {*a, *b, *c};
node* temp = head;
if (temp == NULL)
{
printf("原列表为空!n");
return;
}
for (i = 0; i < 3; i++)
{
if (list[i] == NULL)
{
list[i] = (node*)malloc(sizeof(node));
if (list[i] == NULL)
{
printf("内存分配出错!n");
return;
}
}
list[i]->data = ”; //置头节点标记
list[i]->pNext = NULL;
}//为a、b、c赋初值
node* s[3];
for (i = 0; i < 3; i++)
{
s[i] = list[i];
}//三个链表的临时指针
while (temp != NULL)
{
i = 0;
if (temp->data >= ‘0’ && temp->data <= ‘9’)
{
i = 1;
}
else if (tolower(temp->data) >= ‘a’ && tolower(temp->data) <= ‘z’)
{
i = 2;
}
s[i]->pNext = temp;
s[i] = temp;
temp = temp->pNext;
}
for (i = 0; i < 3; i++)
{
s[i]->pNext = list[i];
}
*a = list[0];
*b = list[1];
*c = list[2];
return;
}
Posted by 陌陌 on 2006年03月30日 at 10:56
6-17完整程序#include "stdio.h"#define k 10struct node{ int num;};int main(){ struct node a[20]; struct node temp; int i,j; for(i=0;i<20;i++) a[i].num=0; for(i=0;i<10;i++) a[i].num=i+1; for(i=0;i<20;i++) printf("%d ",a[i].num); printf("\n"); for(j=0;j<k;j++){ i=9; do{ temp=a[i+j+1]; a[i+j+1]=a[i+j]; a[i+j]=temp; i–; }while(i!=-1); for(i=0;i<20;i++) printf("%d ",a[i].num); printf("\n"); } for(i=0;i<20;i++) printf("%d ",a[i].num); return 0;}
Posted by 陌陌 on 2006年03月30日 at 10:57
6-21数组法完整程序#include "stdio.h"#define N 10struct node{ int num;};int main(){ struct node a[N],temp; int i,j; for(i=0;i<N;i++) a[i].num=i+1; for(i=0;i<N;i++) printf("%d ",a[i].num); printf("\n"); for(i=0,j=N-1;i<=((N-1)/2),j>=((N+1)/2);i++,j–){
temp=a[i]; a[i]=a[j]; a[j]=temp; } for(i=0;i<N;i++) printf("%d ",a[i].num); return 0;
}
Posted by 陌陌 on 2006年03月30日 at 10:58
6-21链表法完整程序#include "stdio.h"#include "stdlib.h"#define N 10struct node{ int num; struct node * pNext;};int main(){ int i=0; struct node *head, *last; struct node * p1,* p2,* p3; head=(struct node *)malloc(sizeof(struct node)); head->pNext=NULL; head->num=i+1; i++; p1=head; while(i<N) { p2=(struct node *)malloc(sizeof(struct node)); p1->pNext=p2; p1=p1->pNext; p1->num=i+1; p1->pNext=NULL; i++; } p1=head; while(p1!=NULL) { printf("% d",p1->num); last=p1; p1=p1->pNext; } printf("\n"); p1=head; p2=p1->pNext; p3=p2->pNext; p1->pNext=NULL; while(1){ p2->pNext=p1; p1=p3->pNext; if(p1==NULL) {p3->pNext=p2; break;} p3->pNext=p2; p2=p1->pNext; if(p2==NULL) {p1->pNext=p3; break;} p1->pNext=p3; p3=p2->pNext; if(p3==NULL) {p2->pNext=p1; break;} } p1=last; while(p1!=NULL) { printf("% d",p1->num); p1=p1->pNext; } return 0;
}
Posted by 陌陌 on 2006年03月30日 at 10:59
6-27完整程序#include "stdio.h"#include "stdlib.h"#define N 10struct node{ int num; struct node * pNext;};int main(){ struct node * ha,* hb,*p1,*p2,*p3,*p4,*head; int i=0; p1=(struct node *)malloc(sizeof(struct node)); p2=(struct node *)malloc(sizeof(struct node)); ha=p1; hb=p2; p1->num=i+1; i++; p2->num=i+1; i++; while(i<N) { p3=(struct node *)malloc(sizeof(struct node)); p4=(struct node *)malloc(sizeof(struct node)); p1->pNext=p3; p3->num=i+1; i++; p1=p1->pNext; p1->pNext=NULL; p2->pNext=p4; p4->num=i+1; i++; p2=p2->pNext; p2->pNext=NULL; } p1=ha; p2=hb; head=NULL; while(p1->pNext!=NULL&&p2->pNext!=NULL) { if(head==NULL) if(p1->num>p2->num) head=p2; else head=p1; if(p1->num>p2->num){ p3=p2->pNext; p2->pNext=p1; p2=p3; } else if(p1->num<=p2->num){ p3=p1->pNext; p1->pNext=p2; p1=p3; } } p1=head; while(p1) { printf("% d",p1->num); p1=p1->pNext; } return 0;}
Posted by 陌陌 on 2006年03月30日 at 10:59
6-31完整程序#include "stdio.h"#include "conio.h"#include "stdlib.h"struct node{ char a; struct node * pNext;};int main(){ struct node * head=NULL,*p,*p1; p=(struct node *)malloc(sizeof(struct node)); head=p; head->pNext=NULL; p1=head; printf("Please input a string.(end with space)\n"); char b; b=getche(); head->a=b; while(b!=’ ‘){ b=getche(); p=(struct node *)malloc(sizeof(struct node)); p->a=b; p1->pNext=p; p1=p; p1->pNext=NULL; } struct node * h1,*h2,*h3,*p2,*p3,*p4,*p5; p=(struct node *)malloc(sizeof(struct node)); h1=p; p1=h1; h1->a=’ ‘; p=(struct node *)malloc(sizeof(struct node)); h2=p; p2=h2; h2->a=’ ‘; p=(struct node *)malloc(sizeof(struct node)); h3=p; p3=h3; h3->a=’ ‘; p4=head; while(p4){ if((p4->a>=65&&p4->a<=90)||(p4->a>=97&&p4->a<=122)){ p5=p4->pNext; p1->pNext=p4; p4->pNext=h1; p1=p4; p4=p5; } else if(p4->a>=48&&p4->a<=57){ p5=p4->pNext; p2->pNext=p4; p4->pNext=h2; p2=p4; p4=p5; } else{ p5=p4->pNext; p3->pNext=p4; p4->pNext =h3; p3=p4; p4=p5; } } printf("devise as:\n"); p1=h1; do{ p1=p1->pNext; printf("%c",p1->a); }while(p1->a!=’ ‘); printf("\n"); p1=h2; do{ p1=p1->pNext; printf("%c",p1->a); }while(p1->a!=’ ‘); printf("\n"); p1=h3; do{ p1=p1->pNext; printf("%c",p1->a); }while(p1->a!=’ ‘); printf("\n"); return 0;}
Posted by my-space on 2006年04月1日 at 14:47
#include<iostream.h>#include<stdio.h>#include<string.h>#include<iomanip.h>struct note2{ char ch[10]; note2 *next;};
void main() { note2 *head=NULL; //head为头指针 note2 *end=NULL; int last=0; cout<<"输入字符串如e1,e2,….e9并以over作为终止符"<<endl; char num[10]; note2 *ip,*ir,*pnext,*phead,*iq; /*在连表反转时:ir用于遍历节点 pnext指向ir指向节点的直接前驱 phead指向直接后驱*/ cout<<"输入ch的值: "<<endl; gets(num); /* 建立单链表"over"作为输入终止符*/ while(strcmp(num,"over")!=0) { ip=new note2; strcpy(ip->ch,num);
if(head==NULL) head=ip; else end->next=ip; end=ip; last++; cout<<"输入ch的值:"<<endl; gets(num); } if(end!=NULL) end->next=NULL; if(head==NULL) { cout<<"链表为空"<<endl; return; } if(last<=2) //单链表反转 { iq=end; end->next=head; end=head; head=iq; end->next=NULL; } else { phead=head; ir=phead->next; pnext=ir->next; while(pnext->next!=NULL) { ir->next=phead; phead=ir; ir=pnext; pnext=pnext->next; } ir->next=phead; pnext->next=ir; end=head; head=pnext; end->next=NULL; } cout<<"输出结果:"<<endl; //链表输出 ir=head; while(ir->next!=NULL) { cout<<setw(4)<<ir->ch; ir=ir->next; } cout<<setw(4)<<end->ch<<endl; cout<<endl;}
Posted by my-space on 2006年04月1日 at 14:53
/*作业6.21顺序表完整程序*/#include<iostream.h>#include<stdio.h>#include<string.h>#include<iomanip.h>const int m=9;struct note{ char ch[10];};note array[m];void main(){ cout<<"输入9个字符串如e1,e2,….e9"<<endl; for(int i=0;i<m;i++) //建立顺序表 { cout<<"输入第"<<i+1<<"个字符串"<<endl; gets(array[i].ch);} char nam[3]; if(m%2==0) //如果m为偶数时作反转 { for(int j=0;j<m/2;j++) { strcpy(nam,array[j].ch); strcpy(array[j].ch,array[m-j-1].ch); strcpy(array[m-j-1].ch,nam); } } else //如果m为奇数时作反转 { for(int k=0;k<(m-1)/2;k++) { strcpy(nam,array[k].ch); strcpy(array[k].ch,array[m-k-1].ch); strcpy(array[m-k-1].ch,nam); } } cout<<"输出结果:"<<endl; //输出最终结果 for(int x=0;x<m;x++) cout<<setw(4)<<array[x].ch; cout<<endl;}
Posted by my-space on 2006年04月4日 at 13:50
//作业7-18
#include<iostream.h>#include<stdio.h>#include<iomanip.h>#include<string.h>const int maxsize=100;char *NEW();void DELETE(char*head,int i,int j);char array[maxsize];void main(){ int i,j; char *head; cout<<"输入i和j的值"<<endl; cin>>i>>j; head=NEW(); cout<<"输出删除前的串"<<endl; cout<<head<<endl; DELETE(head,i,j); cout<<"输出删除后的串"<<endl; cout<<head<<endl;}//建表函数定义char *NEW(){ char *pi; cout<<"输入串"<<endl; pi=gets(array); return pi;}//删除函数定义void DELETE(char *head,int i,int j){ for(int k=i;head[k+j-1]!=”;k++) head[k-1]=head[k+j-1]; head[k-1]=”;}
Posted by my-space on 2006年04月4日 at 13:53
//作业7-20
#include<iostream.h>#include<conio.h>typedef struct node{ char ch; node *next;}node;node *NEW();void RESULT(node *head);//mian函数定义void main(){ node *head1,*pi; head1=NEW(); cout<<"输出未替代前的链表"<<endl; pi=head1->next; while(pi!=NULL) { cout<<pi->ch; pi=pi->next; } cout<<endl; RESULT(head1); cout<<"输出替代后的链表"<<endl; pi=head1->next; while(pi!=NULL) { cout<<pi->ch; pi=pi->next; } cout<<endl; return;
}//下面建表node *NEW(){ node *head,*end,*ip; char num; head=new node; //建立头节点并置空 end=head; cout<<"输入字符值以回车为结束符"<<endl; num=getche(); cout<<endl; while(num!=’\r’) { ip=new node; ip->ch=num; end->next=ip; end=ip; cout<<"输入字符值以回车为结束符"<<endl; num=getche(); cout<<endl; } end->next=NULL; return head;}//下面替代处理void RESULT(node *head){ node *ir; ir=head->next; while(ir!=NULL) { if(ir->ch==’c’) ir->ch=’s’; ir=ir->next; } }