#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
//單鏈表的定義
typedef int elemtype;
typedef struct Node
{
elemtype data;
struct Node *next;
}Node,*linklist;
//單鏈表的初始化
void init_linklist(linklist *l)
{
*l=(linklist)malloc(sizeof(Node));
(*l)->next=NULL;
}
//頭插法建立單鏈表
void createfromhead(linklist l)
{
Node *s;
char c;
int flag=1;
while(flag)
{
c=getchar();
fflush(stdin);
if(c!='0')
{
s=(Node*)malloc(sizeof(Node));
s->data=https://bbs.csdn.net/topics/c;
s->next=l->next;
l->next=s;
}
else
flag=0;
}
}
//單鏈表的插入
int inslist(linklist l,int i,elemtype e)
//在帶頭結點的單鏈表l中第i個位置插入值為e的節點
{
Node *pre,*s;
int k;
if(i<=0)
return ERROR;
pre=l; k=0;//從頭開始查找,查找第i-1個節點
while(pre!=NULL&&k<i-1) //標為查完且未查到第i-1個是重復,找到pre只想第i-1個
{
pre=pre->next;
k=k+1;
}//查找第i-1個節點
if(pre==NULL)//如當前位置pre為空表表示一找完,但還為到第i個,說明插入位置不合理
{
printf("插入位置不合理");
return ERROR;
}
s=(Node*)malloc(sizeof(Node)); //申請一個新的節點
s->data=https://bbs.csdn.net/topics/e; //值e放入s的資料與
s->next=pre->next; //修改指標,完成插入
pre->next=s;
return OK;
}
void main()
{
linklist l;
Node *p;
int i,r,j;
//建立單鏈表
init_linklist(&l);
printf("用頭插法建立單鏈表,請輸入鏈表資料\n");
createfromhead(l);
p=l->next;
while(p!=NULL)
{
printf("%c\n", *p );
p=p->next;
}
//單鏈表的插入
printf("請輸入你要插入的位置:\n");
scanf("%d",&i);
printf("請輸入你要插入的數字:\n");
scanf("%d",&r);
inslist(l,i,r);
printf("結果為:\n");
p=l->next;
while(p!=NULL)
{
printf("%c\n", *p );
p=p->next;
}
}
結果不對,出來的結果里沒有插入的那個數
uj5u.com熱心網友回復:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
//單鏈表的定義
typedef int elemtype;
typedef struct Node
{
elemtype data;
struct Node *next;
}Node,*linklist;
//單鏈表的初始化
void init_linklist(linklist *l)
{
*l = (linklist)malloc(sizeof(Node));
(*l)->next=NULL;
}
//頭插法建立單鏈表
void createfromhead(linklist l)
{
Node *s;
char c;
int flag=1;
while(flag)
{
c = getchar();
//fflush(stdin);
getchar();
if(c!='0')
{
s=(Node*)malloc(sizeof(Node));
s->data=https://bbs.csdn.net/topics/c;
s->next=l->next;
l->next=s;
}
else
flag=0;
}
}
//單鏈表的插入
int inslist(linklist l,int i,elemtype e)
//在帶頭結點的單鏈表l中第i個位置插入值為e的節點
{
Node *pre,*s;
int k;
if(i<=0)
return ERROR;
pre=l; k=0;//從頭開始查找,查找第i-1個節點
while(pre!=NULL&&k<i-1) //標為查完且未查到第i-1個是重復,找到pre只想第i-1個
{
pre=pre->next;
k=k+1;
}//查找第i-1個節點
if(pre==NULL)//如當前位置pre為空表表示一找完,但還為到第i個,說明插入位置不合理
{
printf("插入位置不合理");
return ERROR;
}
s=(Node*)malloc(sizeof(Node)); //申請一個新的節點
s->data=https://bbs.csdn.net/topics/e; //值e放入s的資料與
s->next=pre->next; //修改指標,完成插入
pre->next=s;
return OK;
}
//void main()
int main()
{
linklist l;
Node *p;
int i,r,j;
//建立單鏈表
init_linklist(&l);
printf("用頭插法建立單鏈表,請輸入鏈表資料\n");
createfromhead(l);
p = l->next;
while(p!=NULL)
{
//printf("%c\n", *p );
printf("%c\n", p->data );
p=p->next;
}
//單鏈表的插入
printf("請輸入你要插入的位置:\n");
scanf("%d",&i);
getchar();
printf("請輸入你要插入的數字:\n");
//scanf("%d",&r);
scanf("%c",&r);
if (inslist(l,i,r) == ERROR) {
printf("Insert error!\n");
return -1;
}
printf("結果為:\n");
p=l->next;
while(p!=NULL)
{
//printf("%c\n", *p );
printf("%c\n", p->data );
p=p->next;
}
}
供參考~
插入演算法沒什么問題,問題在于main函式里用的r是%d格式的,注意'1'和1是不相等的。
如果按照樓主的寫法,可以考慮輸入51,這樣插入的是'3',
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/175434.html
標籤:C語言
上一篇:glut.h頭檔案的呼叫
