链表问题
程序如下:#include<stdio.h>
#include<stdlib.h>
struct lnode
{
char data;
struct lnode *next;
};
struct lnode *head;
setnull(struct lnode **p)
{
*p=NULL;
}
void insert(struct lnode **p,char x,int i)
{
int j=1;
struct lnode *s,*q;
s=(struct lnode *)malloc(sizeof(struct lnode));
s->data=x;
q=*p;
if(i==1)
{
s->next=q;
*p=s;
}
else
{
while(j<i-1&&q->next!=NULL)
{
q=q->next;j++;
}
if(j==i-1)
{
s->next=q->next;
q->next=s;
}
else
printf("位置参数不对\n");
}
}
void del(struct lnode **p,int i)
{
int j=1;
struct lnode *q=*p,*t;
if(i==1)
{
t=q;
*p=q->next;
}
else
{
while(j<i-1&&q->next!=NULL)
{
q=q->next;j++;
}
if(q->next!=NULL&&j==i-1)
{
t=q->next;
q->next=t->next;
}
else
printf("位置不对.");
}
if(t!=NULL)
free(t);
}
void display(struct lnode **p)
{
struct lnode *q;
q=*p;
printf("链表显示:");
if(q==NULL)
printf("链表为空:");
else if(q->next==NULL)
printf("%c\n",q->data);
else
{
while(q->next!=NULL)
{
printf("%c-",q->data);q=q->next;
}
printf("%c",q->data);
}
printf("\n");
}
void main()
{
setnull(&head);
insert(&head,'a',1);
display(&head);
}
这是一个单链表,是通过直接接入数据,如果我想先输入一组数据,然后再通过某些条件再插入某个地方的话,那么这组数据应该如何生成呢? //pHead是头结点,i是计数器;
void createlist(struct Inode *pHead,int i)
{
structInode *pTemp;
int iCount;
pTemp=pHead;
for(iCount=0;iCount<i;iCount++)
{
scanf("%c",pHead->data);
pHead=pHead->netx;
}
pHead=pTemp;
}
页:
[1]