#include<stdio.h>
#include<malloc.h>
typedef int DataType;
typedef struct Node{
DataType data;
struct Node *next;
}SLNode;
int main(){
int i,x,k,num1,num2,input1,input2;
SLNode *head1;
SLNode *head2;
SLNode *head3;
ListInitiate(&head1);
ListInitiate(&head2);
ListInitiate(&head3);
printf("輸入第一條單鏈表元素個數:\n");
scanf("%d",&num1);
printf("\n輸入第一條單鏈表元素:\n");
for(i=0;i<num1;i++){
scanf("%d",&input1);
ListInsert(head1,i,input1);
}
printf("輸入第二條單鏈表的元素個數:\n");
scanf("%d",&num2);
printf("\n輸入第二條單鏈表元素:\n");
for(i=0;i<num2;i++){
scanf("%d",&input2);
ListInsert(head2,i,input2);
}
printf("\n輸入插在第一條單鏈表的位置:\n");
scanf("%d",&k);
List(head1,head2,head3,k);
for(i=0;i<ListLength(head3);i++){
ListGet(head3,i,&x);
printf("%d",x);
}
return 0;
}
//初始化
void ListInitiate(SLNode **head){
*head=(SLNode *)malloc(sizeof(SLNode));
(*head)->next=NULL;
}
//求當前元素個數
int ListLength(SLNode *head){
SLNode *p=head;
int size=0;
while(p->next!=NULL){
p=p->next;
size++;
}
return size;
}
//插入
int ListInsert(SLNode *head,int i,DataType x){
SLNode *p,*q;
int j;
p=head;
j=-1;
while(p->next!=NULL&&j<i-1){
p=p->next;
j++;
}
if(j!=i-1){
printf("插入元素位置錯誤!");
return 0;
}
q=(SLNode *)malloc(sizeof(SLNode));
q->data=https://bbs.csdn.net/topics/x;
q->next=p->next;
p->next=q;
return 1;
}
//取出
int ListGet(SLNode *head,int i,DataType *x){
SLNode *p;
int j;
p=head;
j=-1;
while(p->next!=NULL&&j<i){
p=p->next;
j++;
}
if(j!=i){
printf("取出元素位置錯誤!");
return 0;
}
*x=p->data;
return 1;
}
// 一個單鏈表中插入另一個
int List(SLNode *head1,SLNode *head2,SLNode *head3,int k){
SLNode *p,*q,*m;
p=head1->next;
q=head2->next;
m=head3;
int a,x;
for(a=0;a<k;a++){
m->next=p;
p=p->next;
m=m->next;
}
for(int j=0;j<ListLength(head2);j++){
m->next=q;
q=q->next;
m=m->next;
}
for(int n=0;n<ListLength(head1)-a;n++){
m->next=p;
p=p->next;
m=m->next;
}
沒有報錯,但什么結果都運行不出來,望大佬解答,為什么運行不出結果,怎么改
uj5u.com熱心網友回復:
大佬何在大佬教我謝謝大佬轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/106007.html
標籤:基礎類
下一篇:USB
