利用数据结构和C语言所学的相关知识,实现单链表的创建、插入、删除、打印和查询功能。

作者&投稿:可飘 (若有异议请与网页底部的电邮联系)
利用数据结构和C语言所学的相关知识,实现单链表的创建、插入、删除、打印和查询功能。~

1.整数
# include "iostream.h"
# include "stdlib.h"
# define NULL 0
typedef struct list{
int data;
struct list* next;
}list,*LIST;
void create(LIST& head){//创建链表
LIST p1,p2;
head=p1=p2=(LIST)malloc(sizeof(list));
cout<<"please input a int type num,quit by pressing 0
data: ";
cin>>p1->data;
for(;p1->data!=0;){
p2=p1;
p1=(LIST)malloc(sizeof(list));
cout<<"data: ";
cin>>p1->data;
p2->next=p1;
}
p2->next=NULL;
if(head->data==0)
head=NULL;
}
void insert(LIST& head){//把元素插入链表
LIST p;
p=(LIST)malloc(sizeof(list));
cout<<"please input a int type data you want to insert
data: ";
cin>>p->data;
p->next=head;
head=p;

}
void Delete(LIST& head){//删除链表元素
LIST p,q;
p=head;
if(head==NULL)
cout<<"list NULL,erro
";
else{
cout<<"please input a int type num you want to delete
data: ";
int a;
cin>>a;
for(;p->data!=a&&p->next!=NULL;p=p->next)
q=p;
if(p->next==NULL&&p->data!=a)
cout<<a<<" is not in the list
";
else if(p->data==a)
if(p!=head)
q->next=p->next;
else
head=head->next;
}
}
void SORT(LIST& head){ //链表排序
LIST p,q,s;
if(head==NULL)
cout<<"list is NULL,you have no need to sort it
";
else{
for(p=head;p!=NULL;p=p->next){
s=p;
for(q=p;q!=NULL;q=q->next)
if(s->data>q->data)
s=q;
int a=p->data;
p->data=s->data;
s->data=a;
}
}
}
void print(LIST head){//输出链表
LIST p;
p=head;
if(head==NULL)
cout<<"list NULL,quit
";
else{
cout<<"
here is the list
";
for(;p!=NULL;p=p->next)
coutdata<<" ";
cout<<endl;
}
}
int main(){
LIST head;
create(head);
print(head);
insert(head);
SORT(head);
print(head);
Delete(head);
print(head);

return 0;
}


2.字符
#include
using namespace std;

typedef struct node
{
char data;
struct node *next;
}link;

link * get(link *l, int i)
{
link *p;int j=0;
p=l;
while((jnext!=NULL))
{p=p->next;j++;}
if(j==i)
return p;
else
return NULL;
}

link * ins (link *l, char ch,int i)
{ link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"输入有误"<<endl;
else
{
s=(link *)malloc(sizeof(link));
s->data=ch;
s->next=p->next;
p->next=s;
}
return l;
}

link * find(link *l, char ch)
{
link *p; int i=0; int j=0;
p=l;

while(p!=NULL)
{ i++;
if(p->data!=ch)
p=p->next;
else {cout<<"您查找的数据在第"<<i-1<<"个位置."<<endl;
j=1;p=p->next;
}

}
if(j!=1)
cout<<"您查找的数据不在线性表中."<<endl;
return l;
}


link * del(link *l, int i)
{
link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"输入有误"<<endl;
else
{
s=p->next;
p->next=s->next;
free(s);
}
return l;
}

link * add(link *l )
{
link *p,*s;
cout<<"请输入一串单字符数据,以*结束!"<<endl;
char ch;
link *HEAD;
link *R,*P,*L;
HEAD=(link *)malloc(sizeof(link));
HEAD->next=NULL;
R=HEAD;
getchar();
ch=getchar();
while(ch!='*')
{
P=(link *)malloc(sizeof(link));
P->data=ch;P->next=NULL;
R->next=P;R=R->next;
getchar();
ch=getchar();

}

L=HEAD;
cout<<"当前输入的线性表为:"<<endl;
P=L;P=P->next;
if(L!=NULL)
do
{coutdata<<" ";
P=P->next;
}while(P!=NULL);
cout<<endl;
p=l;
while(p->next!=NULL)
p=p->next;
s=L;
p->next=s->next;
p=l;
return l;
}


link * print(link *l)
{ int i,k;
char ch;
link *p,*q;
cout<<"当前线性表为:"<<endl;
p=l;p=p->next;
if(l!=NULL)
do
{coutdata<<" ";
p=p->next;
}while(p!=NULL);
cout<<endl;
cout<<"请选择您要的操作:";
cout<<" 1、插入";
cout<<" 2、查找";
cout<<" 3、删除";
cout<<" 4、合并";
cout<<" 0、退出";
cout<<endl;
cin>>k;
if(k==1)
{
cout<<"请输入您要插入的数据值:";
cin>>ch;
cout<<"请输入您要插入的位置:";
cin>>i;
p=ins(l,ch,i);
q=print(l);
}
else if(k==2)
{
cout<<"请输入您要查找的数据值:";
cin>>ch;
p=find(l,ch);
q=print(l);
}
else if(k==3)
{
cout<<"请输入您要删除的数据的位置:";
cin>>i;
p=del(l,i);
q=print(l);
}
else if(k==4)
{ p=add(l);
q=print(l);
}
else if(k==0)
;
else
{cout<<"输入错误!"<<endl;
q=print(l);}
return l;
}


int main()
{
cout<<"请输入一串单字符数据,以*结束!"<<endl;
char ch;
//link *head;
link *r,*p,*q,*l;
l=(link *)malloc(sizeof(link));
l->next=NULL;
r=l;
ch=getchar();
// getchar();
while(ch!='*')
{
p=(link *)malloc(sizeof(link));
p->data=ch;p->next=NULL;
r->next=p;r=r->next;
ch=getchar();
// getchar();
}
//l=head;
q=print(l);
return 0;

}



3.c
#include
#include
#define N 8
typedef struct node
{int data;
struct node *next;
}node;

node * createsl()
{
node *p,*s,*h;
int j=1,x;
p=s=h=(node*)malloc(sizeof(node));
h->next=NULL;
printf("please input the data to create the list,end with -1 or %d nupmbers
",N);
while(x!=-1&&j<=N)
{
printf("number %d:",j);
scanf("%d",&x);
s=(node*)malloc(sizeof(node));
s->data=x;
if(h->next==NULL)
h->next=s;
else
p->next=s;
p=s;
j++;
}
p->next=NULL;
return h;
}

int access(node *h,int i)
{
node *p;int j=1;
p=h->next;
while(p!=NULL)
{
if(p->data==i)
break;
p=p->next;
j++;
}
if(p!=NULL)
{
printf("find the number in position:%d
",j);
return(p->data);
}
else
{
printf("can't find the number in the list!
");
return -1;
}
}

void insertsl(node *h,int i)
{
node *p,*t;
int j=1;
p=h->next;;
while(p->next!=NULL)
{
p=p->next;
j++;
}

t=(node*)malloc(sizeof(node));
t->data=i;
t->next=p->next;
p->next=t;
printf("insert success in position %d
",j+1);
}


void deletesl(node *h,int i)
{
node *p,*s,*q;
int j=1;
p=h;
while(p->next!=NULL)
{
q=p->next;
if(q->data==i)
break;
p=p->next;
j++;
}

if(p->next==NULL)
{
printf("Can't find the number you want to delete.
");
return;
}
else
{
s=p->next;
p->next=s->next;
free(s);
printf("delete success in position %d
",j+1);
}
}

void print(node *h)
{
printf("
print all the data in the list:") ;
node *s;
s=h->next;
if(s!=NULL)
{
while(s!=NULL)
{
printf(" %d ",s->data) ;
s=s->next;
}
}
else
printf("the list is empty!%d");
printf("
");
}

int main()
{
node *p;
int a;
p=createsl() ;
printf("
you need find the number:
");
scanf("%d",&a);
access(p,a);

printf("
please input the number you want to insert:
");
scanf("%d",&a);
insertsl(p,a);

printf("
please input the number you want to delete:
");
scanf("%d",&a);
deletesl(p,a);

print(p);
return 0;
}

1.c++编的
#include
using namespace std;

typedef struct node
{
char data;
struct node *next;
}link;

link * get(link *l, int i)
{
link *p;int j=0;
p=l;
while((jnext!=NULL))
{p=p->next;j++;}
if(j==i)
return p;
else
return NULL;
}

link * ins (link *l, char ch,int i)
{ link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"输入有误"<<endl;
else
{
s=(link *)malloc(sizeof(link));
s->data=ch;
s->next=p->next;
p->next=s;
}
return l;
}

link * find(link *l, char ch)
{
link *p; int i=0; int j=0;
p=l;

while(p!=NULL)
{ i++;
if(p->data!=ch)
p=p->next;
else {cout<<"您查找的数据在第"<<i-1<<"个位置."<<endl;
j=1;p=p->next;
}

}
if(j!=1)
cout<<"您查找的数据不在线性表中."<<endl;
return l;
}


link * del(link *l, int i)
{
link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"输入有误"<<endl;
else
{
s=p->next;
p->next=s->next;
free(s);
}
return l;
}

link * add(link *l )
{
link *p,*s;
cout<<"请输入一串单字符数据,以*结束!"<<endl;
char ch;
link *HEAD;
link *R,*P,*L;
HEAD=(link *)malloc(sizeof(link));
HEAD->next=NULL;
R=HEAD;
getchar();
ch=getchar();
while(ch!='*')
{
P=(link *)malloc(sizeof(link));
P->data=ch;P->next=NULL;
R->next=P;R=R->next;
getchar();
ch=getchar();

}

L=HEAD;
cout<<"当前输入的线性表为:"<<endl;
P=L;P=P->next;
if(L!=NULL)
do
{coutdata<<" ";
P=P->next;
}while(P!=NULL);
cout<<endl;
p=l;
while(p->next!=NULL)
p=p->next;
s=L;
p->next=s->next;
p=l;
return l;
}


link * print(link *l)
{ int i,k;
char ch;
link *p,*q;
cout<<"当前线性表为:"<<endl;
p=l;p=p->next;
if(l!=NULL)
do
{coutdata<<" ";
p=p->next;
}while(p!=NULL);
cout<<endl;
cout<<"请选择您要的操作:";
cout<<" 1、插入";
cout<<" 2、查找";
cout<<" 3、删除";
cout<<" 4、合并";
cout<<" 0、退出";
cout<<endl;
cin>>k;
if(k==1)
{
cout<<"请输入您要插入的数据值:";
cin>>ch;
cout<<"请输入您要插入的位置:";
cin>>i;
p=ins(l,ch,i);
q=print(l);
}
else if(k==2)
{
cout<<"请输入您要查找的数据值:";
cin>>ch;
p=find(l,ch);
q=print(l);
}
else if(k==3)
{
cout<<"请输入您要删除的数据的位置:";
cin>>i;
p=del(l,i);
q=print(l);
}
else if(k==4)
{ p=add(l);
q=print(l);
}
else if(k==0)
;
else
{cout<<"输入错误!"<<endl;
q=print(l);}
return l;
}


int main()
{
cout<<"请输入一串单字符数据,以*结束!"<<endl;
char ch;
//link *head;
link *r,*p,*q,*l;
l=(link *)malloc(sizeof(link));
l->next=NULL;
r=l;
ch=getchar();
// getchar();
while(ch!='*')
{
p=(link *)malloc(sizeof(link));
p->data=ch;p->next=NULL;
r->next=p;r=r->next;
ch=getchar();
// getchar();
}
//l=head;
q=print(l);
return 0;

}


2.c语言编的

#include
#include
#define N 8
typedef struct node
{int data;
struct node *next;
}node;

node * createsl()
{
node *p,*s,*h;
int j=1,x;
p=s=h=(node*)malloc(sizeof(node));
h->next=NULL;
printf("please input the data to create the list,end with -1 or %d nupmbers
",N);
while(x!=-1&&j<=N)
{
printf("number %d:",j);
scanf("%d",&x);
s=(node*)malloc(sizeof(node));
s->data=x;
if(h->next==NULL)
h->next=s;
else
p->next=s;
p=s;
j++;
}
p->next=NULL;
return h;
}

int access(node *h,int i)
{
node *p;int j=1;
p=h->next;
while(p!=NULL)
{
if(p->data==i)
break;
p=p->next;
j++;
}
if(p!=NULL)
{
printf("find the number in position:%d
",j);
return(p->data);
}
else
{
printf("can't find the number in the list!
");
return -1;
}
}

void insertsl(node *h,int i)
{
node *p,*t;
int j=1;
p=h->next;;
while(p->next!=NULL)
{
p=p->next;
j++;
}

t=(node*)malloc(sizeof(node));
t->data=i;
t->next=p->next;
p->next=t;
printf("insert success in position %d
",j+1);
}


void deletesl(node *h,int i)
{
node *p,*s,*q;
int j=1;
p=h;
while(p->next!=NULL)
{
q=p->next;
if(q->data==i)
break;
p=p->next;
j++;
}

if(p->next==NULL)
{
printf("Can't find the number you want to delete.
");
return;
}
else
{
s=p->next;
p->next=s->next;
free(s);
printf("delete success in position %d
",j+1);
}
}

void print(node *h)
{
printf("
print all the data in the list:") ;
node *s;
s=h->next;
if(s!=NULL)
{
while(s!=NULL)
{
printf(" %d ",s->data) ;
s=s->next;
}
}
else
printf("the list is empty!%d");
printf("
");
}

int main()
{
node *p;
int a;
p=createsl() ;
printf("
you need find the number:
");
scanf("%d",&a);
access(p,a);

printf("
please input the number you want to insert:
");
scanf("%d",&a);
insertsl(p,a);

printf("
please input the number you want to delete:
");
scanf("%d",&a);
deletesl(p,a);

print(p);
return 0;
}

C++有,c的没有,不过可以自己改回去。
#include<iostream>
using namespace std;
int len=0;
struct list//结构的声明
{
int data;
list *next;
};
list *head;
list *create()//建立链表,这是第一步;
{
list *p,*q;
head=NULL;
int temp;
cout<<"Now create the list,Input the data,end by -1:"<<endl;
cin>>temp;
len=0;
while(temp!=-1)
{
len++;
p=new list;
p->data=temp;
if(head==NULL)
head=p;
else
{
q->next=p;
}
q=p;
cin>>temp;
}
if(head!=NULL)
q->next=NULL;
return head;
}
void display(list *head)//显示链表的所有数据。
{
if(head==NULL)
{
cout<<"The list is empty!"<<endl;
return;
}
cout<<"the list is:"<<endl;
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
}
void search(list *phead)//输入序号查找它的数据,验证此序号的数是否存在。
{
//cout<<"len="<<len<<endl;
int m;
list *r=NULL;
int count;
cout<<"Input the number you want to search:"<<endl;
cin>>m;
do
{
if(len<m||m<=0)
do
{
cout<<"not found,input the number you want to search,end by -1 !"<<endl;
cin>>m;

}while(len<m&&m!=-1);
if(m==-1)
break;
count=1;
r=new list;
r=phead;
while(1)
{
if(m==1)
{
cout<<"the data you want to search is: "<<endl;
cout<<r->data<<endl;
break;
}
else
{
count++;
r=r->next;
if(count==m)
{
cout<<"the data you want to search is: "<<endl;
cout<<r->data<<endl;
break;
}
if(r==NULL)
{
cout<<"Not found!"<<endl;
break;
}
}
}
cout<<"Input the number you want to search,end by -1:"<<endl;
cin>>m;
}while(m!=-1);
}
void find(list *head)//输入数据查找它的位置
{
int m,located=1,g;
list *temp1;
temp1=head;
cout<<"input the data you want to search: "<<endl;
cin>>m;
do
{
g=0;

if(temp1->data==m)
{
cout<<"have found,it located in "<<located<<endl;
located=1;
temp1=head;
cout<<"input the data you want to search,end by -1:"<<endl;
cin>>m;
}
else
{
located++;
temp1=temp1->next;
}
if(temp1==NULL)
{
cout<<"not found!input the data you want to search,end by -1:"<<endl;
located=1;
temp1=head;
cin>>m;
}
}while(m!=-1);
}
void insert(list *&head)//插入数据
{
int node,data,count;
struct list *temp1=NULL,*temp2=NULL,*temp3=NULL;

//temp=head;
cout<<"input the node and data:\n";
cin>>node>>data;
count=1;
if(node>len||node<=0)
{
do
{
cout<<"the node inputed wrong!try to input again:"<<endl;
cin>>node>>data;
}while(node>len||node<=0);
//return;
}
temp1=new list;
temp1->data=data;
if(head==NULL)
{
head=temp1;
head->next=NULL;
len++;
return ;
}
if(node==1)
{
temp1->next=head;
head=temp1;
len++;
return ;
}

temp2=head;
temp3=head->next;
while(temp3!=NULL)
{
count++;
if(count<node)
{
temp2=temp3;
temp3=temp3->next;
}
else
break;
}
temp2->next=temp1;
temp1->next=temp3;
len++;
return ;
}
list *del_node(list *&head)//输入节点删除对应的数据
{
cout<<"input the node you want to delete:"<<endl;
int node,count=1;
cin>>node;
if(node<=0||node>len)
{
do
{
cout<<"input wrong,try again!"<<endl;
cin>>node;
}while(node<=0||node>len);
}
if(head==NULL)
{
len--;
return head;

}
list *temp1=NULL,*temp2=NULL;

temp1=head;
if(node==1)
{
head=temp1->next;
delete temp1;
len--;
return head;
}
temp2=temp1->next;
while(temp2!=NULL)
{
count++;
if(count==node)
{
temp1->next=temp2->next;
delete temp2;
len--;
return head;
}
temp1=temp2;
temp2=temp2->next;
}
return head;
}
void del_data(list *&head)//输入数据删除对应的节点
{
cout<<"input the data you want to delete:"<<endl;
int data;
cin>>data;
list *temp1=NULL,*temp2=NULL;
temp1=head;
if(temp1->data==data)
{
head=temp1->next;
delete temp1;
len--;
return;
}
temp2=temp1->next;
while(temp2!=NULL)
{
if(temp2->data==data)
{
temp1->next=temp2->next;
delete temp2;
len--;
return;
}
temp1=temp2;
temp2=temp2->next;
}
cout<<"Not found the data!"<<endl;
return;
}
void main()
{
list *head=NULL;
head=create();
int choose;
cout<<"\nInput 1: display the list.\n"
<<"Input 2: input location to search the data.\n"
<<"Input 3: input the data to find its location.\n"
<<"Input 4: insert the data."
<<"\nInput 5: delete the node's data:"
<<"\nInput 6: delete the data you input:"
<<endl;
cin>>choose;
do{
switch(choose)//构建一个菜单供客户选择操作。
{
case 1:display(head);break;
case 2:search(head);break;
case 3:find(head);break;
case 4:insert(head);break;
case 5:del_node(head);break;
case 6:del_data(head);break;
default:cout<<"input wrong!try again!"<<endl;break;
}
cout<<"Input the choose,end by -1"<<endl;
cin>>choose;
}while(choose!=-1);

}

class List
{
private:
typedef struct listNode
{
char data;
struct listNode * nextPtr;
}LISTNODE;

typedef LISTNODE * LISTNODEPTR;

LISTNODEPTR startptr; //链表首地址

public:
List(LISTNODEPTR=NULL);
void insert(char); //把一个新值按排序顺序插入到链表中
char del(char); //删除一个链表元素
int isEmpty(); //如果链表为空,返回1,否则返回0
void printList(); //打印链表
};

//------------------------------------------------------------------------------
//构造函数
List::List(LISTNODEPTR stptr)
{
startptr=(stptr!=NULL)? stptr : NULL;
}
//------------------------------------------------------------------------------
//把一个新值按排序顺序插入到链表中
void List::insert(char value)
{
LISTNODEPTR newptr,previousptr,currentptr;
newptr = new LISTNODE;

if(newptr!=NULL)
{
newptr->data=value;
newptr->nextPtr =NULL;

previousptr =NULL;
currentptr=startptr;

while(currentptr!=NULL && value>currentptr->data )
{
previousptr = currentptr;
currentptr = currentptr->nextPtr;
}

if(previousptr==NULL)
{
newptr->nextPtr=startptr;
startptr=newptr;
}
else
{
previousptr->nextPtr=newptr;
newptr->nextPtr=currentptr;
}
}
else
cout<<value<<"not inserted.No memory available."<<endl;

}
//------------------------------------------------------------------------------
//删除一个链表元素
char List::del(char value)
{
LISTNODEPTR previousptr,currentptr,tempptr;
if(value==startptr->data )
{
tempptr=startptr;
startptr=startptr->nextPtr ;
delete [] tempptr;
return value;
}
else
{
previousptr=startptr;
currentptr=startptr->nextPtr;

while(currentptr!=NULL && currentptr->data != value)
{
previousptr=currentptr;
currentptr=currentptr->nextPtr ;
}

if(currentptr!=NULL)
{
tempptr=currentptr;
previousptr->nextPtr =currentptr->nextPtr ;
delete [] tempptr;
return value;
}
}

return 0;
}
//------------------------------------------------------------------------------
//如果链表为空,返回1,否则返回0
int List::isEmpty()
{
return startptr == NULL;
}

//------------------------------------------------------------------------------
//打印链表
void List::printList()
{
LISTNODEPTR currentptr;
currentptr = startptr;
if(currentptr==NULL)
cout<<"List is empty"<<endl;
else
{
cout<<"The list is:"<<endl;

while(currentptr!=NULL)
{
cout<<currentptr->data <<"-->";
currentptr = currentptr->nextPtr;
}

cout<<"NULL"<<endl;
}
}

#include <iostream.h>
#include <conio.h>

main()
{
List char_list;
unsigned int choice;
char item;

cout<<"请输入你的选择:"<<endl;
cout<<"1.插入一个元素到链表中。"<<endl;
cout<<"2.删除链表中的一个元素。"<<endl;
cout<<"3.结束。"<<endl;

cout<<"选择(1-3):"<<endl;
cin>>choice;

while(choice!=3)
{
switch(choice)
{
case 1:
cout<<"请输入一个字母:"<<endl;
cin>>item;
char_list.insert(item);
char_list.printList();
break;
case 2:
if(!char_list.isEmpty())
{
cout<<"请输入一个要从链表中删除的字母:"<<endl;
cin>>item;
if(char_list.del(item))
{
cout<<item<<"已经从链表中删除"<<endl;
char_list.printList();
}
else
{
cout<<item<<"没有在链表中找到"<<endl;
}
}
else
cout<<"链表是空的"<<endl;
break;
default:
cout<<"无效的选择"<<endl;

cout<<"请输入你的选择:"<<endl;
cout<<"1.插入一个元素到链表中。"<<endl;
cout<<"2.删除链表中的一个元素。"<<endl;
cout<<"3.结束。"<<endl;
break;

}
cout<<"选择(1-3):"<<endl;
cin>>choice;
choice = choice;
}
cout<<"结束程序"<<endl;
cout<<"Press any key to exit."<<endl;
getch();
return 0;
}

typedef int ElemType;
struct sNode
{
ElemType data;
struct sNode* next;
};
//初始化链表
void InitList(struct sNode** HL)
{
*HL = NULL;
}
//清空链表
void ClearList(struct sNode** HL)
{
struct sNode *cp, *np;
cp = *HL;
while(cp != NULL)
{
np = cp->next;
free(cp);
cp = np;
}
*HL = NULL;
}
//返回单链表的长度
int SizeList(struct sNode* HL)
{
int i = 0;
while(HL != NULL)
{
i++;
HL = HL->next;
}
return i;
}
//判断单链表是否为空
int EmptyList(struct sNode* HL)
{
if(HL == NULL)
return 1;
else
return 0;
}
//返回表中第pos个结点中的元素
ElemType GetElem(struct sNode* HL, int pos)
{
int i = 0;
if (pos < 1)
{
printf("pos值非法,退出运行!

\n");
exit(1);
}
while (HL != NULL)
{
i++;
if (i == pos)
break;
HL = HL->next;
}
if (HL != NULL)
return HL->next;
else
{
printf("pos值非法,退出运行!

\n");
exit(1);
}
}
//遍历单链表
void TraverseList(struct sNode* HL)
{
while (HL != NULL)
{
printf("%5d", HL->data);
HL = HL->next;
}
printf("\n");
}
//查找值为x的第一个元素
ElemType* FindList(struct sNode* HL, ElemType x)
{
while (HL != NULL)
if (HL->data == x)
return &HL->data;
else
HL == HL->next;
return NULL;
}
//修改表中第pos个结点的值为x的值
int UpdatePosList(struct sNode*HL, int pos,

ElemType x)
{
int i = 0;
struct sNode* p = HL;
while (p != NULL)
{
i++;
if (pos == i)
break;
else
p =p->next;
}
if (pos == i)
{
p->data = x;
return 1;
}
else
return 0;
}
//向表头插入一个元素
void InsertFirstList(struct sNode** HL, ElemType

x)
{
struct sNode *newp;
newp = malloc(sizeof(struct sNode));
if (newp == NULL)
{
printf("内存动态空间用完,退出运

行!\n");
exit(1);
}
newp->data = x;
newp->next = *HL;
*HL = newp
}
//在第pos个结点位置添加一个元素为x的结点
int InsertPosList(struct sNode** HL, int pos,

ElemType x)
{
int i = 0;
struct sNode *newp;
struct sNode* cp = *HL, *ap = NULL;
if (pos <= 0)
{
printf("pos值不正确,返回0表示插

入失败!\n");
return 0;
}
while (cp != NULL)
{
i++;
if (pos == i)
break;
else
{
ap = cp;
cp = cp->next;
}
}
newp = malloc(sizeof(struct sNode));
if (newp == NULL)
{
printf("内存动态空间用完,插入失

败!\n");
return 0;
}
newp->data = x;
if (ap == NULL)
{
newp->next = cp;
*HL = newp;
}
else
{
newp->next = cp;
ap->next = newp;
}
return 1;
}
//向有序表中插入元素x结点
void InsertOrderList(struct sNode** HL, ElemType

x)
{
struct sNode *newp;
struct sNode* cp = *HL, *ap = NULL;
newp = malloc(sizeof(struct sNode));
if (newp == NULL)
{
printf("内存动态空间用完,退出运

行!\n");
exit(1);
}
newp->data = x;
if (cp == NULL || x < cp->data)
{
newp->next = cp;
*HL = newp;
return;
}

while (cp != NULL)
{
if (x < cp->data)
break;
else
{
ap = cp;
cp = cp->next;
}
}
newp->next = cp;
ap->next = newp;

}
//从表中删除表头结点
ElemType DeleteFirstList(struct sNode** HL)
{
ElemType temp;
struct sNode* p = *HL;
if (*HL == NULL)
{
printf("单链表为空,无表头删除,

退出运行!\n");
exit(1);
}
*HL = (*HL)->next;
temp = p->data;
free(p);
return temp;
}
//删除表尾结点并返回值
ElemType DeleteLastList(struct sNode** HL)
{
ElemType temp;
struct sNode* cp = *HL;
struct sNode* ap = NULL;
if (cp == NULL)
{
printf("单链表为空,无表头删除,

退出运行!\n");
exit(1);
}
while (cp->next != NULL)
{
ap = cp;
cp = cp->next;
}
if (ap == NULL)
*HL = (*HL)->next;
else
ap->next = NULL;
temp = cp->data;
free(cp);
return temp;
}

//删除第pos个结点并返回它的值
ElemType DeletePosList(struct sNode** HL, int

pos)
{
int i = 0;
ElemType temp;
struct sNode* cp = *HL;
struct sNode* ap = NULL;
if (cp == NULL || pos <= 0)
{
printf("单链表为空或pos值不正确,

退出运行!\n");
exit(1);
}
while (cp != NULL)
{
i++;
if (i == pos)
break;
ap = cp;
cp = cp->next;
}
if (cp == NULL)
{
printf("pos值不正确,退出运行!

\n");
exit(1);
}
if (pos == 1)
*HL = (*HL)->next;
else
ap->next = cp->next;
temp = cp->data;
free(cp);
return temp;
}
//删除值为x的第一个结点
int DeleteValueList(struct sNode** HL, ElemType

x)
{
struct sNode* cp = *HL;
struct sNode* ap = NULL;
while (cp != NULL)
{
if (cp->data == x)
break;
ap = cp;
cp = cp->next;
}
if (cp == NULL)
return 0;
if (ap == NULL)
*HL = (*HL)->next;
else
ap->next = cp->next;
free(cp);
return 1;
}

c语言和数据结构如何互相结合学习
答:就是先学习C语言的一些基本知识,比如数组啊,指针啊...然后可以根据数据结构里的算法(不是程序),按C语言的文法编写程序

问下学完c语言和数据结构之后,学什么比较好
答:接下来可以学习算法设计与分析、然后学C++或者Java、接着再学习设计模式,有了这些基础,无论是面向过程的语言还是面向对象的语言,都能够比较从容的使用了。

数据结构与c语言的关系
答:② 不要太认为你没学数据结构和算法难以解决实际编程问题。③ C语言只要你熟练掌握,这就是你学Data structure and Algorithms 的基础。④ 学好数构和算法的前提是:你C语言用得比较熟练了(特别是指针、复合变量、数组的编程运用)⑤ 最后,你只要看一本关于数据结构和算法的书就够了《算法导论》(国...

数据结构(C语言版)目录
答:数据结构(C语言版)的详细内容概览如下:第1章,数据结构概论,涵盖了数据结构的基本概念,如数据的组织和存储方式,以及与之相关的算法和算法分析。本章通过实例帮助理解,随后的习题旨在巩固所学。第二章,数组与矩阵,首先介绍数组的基本概念,然后深入探讨矩阵的压缩存储方式,以及矩阵的运算。通过这些内...

学数据结构要具备哪些C语言知识
答:学数据结构:1、数据类型要清楚;2、数组 3、函数 4、指针 5、结构体 其中函数、指针、结构体非常重要,如果这一块搞不清楚的话,要想学好数据结构很难

数据结构算法与c语言的关系?
答:C语言是工具,数据结构是基础,算法是核心且有难有易,初级的编程只要懂编程语言和一般算法即可,至于数据结构可作一般了解;中级的编程要对数据结构和算法有深入的理解和掌握;高级的编程就需要完全理解各种数据结构以及自己编写算法了!不过现在的很多程序员都是在中级阶段的居多吧!

学习数据结构是不是和学过的C语言程序设计很有关联?
答:数据结构是计算机学科一个重要组成部分,但是数据结构不依赖某种特定的语言。不过只要你要编程,就不能离开数据结构,如果你语言学的不好,那么是无法实现相应的数据结构的算法的。建议你好好巩固一下程序设计语言,这样再学数据结构的时候不至于太痛苦。数据结构讲述计算机中数据的组织方式,如线性表、链表、...

数据结构和c语言有什么区别吗,为什么数据结构上都是算法,都没有具体的...
答:数据结构主要是向我们讲述程序的算法 即思维方式 书上面讲的算法并不是一个个的实例 而是一些伪代码 要真正的实现书上所讲的程序 需要真正了解那些伪代码的含义 并正确的将其转化成c语言程序 学数据结构主要是学的那些思维方式 而真正的实现程序那就需要你自己的基本工 和经验了 ...

关于数据结构和C语言的联系。
答:其实呢,数据结构不是针对某一计算机语言(比如c语言)而发展的科学,只是我们从一开始学的就是c语言,所以我们用c来贯穿数据结构。目前大多数计算机语言都用得到数据结构,对于各种计算机语言,数据结构都是相通的。指针是c语言的灵魂,用不好的话也是c语言的噩梦,正所谓成也萧何败也萧何,我们可以看到...

学好数据结构是不是非要学好C语言
答:用C描述的数据结构(严蔚敏版)里面用到了大量的C语言的知识特别是C的精华——指针,以及一系列的C的知识,什么结构体啦等等。所以如果你选择看用C描述的数据结构那么学好C是毋容置疑的,反过来,学习数据结构也是学习C的一种非常好的途径,特别是指针。如果你熟悉其他的语言,也可以选择其他语言描述的...