C语言链表的建立与插入

Python018

C语言链表的建立与插入,第1张

#include

<stdio.h>

#include

<stdlib.h>

//使用结构体构建链表

struct

node{

int

data

struct

node

*next

}

void

main()

{

int

a,n=1

struct

node

*p,*head,*t

head=(struct

node

*)malloc(sizeof(struct

node))

//p=(struct

node

*)malloc(sizeof(struct

node))

//申请动态空间

p=head

//申请动态空间

t=(struct

node

*)malloc(sizeof(struct

node))

for(n<=5n++)

//输入1,3,5,7,9

{

p->data=2*n-1

p->next=(struct

node

*)malloc(sizeof(struct

node))

p=p->next

}

printf("原始链表如下:\n")

//输出原始链表

for(p=headp->next!=NULLp=p->next)

{

printf("%d

",p->data)

}

printf("\n请输入需要插入的数据\n")

//输入所要插入的新数据

scanf("%d",&a

)

for(p=headp->next!=NULL)

//按顺序插入相应位置

{

if(p->data

<=

a

&&

(p->next)->data

>=

a)

{

t->data

=a

t->next

=p->next

p->next=t

break

}

p=p->next

}

printf("插入新数据后的链表\n")

//输出插入新数据的链表

for(p=headp->next!=NULL)

{

printf("%d

",p->data)

p=p->next

}

printf("\n")

free(p)

free(head)

free(t)

}

因为你insert函数只是传进head的值,只是值传递,不能对head本身进行修改,只能对head->next以及后面的内容进行修改。

用二级指针,就可以改head了

#include<iostream>

using namespace std

struct Node

{

     int num

     Node *next

}

Node * create()

{

     Node *head,*p1,*p2

     p1 = new Node

     cout<<"输入数字: "

     cin>>p1->num

     p2=head=p1

     while(p1->num!=0)

     {

           p1 = new Node

           cout<<"输入数字: "

           cin>>p1->num

           p2->next = p1

           p2=p1

    }

     p2->next=NULL

     return head

}

void display(Node *head)

{

      Node *p

      int i

      for(p=head, i=1 p->next!=NULL i++,p=p->next)

      {

            cout<<"第"<<i<<"个元素是: "<<p->num<<endl

      }

}

void insert(Node  **head)

{

      int position

      cout<<"插入的位置: "

      cin>>position

      Node *p

      p = *head

      Node *NewNode= new Node

      cout<<"输入数字: "

      cin>>NewNode->num

      if(position == 0)

      {        

           NewNode->next = p

           *head = NewNode 

      }

}

int main()

{

     Node *a =create()

     display(a)

     insert(&a)

     display(a)

     return 0

}

其实最后你应该加个释放函数,用delete释放所有节点

根据题意:

一、链表创建:根据输入的数字,动态创建任意多个节点插入链表。(题目规定n<=40,如不想使用malloc动态申请内存,需直接定义最大上限40个节点)。

二、链表排序:交换节点内容(不是地址),保留链表指针的值(*next的值)。

三、打印链表:利用链表指针遍历链表。

四、对动态申请的链表地址空间释放(在本程序中创建后程序就结束了,即使不写free释放,结束后也会释放。但在复杂程序中调用创建,后续还有代码,需像我代码中写函数动释放不用的内存,避免浪费)。

下面是代码:

#include <stdio.h>

#include <malloc.h>

typedef struct numStruct

{

  int num

  struct numStruct *next

}NST

NST *insert2List(int num)//根据数字创建节点(动态),插入链表(首次自动生成头节点),成功返回头节点,失败返回NULL。

void showList(NST *nsthead)//打印链表

void px(NST *nsthead)//链表排序

void freeList(NST *nsthead)//释放链表内存

int main()

{

  NST *nsthead=NULL

  int i=0,n=50,*nums=NULL

  while(n>40)

      scanf("%d",&n)

  nums=(int *)malloc(sizeof(int)*n)

  if(!nums) return 1

  while(i<n)

      scanf("%d",&nums[i++])

  i=0

  while(i<n)

      nsthead=insert2List(nums[i++])

  px(nsthead)

  showList(nsthead)

  freeList(nsthead)

  return 0

}

void freeList(NST *nsthead)

{

  NST *temp=NULL,*nst=NULL

  if(nsthead)

  {

      nst=nsthead->next

      while(nst!=NULL)

      {

          temp=nst

          nst=nst->next

          free(temp)

      }

  }

  free(nsthead)

}

void showList(NST *nsthead)

{

  if(nsthead)

      while(nsthead->next!=NULL)

      {

          printf("%d ",nsthead->next->num)

          nsthead=nsthead->next

      }

  printf("\n")

}

void px(NST *nsthead)

{

  NST *nt1=NULL,*nt2=NULL,ntTemp,*nextSave=NULL

  if(nsthead)

  {

      nt1=nsthead->next

      while(nt1)

      {

          nt2=nt1->next

          while(nt2)

          {

              if(nt1->num>nt2->num)

              {

                  ntTemp=*nt1

                  nextSave=nt1->next

                  *nt1=*nt2

                  nt1->next=nextSave

                  nextSave=nt2->next

                  *nt2=ntTemp

                  nt2->next=nextSave

              }

              nt2=nt2->next

          }

          nt1=nt1->next

      }

  }

}

NST *insert2List(int num)

{

  static NST *nsthead=NULL,*nstTail=NULL

  NST *nstNew=NULL

  nstNew=(NST *)malloc(sizeof(NST))

  if(!nstNew) return NULL

  nstNew->next=NULL

  nstNew->num=num

  if(!nsthead)

  {

      nsthead=(NST *)malloc(sizeof(NST))

      if(!nsthead) return NULL

      nsthead->next=nstNew

  }

  else

      nstTail->next=nstNew

  nstTail=nstNew

  return nsthead

}