#include
<
stdio.h
>
#include < malloc.h >
#define NULL 0
#define LEN sizeof(struct student)
struct student
... {
int data;
struct student *next;
} ;
struct student * creat( )
... {
struct student *head, *p, *s;
p = (struct student *)malloc( LEN );
scanf("%d", &p->data);
head = NULL;
while( 0 != p->data)
...{
if(head == NULL)
head = p;
else
...{
s = (struct student *)malloc( LEN );
scanf("%d",&s->data);
p->next = s;
p = s;
}
}
p->next = NULL;
return(head);
}
void print( struct student * head)
... {
struct student *p;
p = head;
while(p != NULL)
...{
printf("%d ",p->data);
p = p->next;
}
}
int main()
... {
struct student *head;
head = creat();
print(head);
getch();
return 0;
}
#include < malloc.h >
#define NULL 0
#define LEN sizeof(struct student)
struct student
... {
int data;
struct student *next;
} ;
struct student * creat( )
... {
struct student *head, *p, *s;
p = (struct student *)malloc( LEN );
scanf("%d", &p->data);
head = NULL;
while( 0 != p->data)
...{
if(head == NULL)
head = p;
else
...{
s = (struct student *)malloc( LEN );
scanf("%d",&s->data);
p->next = s;
p = s;
}
}
p->next = NULL;
return(head);
}
void print( struct student * head)
... {
struct student *p;
p = head;
while(p != NULL)
...{
printf("%d ",p->data);
p = p->next;
}
}
int main()
... {
struct student *head;
head = creat();
print(head);
getch();
return 0;
}
插入和删除新的节点
#include
<
stdio.h
>
#include < malloc.h >
#define NULL 0
#define LEN sizeof(struct student)
struct student
... {
int data;
struct student *next;
} ;
struct student * creat( ) /**/ /* 创建无头节点的链表 */
... {
struct student *head, *p, *s; /**//* s为新开辟的接点 */
p = (struct student *)malloc( LEN );
scanf("%d", &p->data);
head = NULL;
while( 0 != p->data)
...{
if(head == NULL)
head = p;
else
...{
s = (struct student *)malloc( LEN );
scanf("%d",&s->data);
p->next = s;
p = s;
}
}
p->next = NULL;
return(head);
}
struct student * insert( struct student * head, struct student * stu)
/**/ /* 插入新的接点 */
... {
struct student *p, *q, *s; /**//* p为游标结点,q为p前结点,s为插入结点 */
p = head;
s = stu;
if(head == NULL)
...{
head = s;
s ->next = NULL;
}
else
...{
while((s->data > p->data)&&(p->next != NULL))
...{
q = p;
p = p->next;
}
if(s->data <= p->data)
...{
if(head == p)
head = s;
else
q->next = s;
s->next = p;
}
else
...{
p->next = s;
s->next =NULL;
}
}
return(head);
}
struct student * del( struct student * head, int num)
/**/ /* 删除节点函数 */
... {
struct student *p, *q, *f; /**//* 释放删除节点的内存 */
if(head == NULL)
...{
printf("linklist null! ");
goto end;
}
else
...{
p = head;
while((num != p->data)&&(p->next != NULL))
...{
q = p;
p = p->next;
}
if(num == p->data)
...{
if( p == head )
...{
f = head;
head = p->next;
free(f);
}
else
...{
f = p;
q->next = p->next;
free(f);
}
}
}
end:
return head;
}
void print( struct student * head) // 输出链表
... {
struct student *p;
p = head;
printf("head=%d ",head->data);
while(p != NULL)
...{
printf("%d ",p->data);
p = p->next;
}
}
int main()
... {
struct student *head, *s;
int data;
head = creat();
print(head);
printf("insert new node: ");
s = (struct student *)malloc(LEN);
scanf("%d",&s->data);
head = insert(head,s);
printf("now the linklist is: ");
print(head);
printf("input the delete num: ");
scanf("%d",&data);
head = del(head, data);
print(head);
getch();
return 0;
}
#include < malloc.h >
#define NULL 0
#define LEN sizeof(struct student)
struct student
... {
int data;
struct student *next;
} ;
struct student * creat( ) /**/ /* 创建无头节点的链表 */
... {
struct student *head, *p, *s; /**//* s为新开辟的接点 */
p = (struct student *)malloc( LEN );
scanf("%d", &p->data);
head = NULL;
while( 0 != p->data)
...{
if(head == NULL)
head = p;
else
...{
s = (struct student *)malloc( LEN );
scanf("%d",&s->data);
p->next = s;
p = s;
}
}
p->next = NULL;
return(head);
}
struct student * insert( struct student * head, struct student * stu)
/**/ /* 插入新的接点 */
... {
struct student *p, *q, *s; /**//* p为游标结点,q为p前结点,s为插入结点 */
p = head;
s = stu;
if(head == NULL)
...{
head = s;
s ->next = NULL;
}
else
...{
while((s->data > p->data)&&(p->next != NULL))
...{
q = p;
p = p->next;
}
if(s->data <= p->data)
...{
if(head == p)
head = s;
else
q->next = s;
s->next = p;
}
else
...{
p->next = s;
s->next =NULL;
}
}
return(head);
}
struct student * del( struct student * head, int num)
/**/ /* 删除节点函数 */
... {
struct student *p, *q, *f; /**//* 释放删除节点的内存 */
if(head == NULL)
...{
printf("linklist null! ");
goto end;
}
else
...{
p = head;
while((num != p->data)&&(p->next != NULL))
...{
q = p;
p = p->next;
}
if(num == p->data)
...{
if( p == head )
...{
f = head;
head = p->next;
free(f);
}
else
...{
f = p;
q->next = p->next;
free(f);
}
}
}
end:
return head;
}
void print( struct student * head) // 输出链表
... {
struct student *p;
p = head;
printf("head=%d ",head->data);
while(p != NULL)
...{
printf("%d ",p->data);
p = p->next;
}
}
int main()
... {
struct student *head, *s;
int data;
head = creat();
print(head);
printf("insert new node: ");
s = (struct student *)malloc(LEN);
scanf("%d",&s->data);
head = insert(head,s);
printf("now the linklist is: ");
print(head);
printf("input the delete num: ");
scanf("%d",&data);
head = del(head, data);
print(head);
getch();
return 0;
}
链表的创建,插入,删除,打印输出(c)
struct
student
*
reverse(
struct
student
*
head)
... {
struct student *p, *q, *r;
p = head ;
q = p->next;
while(q != NULL)
...{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = NULL;
head = p;
return(head);
}
... {
struct student *p, *q, *r;
p = head ;
q = p->next;
while(q != NULL)
...{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = NULL;
head = p;
return(head);
}