33,010
社区成员
发帖
与我相关
我的任务
分享
#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 CreateListF(int ch)
{
ListNode *s;
head=NULL;
while(ch!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
s->next=head;
head=s;
ch=getchar();
}
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);
}
void print(LinkList *l)
{
ListNode *s;
s=l;
while(s->next!=NULL)
{
printf("%5d",s->data);
s=s->next;
}
}
main()
{
int a,b,c;
clrscr();
printf("Please crease list:\n");
do
{
scanf("%d",&c);
CreateListF(c);
}while(c!=0);
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);
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)
{
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;
}
// 322.cpp : Defines the entry point for the console application.
//
#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=NULL;
LinkList CreateListF(int ch)
{
ListNode *s;
// head=NULL;
while(ch!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
s->next=head;
head=s;
ch=getchar();
}
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);
}
void print(LinkList l) //LinkList已经是ListNode*指针类型,不要再加*
{
ListNode *s;
s=l;
while(s!=NULL) //循环里面已经取了next
{
printf("%5d",s->data);
s=s->next;
}
}
int main() //怎么少了返回int
{
int a,b,c;
// clrscr(); 不知道这个函数是什么用
printf("Please crease list:\n");
/* do 这里的“0”应该是控制结束的标志吧,按照你这样写,最后0是作为一个元素在链表中的,
{ 要是不想要的话,看我下面的写法
scanf("%d",&c);
CreateListF(c);
}while(c!=0);
*/
scanf("%d",&c);
while(c!=0)
{
CreateListF(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);
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;
}