当前位置:编程学习 > C/C++ >>

C语言链表问题。急!!!

题目是这样的:

(等看到答案再加分)

  1 建立一个数据域长度不超过n的字符串的单链表

  2 将一指定字符串插入到单链表的指定位置,不允许重复

  3 将单链表中一指定字符串删除

 

答案:#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int n;

typedef struct node

{

     char string[80];

     struct node *next;

}link;

link *creat()

{

link *head,*p1,*p2;

p1=p2=(link *)malloc(sizeof(link));

gets(p1->string);

while(p1->string[0]!='0')

{

n++;

if(n==1) head=p1;

else p2->next=p1;

p2=p1;

p1=(link *)malloc(sizeof(link));

gets(p1->string);

}

p2->next=NULL;

free(p1);

return head;

}

link *ins(link *head,char *str,int i)

{

if(i>n)

return head;

link *p1,*p2,*p0;

int h;

p1=head;h=0;

while(h<i)

{p2=p1;p1=p1->next;h++}

p0=(link *)malloc(sizeof(link));

strcpy(p0->string,str);

p0->next=p1;

p2->next=p0;

return head;

}

link *del(link *head,char *str)

{

link *p1,*p2;

p1=head;

while(strcmp(p1->string,str)&&p1->next)

{p2=p1;p1=p1->next}

if(p1->next)

{p2->next=p1->next;free(p1)}

else printf("%s not found!\n",str);

return head;

}

main函数我不写了。。。

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int ig;                                                                  /*定义全局变量*/
struct link                                                              /*结构体指针*/
{
 char str[20];                                                     /*定义数据域*/
 struct link *next;                                               /*指针域    */
};
struct link *create(void)                                                /*创建      */
{ struct link *head=NULL,*new,*tail;
  int icount=0;                                                    /*计数器初始化*/
  while(1)
  {  new=(struct link *)malloc(sizeof(struct link));
    printf("input the string(-1 to end) No. %d:",icount+1);
    scanf("%s",new->str);
    if(strcmp(new->str,"-1")==0)                             /*输入-1时结束*/
    {  new=NULL;
      free(new);                                       /*释放new   */
      return head;                                     /*结束输入  */
    }
    else
    {  icount++;                                        /*计数      */
      if(icount==1)                                    /*第一个结点*/
      {  head=tail=new;
      }
      else
      {  tail->next=new;
        tail=new;                                /*以后的所有结点*/
      }
    }
  }
}
struct link *add(struct link *head,int ik,char ckey[20])                               /*添加结点    */
{  struct link *p,*new;
  int ij;
  new=(struct link *)malloc(sizeof(struct link));                  /*新结点分配内存*/
  strcpy(new->str,ckey);
  new->next=NULL;                                          /*设置当前指针域*/
  if(head==NULL)
    head=new;
  else
  {  if(ik==0)                                        /*新结点在head后*/
      new->next=head,head=new;
    else
    {  p=head,ij=0;
      while(p->next!=0&&ij<ik-1)
      {  p=p->next,ij++;                  /*新结点在链表中部*/
      }
      new->next=p->next,p->next=new;
    }
    return head;
  }
}
struct link *delete(struct link *head,int ikey)                          /*删除函数     */
{  struct link *p,*q;
 

上一个:C语言宏定义 和 编程模块化
下一个:C语言与E语言的区别

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,