33,008
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define ERROR 0
typedef char DataType;
typedef struct node{
DataType data;
struct node *next;
}ListNode;
typedef ListNode *LinkList;
ListNode *p;
LinkList head;
LinkList CreateListR(int ch)
{
ListNode *s,*r;
r=head;
while((ch=getchar())!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
r->next=s;
r=s;
}
r->next=NULL;
return head;
}
ListNode *GetNode(LinkList head,int i)
{
int j;
ListNode *p;
p=head;
j=0;
while(p->next&&j<i)
{
p=p->next;
j++;
}
if(i==j)
return p;
else
return NULL;
}
int InsertList(LinkList head,DataType x,int i)
{
ListNode *p,*s;
p=GetNode(head,i-1);
if(p==NULL)
return ERROR;
s=(ListNode *)malloc(sizeof(ListNode));
s->data=x;
s->next=p->next;
p->next=s;
}
int DeleteList(LinkList head,int i)
{
ListNode *p,*r;
p=GetNode(head,i-1);
if(p==NULL||p->next==NULL)
return ERROR;
r=p->next;
p->next=r->next;
free(r);
}
int print(LinkList l)
{
ListNode *s;
s=l;
while(s!=NULL)
{
printf("%5d",s->data);
s=s->next;
}
printf("\n");
}
int main()
{
int a,b,c;
printf("Please create list:\n");
scanf("%d",&c);
while(c!=0)
{
CreateListR(c);
scanf("%d",&c);
}
printf("The list is:\n");
print(head);
printf("Please input the number you want to insert:\n");
scanf("%d",&a);
printf("Please input insert position:\n");
scanf("%d",&b);
InsertList(head,a,b);
print(head);
printf("Please input the position you want to delete:\n");
scanf("%d",&b);
DeleteList(head,b);
printf("The list is:\n");
print(head);
return 0;
}
LinkList CreateListR(void)
{
LinkList head=(LinkList)malloc(sizeof(ListNode));
ListNode *s,*r;
int ch;
r=head;
printf("Please create list:\n");
while((ch=getchar())!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
r->next=s;
r=s;
}
r->next=NULL;
return head;
}
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define ERROR 0
typedef char DataType;
typedef struct node{
DataType data;
struct node *next;
}ListNode;
typedef ListNode *LinkList;
ListNode *p;
LinkList head;
LinkList CreateListR(void)
{
LinkList head=(LinkList)malloc(sizeof(ListNode));
ListNode *s,*r;
int ch;
r=head;
printf("Please create list:\n");
while((ch=getchar())!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
r->next=s;
r=s;
}
r->next=NULL;
return head;
}
ListNode *GetNode(LinkList head,int i)
{
int j;
ListNode *p;
p=head;
j=0;
while(p->next&&j<i)
{
p=p->next;
j++;
}
if(i==j)
return p;
else
return NULL;
}
int InsertList(LinkList head,DataType x,int i)
{
ListNode *p,*s;
p=GetNode(head,i-1);
if(p==NULL)
return ERROR;
s=(ListNode *)malloc(sizeof(ListNode));
s->data=x;
s->next=p->next;
p->next=s;
}
int DeleteList(LinkList head,int i)
{
ListNode *p,*r;
p=GetNode(head,i-1);
if(p==NULL||p->next==NULL)
return ERROR;
r=p->next;
p->next=r->next;
free(r);
}
int print(LinkList l)
{
ListNode *s;
s=l;
while(s->next!=NULL)
{
s=s->next;
printf("%5d",s->data);
}
printf("\n");
}
int main()
{
int a,b,c;
CreateListR();
printf("The list is:\n");
print(head);
printf("Please input the number you want to insert:\n");
scanf("%d",&a);
printf("Please input insert position:\n");
scanf("%d",&b);
InsertList(head,a,b);
print(head);
printf("Please input the position you want to delete:\n");
scanf("%d",&b);
DeleteList(head,b);
printf("The list is:\n");
print(head);
return 0;
}
]
LinkList CreateListR(int ch)
{
LinkList head=(LinkList)malloc(sizeof(ListNode));
ListNode *s,*r;
r=head;
while((ch=getchar())!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
r->next=s;
r=s;
}
r->next=NULL;
return head;
}
int print(LinkList l)
{
ListNode *s;
s=l;
while(s->next!=NULL)//不要用s!=NULL
{
s=s->next;
printf("%5d",s->data);
}
printf("\n");
return 1;
}