69,336
社区成员
发帖
与我相关
我的任务
分享
typedef struct Node
{
int data;
struct Node *next;
}Node,*LinKlist;
//假设带表头结点的单向链表头指针为head,试编写一个算法将值为5的结点插入到连接表的第k个结点前,并对该链表进行排序。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
struct NODE {
int data;
struct NODE *next;
} H,*head,*p,*q;
int i,j,k,n,t;
int main() {
srand(time(NULL));
//填写头节点数据
H.data=-1;
H.next=NULL;
head=&H;
//创建10个节点的单链表
p=head;
for (i=0;i<10;i++) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) return 1;
q->data=10+rand()%90;//填写10..99的随机值
q->next=NULL;
p->next=q;
p=q;
}
//输出整个单链表
p=head->next;
while (1) {
if (NULL==p) {
printf("\n");
break;
}
printf("%02d->",p->data);
p=p->next;
}
//将值为5的结点插入到单链表的第k个结点前
k=3;
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==n) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) return 1;
q->data=5;
q->next=p->next;
p->next=q;
break;
}
p=p->next;
}
//输出整个单链表
p=head->next;
while (1) {
if (NULL==p) {
printf("\n");
break;
}
printf("%02d->",p->data);
p=p->next;
}
//从小到大排序
for (p=head->next;p!=NULL && p->next!=NULL;p=p->next) {
for (q=p->next;q!=NULL;q=q->next) {
if (p->data > q->data) {
t=p->data;p->data=q->data;q->data=t;
}
}
}
//输出整个单链表
p=head->next;
while (1) {
if (NULL==p) {
printf("\n");
break;
}
printf("%02d->",p->data);
p=p->next;
}
//释放所有节点
p=head->next;
while (1) {
if (NULL==p) {
break;
}
q=p->next;
free(p);
p=q;
}
return 0;
}
//20->75->80->82->78->33->54->63->10->99->
//20->75->05->80->82->78->33->54->63->10->99->
//05->10->20->33->54->63->75->78->80->82->99->
#include <stdio.h>
#include <string.h>
#include <malloc.h>
//int LengthList(LinKlist);
//int deletelist(LinKlist, int, int);
typedef struct Node
{
int data;
struct Node *next;
}Node,*LinKlist;
int LengthList(LinKlist);//LinKlist都还没定义呢。。放到struct定义后边。。
int deletelist(LinKlist, int, int);
int main()
{
int n=0;
LinKlist l=(LinKlist)malloc(sizeof(Node));
LinKlist start=l;
while (n>=0)
{
if(n==10)
{
l->next=NULL;
break;
}
l->data=n+1;
l->next=(LinKlist)malloc(sizeof(Node));
l=l->next;
n++;
}
l=start;
if(deletelist(start,3,7)==0)
{
while (n>=0)
{
printf("%d ",l->data);
l=l->next;
if(l->next==NULL)
{
break;
}
}
}
}
int deletelist(LinKlist l,int min,int max)
{
Node *q=l,*p=l->next;
int del=0;
while (p)
{
if(p->data<min)
{
q=p;
p=p->next;
}
else if(p->data<=max)
{
q->next=p->next;
free(p);
p=q->next;
del=1;
}
else
{
break;
}
}
if(del==0)
return -1;
return 0;
}
int LengthList(LinKlist pHead)
{
int length = 0;
LinKlist n = pHead->next;
while(NULL != n)
{
++length;
n = n->next;
}
return length;
}
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int LengthList(LinKlist);
//int deletelist(LinKlist, int, int);
typedef struct Node
{
int data;
struct Node *next;
}Node,*LinKlist;
int main()
{
int n=0;
LinKlist l=(LinKlist)malloc(sizeof(Node));
LinKlist start=l;
while (n>=0)
{
if(n==10)
{
l->next=NULL;
break;
}
l->data=n+1;
l->next=(LinKlist)malloc(sizeof(Node));
l=l->next;
n++;
}
l=start;
if(deletelist(start,3,7)==0)
{
while (n>=0)
{
printf("%d ",l->data);
l=l->next;
if(l->next==NULL)
{
break;
}
}
}
}
int deletelist(LinKlist l,int min,int max)
{
Node *q=l,*p=l->next;
int del=0;
while (p)
{
if(p->data<min)
{
q=p;
p=p->next;
}
else if(p->data<=max)
{
q->next=p->next;
free(p);
p=q->next;
del=1;
}
else
{
break;
}
}
if(del==0)
return -1;
return 0;
}
int LengthList(LinKlist pHead)
{
int length = 0;
LinKlist n = pHead->next;
while(NULL != n)
{
++length;
n = n->next;
}
return length;
}
就是我注释的那里