Ⅰ 数据结构c语言编程 急求!!!!
//在一个递增有序的线性表L中插入一个值为x的元素,并保持其递增有序特性
bool listInsert(node* L, elementType x)
{
//注意:带有空的头结点L
node* p = L->next;
node* s = new node;// 创建一个新节点
s->data = x;//装入数据
if (p->data >= x)//排在第一的特例
{
s->next = L->next;//插入操作
L->next = s;
return true;
}
while ((p != NULL) && (p->next != NULL)) //搜索ai-1节点指针
{
if (p->next->data >= x && p->data < x)
{
//插入新节点
s->next = p->next;
p->next = s;
return true;
}
p = p->next;
}
//排在最后的情况
//插入新节点
s->next = NULL;
p = s;
return true;
}
【答题不易,请采纳谢谢】
Ⅱ 数据结构c语言编程
#include "stdio.h"
#include <malloc.h>
#include <stdlib.h>
#include <string>
struct Student
{
int num;
char name[20];
int sex;//0女,1男
int score;
}stu;
struct list
{
struct Student student;
struct list *next;
};
struct list *CreatFemale(struct list *head,struct Student stu)//女生信息
{
struct list *p;
struct list *p1,*p2;
p = head;
p1 = (struct list *)malloc(sizeof(struct list));
p1->student = stu;
if (head == NULL)
{
head = p1;
p1->next = NULL;
}
else
{
while (p->next != NULL && stu.score >= p->student.score)
{
p2 = p;
p = p->next;
}
if (stu.score < p->student.score)
{
if (p == head)
{
head = p1;
p1->next = p;
}
else
{
p2->next = p1;
p1->next = p;
}
}
else
{
p->next = p1;
p1->next = NULL;
}
}
return head;
}
struct list *CreatMale(struct list *head,struct Student stu)//男生信息
{
struct list *p;
struct list *ptr;
struct list *q;
struct list *p1,*p2;
q = head;
ptr = head;
p1 = (struct list *)malloc(sizeof(struct list));
p1->student = stu;
while (q->next != NULL && q->student.sex == 0)
{
ptr = q;
q = q->next;
}
p = q;
if (head == NULL)
{
head = p1;
p1->next = NULL;
}
else
{
while (p->next != NULL && stu.score >= p->student.score)
{
p2 = p;
p = p->next;
}
if (stu.score < p->student.score && p->student.sex == 1)
{
if (p == q )
{
p = p1;
ptr->next = p1;
p1->next = q;
}
else
{
p2->next = p1;
p1->next = p;
}
}
else
{
p->next = p1;
p1->next = NULL;
}
}
return head;
}
void Print(struct list *head)//输出到文件
{
FILE *fp;
char Sex[10];
fp = fopen("data1.txt","w");
if (fp == NULL)
{
printf("can't open the file!\n");
exit(0);
}
struct list *p;
struct list *q;
p = head;
while (p != NULL)
{
if (p->student.sex == 0)
{
strcpy(Sex,"女");
}
else
{
strcpy(Sex,"男");
}
fprintf(fp,"%d %s %s %d\n",p->student.num,p->student.name,Sex,p->student.score);
q = p->next;
free(p);
p = q;
}
fclose(fp);
}
main()
{
struct list *head;
FILE *fp;
head = NULL;
fp = fopen("data.txt","r");
if (fp == NULL)
{
printf("can't open the file!\n");
exit(0);
}
while (!feof(fp))
{
fscanf(fp,"%d%s%*c%d%d",&stu.num,stu.name,&stu.sex,&stu.score);
if (stu.sex == 0)
{
head = CreatFemale(head,stu);//女
}
}
fseek(fp,0L,0);
while (!feof(fp))
{
fscanf(fp,"%d%s%*c%d%d",&stu.num,stu.name,&stu.sex,&stu.score);
if (stu.sex == 1)
{
head = CreatMale(head,stu);//男的
}
}
fclose(fp);
Print(head);
}
Ⅲ c语言 数据结构在c语言编程时到底怎么用
我刚开始学数据结构时也有一样的问题,数据结构就是结构
比如你有一个好的算法
算法涉及到了
后进先出
那你就可以直接用栈结构,首先在自己的代码里自定义栈结构
pop
push
等等函数
就和定义结构体那样
定义好了
下面就直接调用那些pop
函数之类的
就可以了。
Ⅳ 数据结构 c语言编程
#include "stdlib.h"
#include "iostream.h"
#define ERROR 0
#define OK 1
typedef int elemtype;
typedef struct lnode{
elemtype data;
struct lnode* next;
}lnode,*linklist;
void init_linklist(linklist &p)//链表参数采用引用参数,因初始化链表是从没有链表到有链表,并且要保留这个空链表以便后面使用,所以必须采用引用参数。当然采用指针参数也是可以的。
{
p=(linklist)malloc(sizeof(lnode));
p->next=NULL;
}
void crt_linklist(linklist &p,int len)//链表参数采用引用参数,与初始化同理
{
init_linklist(p);
lnode* t;
t=p;
lnode* s;
for(int i=1;i<=len;i++)
{
s=(linklist)malloc(sizeof(lnode));
s->data=i*10;
s->next=NULL;
t->next=s;
t=s;
}
}
void output_linklist(linklist p)//链表参数采用赋值参数,因为输出不改变链表结构
{
lnode* q=p->next;
int i=1;//初值设为1,因为是从第一个元素开始输出的
while(q)
{
cout<<q->data<<","<<i<<" ";
q=q->next;
i++;
}
}
int main()
{
linklist p;
int i=30;//最好用int,这里是定义长度的,不是定义一个数据元素;或者typedef int Length; Length i=30;更好,呵呵
crt_linklist(p,i);
output_linklist(p);
return 0;
}
Ⅳ C语言编程(数据结构)
int*getnum(inta)
{
staticintn[10];
inti=a;
for(i=1;i<=a;i++)
{
intk=i;
while(i>0)
{
n[i%10]++;
i/=10;
}
i=k;
}
returnn;
}
intmain()
{
int*m=getnum(11);
inti;
for(i=0;i<10;i++)
{
printf("%d",m[i]);
}
getch();
return0;
}
Ⅵ C语言数据结构编程
#include<stdio.h>
#include<stdlib.h>
#include"string.h"
intmain(intargc,char*argv[])
{
char*buffer=(char*)malloc(sizeof(char)*100);
char*b=(char*)malloc(sizeof(char)*100);
memset(buffer,'