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仍是一個有序表。望採納!