c语言无头链表的录入 查看 插入 删除

来源:互联网 发布:arch linux kde 编辑:程序博客网 时间:2024/06/03 23:07

.c文件

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <Point.h>


/* run this program using the console pauser or add your own getch, system("pause") or input loop */




int main(int argc, char *argv[]) {
point *head=NULL;
int row,col;
point *p,*q;
product(&head);          //录入 
showpoints(head);//查看 
printf("请输入要删除的坐标:\n");
scanf("%d %d",&row,&col);
deletepoint(&head,row,col);//删除 
showpoints(head);//查看 

return 0;
}


.h文件

#ifndef  _POINT_
#define  _POINT_
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define length sizeof(point)
typedef struct point
{
int row;
int col;
struct point *next;
 } point;
void product(point **head);

void product(point **head)
{
point *p,*q;

int row,col,n=0;
if(*head)
{
printf("你已经完成了录入"); 

else
{
*head=p=q=NULL;
p=q=(point *)(malloc(length));
printf("请输入有效点的坐标:\n");
scanf("%d%d",&q->row,&q->col);
while(q->row&&q->col)
{
n++;
if(n==1)
{
*head=q;
}
   else
    {
    p->next=q;
}
p=q;
q=(point *)(malloc(length));
printf("请输入有效点的坐标:\n");
scanf("%d %d",&q->row,&q->col);

}
p->next=NULL;







}
void showpoints(point *head);
void showpoints(point *head)
{
point *p;

printf("你输入的点的坐标如下:\n");
p=head;
do
{
printf("%d,%d\n",p->row,p->col);
p=p->next;
}
while(p!=NULL);
}
void  deletepoint(point **head, int row,int col);
    void  deletepoint(point **head, int row,int col)
{
    point *p = *head;
    point *q = NULL;


    if (p->row==row&&p->col==col)
    {
        *head = p->next;
        free(p);
        return;
    }
    else
    {
        while (p!= NULL)
        {
            q =p;
            p= p->next;
            if (p==NULL)
            {
                printf("\n------------未找到要删除的或已删除多个该坐标----------------\n\n");          
                return;
            }
            else if (p->row==row&&p->col==col)
            {
                q->next = p->next;
                free(p);
                p=*head;
            }
        }
    }


}


#endif  _POINT_  

0 0
原创粉丝点击