c#-单链表
Posted 前天亦是明天
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#-单链表相关的知识,希望对你有一定的参考价值。
namespace MyLink; public class MyLinkedList private int _sizeget;set; public class MyTreeNode public int valget;set; public MyTreeNode nextget;set; public MyTreeNode(int val) this.val = val; public MyTreeNode headget;set; public MyLinkedList() head = new MyTreeNode(0); //虚拟头结点 this._size = 0; public int Get(int index) if(index < 0 || index >= this._size) return -1; var cur = head.next; while(index != 0) // 举个案例来测试边界 如 index=0,这个循环就不会执行 cur = cur.next; index--; return cur.val; public void AddAtHead(int val) var newHead = new MyTreeNode(val); newHead.next = head.next; head.next = newHead; this._size ++; public void AddAtTail(int val) var cur = head; // 指向虚拟节点 * while(cur.next != null) cur = cur.next; cur.next = new MyTreeNode(val); this._size++; public void AddAtIndex(int index, int val) var newNode = new MyTreeNode(val); var cur = head; while(index != 0) cur = cur.next; if(cur == null) break; index --; if(cur != null) newNode.next = cur.next; cur.next = newNode; this._size++; public void DeleteAtIndex(int index) if(index < this._size) var cur = head; while(index != 0) cur = cur.next; if(cur == null) return; index--; if (cur != null) cur.next = cur.next.next; this._size--; /** * Your MyLinkedList object will be instantiated and called as such: * MyLinkedList obj = new MyLinkedList(); * int param_1 = obj.Get(index); * obj.AddAtHead(val); * obj.AddAtTail(val); * obj.AddAtIndex(index,val); * obj.DeleteAtIndex(index); */
力扣707
c语言单链表问题,请问这个那里错了,本人菜鸟,下面是程序
#include<stdio.h>
#include<malloc.h>
typedef struct Node
int data;
struct Node *next;
Lnode , *LinkList;
LinkList TCreatList()
Lnode *H,*T,*P; int x;
H=(Lnode *)malloc(sizeof(Lnode));
H->next=NULL; T=H;
scanf("%d",&x);
while( x!=0)
P= (Lnode *)malloc(sizeof(Lnode));
P->data=x; P->next=NULL;
T->next=P; T=P;
scanf("%d",&x);
return H;
void PrintfList(LinkList H)
Lnode *p=H ;
while(p!=NULL)
printf("%d",p->data);
p=p->next;
int LengthList( LinkList H)
Lnode *p=H; int i=0;
while (p->next!=NULL)
i++; p=p->next;
return i;
printf ("链表的长度是%d/n",i);
Lnode *SearchX(LinkList H,int x)
Lnode *p=H->next;
while(p!=NULL && p->data != x)
p=p->next;
return p;
void InsertList(Lnode *H,int a,int x)
printf("请输入要插入的数据:\n");
Lnode *s, *q = H ;
s=(Lnode *)malloc(sizeof(Lnode));
s->data=x; s->next=NULL;
while(q->next!=NULL&&q->next->data!=a)
q=q->next;
s->next=q->next;
q->next=s;
int main()
LinkList H;
H=TCreatList();
PrintfList(H);
LengthList(H);
SearchX(H);
InsertList(H);
return 0;
之前写的一个程序,带测试信息,给你参考:我这里是用的全局节点变量,所有在插入时只传了2个参数,一个target目标(不是位置,换成指定位置更简单),一个数据:
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkNode
int data;
struct LinkNode *next;
link,*plink;
plink head = NULL;
void addfront()
int value;
plink s,p;
printf("please enter the value\n");
scanf("%d",&value);
s = malloc(sizeof(link));
if(s==NULL)
printf("memory error\n");
return;
s->data = value;
if(head == NULL)
head = s;
s->next = NULL;
else
p = head;
head = s;
s->next = p;
void addtail()
int value;
plink s,p;
s = malloc(sizeof(link));
if(s==NULL)
printf("memory error\n");
return;
printf("please enter the value\n");
scanf("%d",&value);
s->data = value;
if(head == NULL)
head = s;
s->next = NULL;
else
p = head;
while(p->next != NULL)
p = p->next;
p->next = s;
s->next = NULL;
void addAll()
int count,i;
printf("please enter the count\n");
scanf("%d",&count);
for(i=0;i<count;i++)
addtail();
void display()
plink p;
p = head;
while(p)
printf("%3d",p->data);
p = p->next;
printf("\n");
plink search(int data)
int i = 0;
plink p;
if(head == NULL)
printf("single link is empty\n");
return NULL;
p = head;
while(p != NULL)
i++;
if(p->data == data)
printf("the number %d is at %d\n",data,i);
return p;
else
p = p->next;
return NULL;
void insert(int target,int insertvalue)
plink p, q, s;
if(head == NULL)
printf("single link is empty\n");
return ;
s = (plink)malloc(sizeof(link));
if(s == NULL)
printf("memory error!\n");
return ;
s->data = insertvalue;
if(head->data == target)
s->next = head;
head = s;
return;
else
p = q = head;
while(p != NULL)
if(p->data == target)
q->next = s;
s->next = p;
printf("insert successful\n");
return ;
else
q = p;
p = p->next;
printf("insert failed!\n");
void delete(int data)
plink p, q;
if(head == NULL)
printf("single link is empty\n");
return ;
if(head->data == data)
p = head->next;
free(head);
head = p;
printf("delete successful\n");
return;
else
p = q = head;
while(p != NULL)
if(p->data == data)
q->next = p->next;
free(p);
printf("delete successful\n");
return ;
else
q = p;
p = p->next;
printf("delete failed!\n");
void deleteAll(int data)
plink p, q;
if(head == NULL)
printf("single link is empty\n");
return ;
p = q = head;
while(p != NULL)
if(head->data == data)
p = head->next;
free(head);
head = p;
else if(p->data == data)
q->next = p->next;
free(p);
p = q->next;
else
q = p;
p = p->next;
int main(void)
int operation;
printf("please enter your operation\n");
do
printf("\t1.add a number at the first\n");
printf("\t2.add a number at the last\n");
printf("\t3.add multi number\n");
printf("\t4.display the singlelink\n");
printf("\t5.search a number\n");
printf("\t6.insert the insertvalue at the target\n");
printf("\t7.delete the value\n");
printf("\t8.delete all of the value\n");
printf("\t9.quit\n");
scanf("%d",&operation);
switch(operation)
case 1:addfront();break;
case 2:addtail();break;
case 3:addAll();break;
case 4:display();break;
case 5:
int value;
printf("enter the search number\n");
scanf("%d",&value);
search(value);break;
case 6:
int target,insertvalue;
printf("enter the target and insertvalue \n");
scanf("%d%d",&target,&insertvalue);
insert(target,insertvalue);
break;
case 7:
int value;
printf("enter the value to delete \n");
scanf("%d",&value);
delete(value);
break;
case 8:
int value;
printf("enter the value to delete all \n");
scanf("%d",&value);
deleteAll(value);
break;
case 9:return;
default:printf("error input\n");
while(1);
参考技术A 写这段程序的人编程习惯不是很好,希望慢慢改进;在使用指针之前,必须初始化,不然会出现段错误。链表扫描输入建议用getchar()函数,可以正确的处理掉回车符。
按照scanf()输入,建立链表我修改为:
LinkList TCreatList()
Lnode *H,*T,*P=NULL; int x;
scanf("%d",&x);
while( 1)
P= (Lnode *)malloc(sizeof(Lnode));
P->data=x;
if(H==NULL)
H=P;
else
T->next=P;
T=P;
else if(x==0)
break;
return H;
还有,仔细检查插入函数,a和x的实际意义是什么,我看不懂;
参考资料:h=p
本回答被提问者和网友采纳以上是关于c#-单链表的主要内容,如果未能解决你的问题,请参考以下文章