数据结构实验3(最近就写这玩意了)
2008-11-01 18:23
302 查看
输入任意多个,任意长度的单词,按字母顺序排序输出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define BASE_SIZE 5
int x=BASE_SIZE;
//本程序的功能是,输入nCount个单词,知道输入了#为止,然后按照他们的名字的字母顺序排序
struct Node
{
char *ch;
Node *pNext;
};
Node *create()
{
int yy;
char * word();
void print(char *name[],int n);
void sort(char *n[],int k);
char **p;//二级指针
char *str;
void sort(char *n[]);
Node *pHead = NULL;
Node *pPrevious = NULL;
Node *pNew = NULL;
int nCount = 0;
do
{
pNew = (Node *)malloc(sizeof(Node));
printf("请输入单词:");
pNew->ch=word();
printf("/n");
pNew->pNext = NULL;
nCount++;
if (NULL == pHead)
{
pHead = pNew;
}
if (NULL == pPrevious)
{
pPrevious = pNew;
}
pPrevious->pNext = pNew;
pPrevious = pNew;
}while('#'!=*(pNew->ch));
nCount--;
printf("你输入了%d个单词/n",nCount);
printf("它们是:/n");
p=new char*[nCount];//动态创立指针数组
pPrevious=pHead;
for(int i=0;i<nCount;i++)//把链表中的字符串放到指针数组里
{
p[i]=pPrevious->ch;
pPrevious=pPrevious->pNext;
}
sort(p,nCount); //排序
print(p,nCount);//输出排序后的单词
system("pause");
return pHead;
}
void sort(char *name[],int n)//排序字母的函数
{
char *temp;
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(strcmp(name[k],name[j])>0)k=j;
if(k!=i)
{temp=name[i];name[i]=name[k];name[k]=temp;}
}
}
void print(char *name[],int n)//输出的函数
{
int i;
for(i=0;i<n;i++)printf("%s/n",name[i]);
}
char * word()
{
char *str;
int n=0;
char c;
str=new char[BASE_SIZE];
while(c!=13)
{
c=getche();
str
=c;
n++;
if(n>x-2)
{
str=(char *)realloc(str,x+BASE_SIZE);
x+=BASE_SIZE;
}
}
str
='/0';
return str;
}
void main()
{
printf("请输入您要排序的单词,直到输入'#'结束/n");
create();
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define BASE_SIZE 5
int x=BASE_SIZE;
//本程序的功能是,输入nCount个单词,知道输入了#为止,然后按照他们的名字的字母顺序排序
struct Node
{
char *ch;
Node *pNext;
};
Node *create()
{
int yy;
char * word();
void print(char *name[],int n);
void sort(char *n[],int k);
char **p;//二级指针
char *str;
void sort(char *n[]);
Node *pHead = NULL;
Node *pPrevious = NULL;
Node *pNew = NULL;
int nCount = 0;
do
{
pNew = (Node *)malloc(sizeof(Node));
printf("请输入单词:");
pNew->ch=word();
printf("/n");
pNew->pNext = NULL;
nCount++;
if (NULL == pHead)
{
pHead = pNew;
}
if (NULL == pPrevious)
{
pPrevious = pNew;
}
pPrevious->pNext = pNew;
pPrevious = pNew;
}while('#'!=*(pNew->ch));
nCount--;
printf("你输入了%d个单词/n",nCount);
printf("它们是:/n");
p=new char*[nCount];//动态创立指针数组
pPrevious=pHead;
for(int i=0;i<nCount;i++)//把链表中的字符串放到指针数组里
{
p[i]=pPrevious->ch;
pPrevious=pPrevious->pNext;
}
sort(p,nCount); //排序
print(p,nCount);//输出排序后的单词
system("pause");
return pHead;
}
void sort(char *name[],int n)//排序字母的函数
{
char *temp;
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(strcmp(name[k],name[j])>0)k=j;
if(k!=i)
{temp=name[i];name[i]=name[k];name[k]=temp;}
}
}
void print(char *name[],int n)//输出的函数
{
int i;
for(i=0;i<n;i++)printf("%s/n",name[i]);
}
char * word()
{
char *str;
int n=0;
char c;
str=new char[BASE_SIZE];
while(c!=13)
{
c=getche();
str
=c;
n++;
if(n>x-2)
{
str=(char *)realloc(str,x+BASE_SIZE);
x+=BASE_SIZE;
}
}
str
='/0';
return str;
}
void main()
{
printf("请输入您要排序的单词,直到输入'#'结束/n");
create();
}
相关文章推荐
- 数据结构实验2(最近就写这玩意了)
- 数据结构实验之排序一:一趟快排
- 数据结构实验之查找五:平方之哈希表
- 数据结构实验之队列一:排队买饭
- 数据结构实验之查找三:树的种类统计
- 数据结构实验之排序六:希尔排序
- 数据结构实验之排序五:归并求逆序数
- 数据结构实验之查找七:线性之哈希表
- 数据结构实验之查找六:顺序查找
- 数据结构实验之排序七:选课名单
- 数据结构实验:连通分量个数 分类: 图论 2015-06-09 17:33 8人阅读 评论(0) 收藏
- 数据结构实验1(一元多项式的相加和相乘)
- 数据结构实验之求二叉树后序遍历和层次遍历---2137
- SDUT-3362 数据结构实验之图论六:村村通公路
- SDUT 3343 数据结构实验之二叉树四:还原二叉树
- 数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历
- 数据结构实验之图论六:村村通公路
- SDUT2121数据结构实验之链表六:有序链表的建立
- 数据结构实验之图论四:迷宫探索
- 数据结构实验之排序三:bucket sort