A. c语言编程题输入两个有序循环链表合成一个有序循环链表
有好几处错误和遗漏,帮你改了,你看下:
#include<stdio.h>
#include<stdlib.h>
#defineNULL0
structnode
{
intData;
structnode*next;
};
main()
{
structnode*p1,*q1,*head1;
structnode*p2,*q2,*head2;
structnode*p3,*q3,*head3;
intx,i;
p1=q1=head1=p2=q2=head2=p3=q3=head3=NULL;
scanf("%d",&x);
while(x!=-1)
{
p1=(structnode*)malloc(sizeof(structnode));
p1->Data=x;
if(q1==NULL)
{
q1=p1;
q1->next=NULL;
head1=p1;
}
if(q1!=NULL)
{
q1->next=p1;
q1=p1;
}
scanf("%d",&x);
}
p1->next=NULL;
scanf("%d",&i);
while(i!=-1)
{
p2=(structnode*)malloc(sizeof(structnode));
p2->Data=i;
if(q2==NULL)
{
q2=p2;
q2->next=NULL;
head2=p2;
}
if(q2!=NULL)
{
q2->next=p2;
q2=p2;
}
scanf("%d",&i);
}
p2->next=NULL;
p1=head1;
p2=head2;
p3=(structnode*)malloc(sizeof(structnode));
q3=head3=p3;
while(p1!=NULL&&p2!=NULL)
{
if(p1->Data<p2->Data)
{
//q3->Data=p1->Data;
q3->next=p1;
q3=p1;
p1=p1->next;
}
else
{
//q3->Data=p2->Data;
q3->next=p2;
q3=p2;
p2=p2->next;
}
if(p1==NULL)
q3->next=p2;
if(p2==NULL)
q3->next=p1;
}
if(q3!=NULL)q3->next=NULL;
p3=head3->next;
while(p3!=NULL)
{
printf("%d",p3->Data);
p3=p3->next;
}
}
B. 用c语言实现一个有序单链表
不多说,直接看代码:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
typedefstructyinshu{
unsignedintyz;
structyinshu*next;
}YSNode,*YinShu;//定义链表节点类型YSNode,和指针YinShu
voidInsertNode(unsignedintn,YinShu&L){
//在L链表的第一个节点之后插入一个节点,值为n
YinShup;
p=(YinShu)malloc(sizeof(YSNode));//分配节点空间
p->yz=n;
p->next=L->next;
L->next=p;
}
voidYinShuFenJie(unsignedintn,YinShu&L){
//对n进行质因数分解,并存在链表L中
unsignedintj,k;
k=(unsignedint)sqrt(n);
for(j=2;j<=k;++j){
if(n%j==0){//遇到一个质因数j
InsertNode(j,L);
n/=j;
k=(unsignedint)sqrt(n);
--j;
}
}
InsertNode(n,L);//插入剩下的质因数n
}
intmain(){
unsignedintn;
YinShuL,p,q;
scanf("%d",&n);
L=(YinShu)malloc(sizeof(YSNode));
L->yz=n;
L->next=NULL;//第一个节点存放n的值
YinShuFenJie(n,L);
p=L->next;q=p->next;
printf("%u=%u",L->yz,p->yz);
free(L);free(p);//释放第一、二个节点
while(q){
p=q;
printf("*%u",p->yz);
q=p->next;
free(p);
}
printf(" Finished. ");
getch();
return0;
}
希望能帮到你!
C. 用c语言建立一个有序链表
先按正常流程建立一个链表,再按照其某一个成员值进行冒泡排序(排序过程的交换,只交换链表指针以外的成员值)。
演示代码如下:(演示代码链表20个节点,成员值为随机值)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct slist
{
int a;
struct slist *next;
}SLIST;
SLIST *init();//生成20个节点成员为随机数的链表
void showList(SLIST *slHead);//打印链表
void px(SLIST *slHead,int flag);//float=1:降序。=2升序
int main()
{
SLIST *slHead=NULL;
slHead=init();
printf("排序前: ");
showList(slHead);
printf(" 降序排序后: ");
px(slHead,1);
showList(slHead);
printf(" 升序排序后: ");
px(slHead,2);
showList(slHead);
return 0;
}
void px(SLIST *slHead,int flag)//flag=1:降序。=2升序
{
SLIST *sl0=slHead->next,*sl1=NULL,slSave,*pSave=NULL;
while(sl0)
{
sl1=sl0->next;
while(sl1)
{
if((flag==1 && sl0->a<sl1->a)||(flag==2 && sl0->a>sl1->a))
{
slSave=*sl0;
*sl0=*sl1;
sl0->next=slSave.next;
pSave=sl1->next;
*sl1=slSave;
sl1->next=pSave;
}
sl1=sl1->next;
}
sl0=sl0->next;
}
}
void showList(SLIST *slHead)
{
int i=0;
while(slHead->next)
{
printf("节点%d成员值:%d ",++i,slHead->next->a);
slHead=slHead->next;
}
printf(" ");
}
SLIST *init()
{
int num,cnt=20;
static SLIST head;
SLIST *slHead=&head,*slTail=NULL,*slNew=NULL;
slHead->next=NULL;
srand(time(NULL));
while(cnt--)
{
num=rand()%100;
slNew=(SLIST *)malloc(sizeof(SLIST));
if(!slNew)return NULL;
slNew->a=num;
slNew->next=NULL;
if(!slHead->next)
slHead->next=slNew;
else
slTail->next=slNew;
slTail=slNew;
}
return slHead;
}
D. 用C语言写一个有序链表,链表类型为字符串.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct _Node {
int data;
struct _Node *next;
};
typedef struct _Node Node;
// 交换两个结点的数据
void SwapNodeData(Node *p1, Node *p2) {
int temp = p1->data;
p1->data = p2->data;
p2->data= temp;
}
// 冒泡排序对链表进行排序
void BubbleSort(Node *head) {
Node *pTemp;
int maxIdx, idx;
// 计算链表长度
maxIdx = 0;
for (pTemp = head; pTemp != NULL; pTemp = pTemp->next)
++maxIdx;
idx = 0;
while (idx < maxIdx-1) {
for (pTemp = head; idx < maxIdx-1; pTemp = pTemp->next, ++idx) {
if (pTemp->data > pTemp->next->data)
SwapNodeData(pTemp, pTemp->next);
}
idx = 0;
--maxIdx;
}
}
int main(void)
{
Node *head = NULL, *temp = NULL, *p = NULL;
int i;
srand((unsigned int)time(NULL));
// 产生随机数链表
head = (Node *)malloc(sizeof(Node));
head->data = rand() % 40;
p = head;
for (i = 1; i < 20; ++i) {
temp = (Node *)malloc(sizeof(Node));
temp->data = rand() % 40;
p->next = temp;
p = p->next;
}
p->next = NULL;
// 输出随机数链表
for (p = head; p != NULL; p = p->next)
printf("%d ", p->data);
printf("\n");
// 对链表排序
BubbleSort(head);
// 输出以排序的链表
for (p = head; p != NULL; p = p->next)
printf("%d ", p->data);
printf("\n");
// 释放资源
for (p = head->next; p != NULL; p = p->next) {
free(head);
head = p;
}
free(head);
head = NULL;
getchar();
return 0;
}
E. C语言 把两个有序链表合并为一个有序链表(递增)
设链表结点结构为Node(int data, Node *next),typedef Node List,链表均带表头结点。
思路是:把list1中的元素看成是集合1,把list2中的元素看成是集合2,把list1头结点(即list1结点)从集合1中脱离下来看成是目标集合的头结点,目标集合开始时是空集,并用last指针始终指向该集合的尾部,然后每次从集合1和集合2中取各自的第一个元素进行比较,较小者从相应集合里脱离,插入到目标集合list1的尾部即last的末尾,并将刚插入的元素作为目标集合list1的新的last,直到集合1为空或集合2为空时结束,最后将未空的集合中的剩余元素链接到last后面即可。
F. 有序链表的合并,c语言
#include<stdio.h>
#include<malloc.h>
typedefstructlist{
intdata;
structlist*next;//下一个节点地址
}list;
//第一条链表
structlist*L=NULL;//头
structlist*head=NULL;//首
structlist*p=NULL;
//第二条链表
structlist*L1=NULL;//头
structlist*head1=NULL;//首
structlist*p1=NULL;
//代理链表
structlist*L2=NULL;//头
structlist*q=NULL;//L2备用地址
structlist*q1=NULL;//备用地址
intmain(){
inti=0,length;
printf("请输入链表的长度 ");
scanf("%d",&length);
head=(structlist*)malloc(sizeof(structlist));
L=head;
printf("请依次输入链表的内容 ");
for(i;i<length;i++){
p=(structlist*)malloc(sizeof(structlist));
scanf("%d",&p->data);
p->next=NULL;
head->next=p;
head=p;
}
inti1=0,length1;
printf("请输入链表的长度 ");
scanf("%d",&length1);
head1=(structlist*)malloc(sizeof(structlist));
L1=head1;
printf("请依次输入链表的内容 ");
for(i1;i1<length1;i1++){
p1=(structlist*)malloc(sizeof(structlist));
scanf("%d",&p1->data);
p1->next=NULL;
head1->next=p1;
head1=p1;
}
L2=(structlist*)malloc(sizeof(structlist));
q=L2;//备用合并链表起始地址
p=L->next;
p1=L1->next;
while(p&&p1){
if(p->data<p1->data){
L2->next=p;
L2=p;
p=p->next;
}elseif(p->data==p1->data){
L2->next=p;
L2=p;
p=p->next;
q1=p1->next;//备用相同元素的下一个地址指向
free(p1);
p1=q1;
}elseif(p->data>p1->data){
L2->next=p1;
L2=p1;
p1=p1->next;
}
}
L2->next=p?p:p1;
free(L1);
printf("合并后链表的内容 ");
p=q->next;
while(p){
printf("%d",p->data);
p=p->next;
}
}
G. c语言采用头插法或尾插法建立链表,从键盘输入递增有序的数据建立链表
#include<stdio.h>
#include<stdlib.h>
/*定义链表结点*/
typedefstructst_node{
intvalue;
structst_node*next;
}node_t;
/*定义链表*/
typedefstruct{
node_thead;
node_t*tail;
}list_t;
/*插入到队列尾部*/
voidlist_push_back(list_t*l,intvalue){
node_t*t=(node_t*)malloc(sizeof(node_t));
t->value=value;
t->next=NULL;
l->tail->next=t;
l->tail=t;
}
/*有序地插入元素*/
voidlist_push_sort(list_t*l,intvalue){
/*找出小于或等于value的节点,插入到该节点前面*/
node_t*p=l->head.next,*last=&l->head,*t;
for(;p;last=p,p=p->next){
if(value<=p->value){
t=(node_t*)malloc(sizeof(node_t));
t->value=value;
t->next=p;
last->next=t;
return;
}
}
/*如果没有小于或等于value的节点,则直接插入到末尾*/
list_push_back(l,value);
}
/*使用数组初始化有序链表*/
voidlist_init(list_t*l,int*p,ints){
inti=0;
l->head.next=NULL;
l->tail=&l->head;
for(;i<s;++i){
list_push_sort(l,p[i]);
}
}
/*清空链表*/
voidlist_clear(list_t*l){
node_t*p=l->head.next,*t;
while(p){
t=p;
p=p->next;
free(t);
}
l->head.next=NULL;
l->tail=&l->head;
}
/*合并有序链表*/
voidlist_merge(list_t*l,list_t*r,list_t*o){
node_t*pl=l->head.next,*pr=r->head.next;
while(pl||pr){
if(pl&&pr){
if(pl->value<=pr->value){
list_push_back(o,pl->value);
pl=pl->next;
}else{
list_push_back(o,pr->value);
pr=pr->next;
}
}elseif(pl){
list_push_back(o,pl->value);
pl=pl->next;
}else{
list_push_back(o,pr->value);
pr=pr->next;
}
}
}
/*删除相同结点*/
voidlist_plicate_delete(list_t*l){
if(&l->head!=l->tail){
node_t*p=l->head.next,*last,*t;
intvalue=p->value;
last=p;
p=p->next;
while(p){
if(value==p->value){
t=p;
last->next=p->next;
p=p->next;
free(t);
}else{
value=p->value;
last=p;
p=p->next;
}
}
}
}
/*打印链表*/
voidlist_show(char*name,list_t*l){
node_t*p=l->head.next;
printf("%s:",name);
for(;p;p=p->next){
printf("%d,",p->value);
}
printf(" ");
}
/*主函数*/
voidmain(){
list_tlist1,list2,list3;
inta[]={10,4,6,12,1,8,14,10,14,6};
intb[]={7,11,6,1,13,5,1,14};
/*所有链表需要初始化后才能使用*/
list_init(&list1,a,sizeof(a)/sizeof(int));
list_init(&list2,b,sizeof(b)/sizeof(int));
list_init(&list3,NULL,0);
printf("初始值: ");
list_show("List1",&list1);
list_show("List2",&list2);
list_show("List3",&list3);
/*合并链表*/
list_merge(&list1,&list2,&list3);
printf("合并后: ");
list_show("List1",&list1);
list_show("List2",&list2);
list_show("List3",&list3);
/*去重复*/
list_plicate_delete(&list3);
printf("去重复后: ");
list_show("List1",&list1);
list_show("List2",&list2);
list_show("List3",&list3);
/*所有链表都需要释放空间*/
list_clear(&list1);
list_clear(&list2);
list_clear(&list3);
}
//这可是血汗钱啊.....
H. 设计一个有序链表建立的程序,能够将无序输入的整数生成有序链表. 用c语言描述,函数名最好是李云清版本的
我给你一个,我调试过的
include <stdio.h>
#include <string.h>
#include <malloc.h>
#define NULL 0
struct stu
{
int num;
int age;
struct stu *next;
};
struct stu * creat(int n)
{
struct stu * head,*pb,*pf;
int i;
for(i=0;i<n;i++)
{
pb=(struct stu *)malloc(sizeof(struct stu));
printf("please input num and age\n");
scanf("%d,%d",&pb->num,&pb->age);
if(i==0)
pf=head=pb;
else
pf->next=pb;
pb->next= NULL ;
pf=pb;
}
return(head);
}
struct stu * search(struct stu * head,int num)
{
struct stu *p;
p=head;
while(p->num!=num&&p->next!=NULL)
p=p->next;
if(p->num==num)
return p;
if(p->num!=num &&p->next==NULL)
printf("not found %d\n",num);
}
struct stu * delet(struct stu * head,int num)
{
struct stu *pb,*pf;
if(head==NULL)
{
printf("\nempty list!\n");;
return head;
}
pb=head;
while(pb->num!=num&&pb->next!=NULL)
{
pf=pb;
pb=pb->next;
}
if(pb->num==num)
{
if(pb==head)
head=pb->next;
else
{
pf->next=pb->next;
}
free(pb);
printf("The node is delete:\n");
}
else
printf("can not find %d",num);
return head;
}
struct stu * insert(struct stu * head,struct stu *pi)
{
struct stu * pb,*pf;
pb=head;
if(head==NULL)
{
head=pi;
pi->next=NULL;
}
else
{
while((pi->num>pb->num)&&(pb->next!=NULL))
{
pf=pb;
pb=pb->next;
}
if(pi->num<=pb->num)
{
if(head==pb)
head=pi;
else
pf->next=pi;
pi->next=pb;
}
else
{
pb->next=pi;
pi->next=NULL;
}
return head;
}
}
void print(struct stu * head)
{
printf("Number\t\tAge\n");
while(head!=NULL)
{
printf("%d\t\t%d\n",head->num,head->age);
head=head->next;
}
}
int main(void)
{
struct stu * head,*pnum;
int n,num;
printf("input numbers of node:");
scanf("%d",&n);
head=creat(n);
print(head);
printf("Input the deleted number: ");
scanf("%d",&num);
head=delet(head,num);
print(head);
printf("Input the inserted number and age: ");
pnum=(struct stu *)malloc(sizeof(struct stu));
scanf("%d%d",&pnum->num,&pnum->age);
head=insert(head,pnum);
print(head);
}
如果你只需要其中的一部分,可以删除不需要的。 比如不要插入, 可以删除insert函数
I. 用C语言编写一个算法,实现有序链表的插入。链表有序且不允许有重复元素
如代码所示,c++语言,设带头节点的单链表L是一个递增有序表,试写一个函数,将x插入L中,并使L仍是一个有序表。望采纳!